
Linux Command Line
The find
command is a powerful tool for searching and manipulating files in Linux. Whether you’re a beginner or an advanced user, understanding how to use this command can greatly improve your productivity and efficiency when working with files in the command line.
When to use find
:
- When you need to search for a specific file or group of files in a directory or its subdirectories
- When you need to perform an action on a specific file or group of files, such as deleting or renaming them
- When you need to search for files that match certain criteria, such as file type or modification time
Basic usage of find
:
The basic syntax for using find
is:
find [directory] [options] [expression]
directory
is the starting point for the search. This can be a specific directory or.
for the current directory.options
specify how the search should be conducted. Some common options include-name
for searching by file name,-type
for searching by file type, and-mtime
for searching by modification time.expression
is the search criteria. For example, if searching for files with a specific file name, the expression would be the file name.
Examples:
- Search for all files with the
.txt
extension in the current directory and its subdirectories:
find . -name "*.txt"
- Delete all files that have not been modified in the last 30 days in the
/tmp
directory:
find /tmp -mtime +30 -delete
- Search for all directories in the
/home
directory:
find /home -type d
- Find all the files whose size is greater than 100MB in a directory called data and its subdirectories
find data -size +100M
- Find all the files that were modified in the last 7 days in a directory called documents and its subdirectories and change the permissions to 755
find documents -mtime -7 -exec chmod 755 {} \;
- Find all the files with the extension .log in the /var/log directory and print the full path of each file
find /var/log -name "*.log" -print
Note: The -print
option is used to print the full path of each file.
These are just a few examples of the many ways you can use the find
command to search and manipulate files in Linux. With its powerful options and expressions, it can be used to perform a wide range of tasks, making it an essential tool for any Linux user to master.
See our list of 75 Linux commands you should know about.