If you want to locate or search for files and directories on your Linux system, the find command is one of the most useful tools you can use. The find command allows you to search based on various criteria such as name, type, size, date, ownership, permission, and more. You can also perform actions on the files and directories that match your search expression, such as running commands, deleting files, or printing them.
How to use the find command in Linux
The general and simplified syntax for the find command is as follows:
find [options] [path...] [expression]
The options attribute controls the treatment of symbolic links, debugging options, and optimization methods. The path… attribute defines the starting directory or directories where find will search for files. The expression attribute is made up of options, search patterns, and actions separated by operators.
If you don’t specify any options or expression, the find command will list all files and directories in the specified path recursively. For example:
find /home/bob
This will list all files and directories under /home/bob.
If you don’t specify any path, the find command will use the current directory as the default path. For example:
find
This will list all files and directories in the current directory.
You can also specify multiple paths for the find command to search. For example:
find /home/bob /var/log
This will list all files and directories under /home/bob and /var/log.
To limit the depth of the search, you can use the -maxdepth option followed by a number. For example:
find /home/bob -maxdepth 2
This will list all files and directories under /home/bob up to two levels deep.
To print only the file names without any other information, you can use the -printf option with a format string. For example:
find /home/bob -printf "%f\n"
This will print only the file names without any path or other details.
To print only the file paths without any other information, you can use the -print0 option with a null character as a separator. For example:
find /home/bob -print0
This will print only the file paths without any newline or other characters. This is useful when you want to pipe the output of the find command to another command that can handle null-separated input, such as xargs .
Find files by name or extension
One of the most common use cases of the find command is to search for files by their name or extension. To do this, you can use the -name option followed by the name of the file you are looking for. For example:
find /home/bob -name document.pdf
This will find all files named document.pdf under /home/bob.
To run a case-insensitive search, you can use the -iname option instead of -name. For example:
find /home/bob -iname document.pdf
This will find all files named document.pdf, Document.pdf, DOCUMENT.pdf, etc. under /home/bob.
You can also use wildcard characters such as * and ? to match any number or any single character respectively. For example:
find /home/bob -name '*.pdf'
This will find all files that end with .pdf under /home/bob.
To find all files that don’t match a certain pattern, you can use the -not option before the -name or -iname option. For example:
find /home/bob -not -name '*.pdf'
This will find all files that don’t end with .pdf under /home/bob.
Find files by type
Sometimes you may need to search for files by their type, such as regular files, directories, symlinks, etc. In Linux, everything is a file, but there are different types of files that have different properties and behaviors.
To search for files by their type, you can use the -type option followed by one of the following descriptors to specify the file type:
f
: a regular filed
: a directoryl
: a symbolic linkc
: a character deviceb
: a block devicep
: a named pipe (FIFO)s
: a socket
For example, to find all directories under /home/bob, you can use:
find /home/bob -type d
To find all symlinks under /home/bob, you can use:
find /home/bob -type l
To find all regular files under /home/bob, you can use:
find /home/bob -type f
You can also combine the -type option with other options such as -name or -size to narrow down your search. For example, to find all regular files that end with .txt and are larger than 1 MB under /home/bob, you can use:
find /home/bob -type f -name '*.txt' -size +1M
Find files by date
Another common criterion for searching files is their date. You can use the following options to specify the date of the files you are looking for:
mtime
: modification time (the last time the file content was changed)atime
: access time (the last time the file was read or written)ctime
: change time (the last time the file metadata was changed
The date options take a number as an argument, which represents the number of days since the file was modified, accessed, or changed. For example, to find all files that were modified exactly 7 days ago under /home/bob, you can use:
find /home/bob -mtime 7
To find all files that were modified more than 7 days ago under /home/bob, you can use the + operator before the number:
find /home/bob -mtime +7
To find all files that were modified less than 7 days ago under /home/bob, you can use the - operator before the number:
find /home/bob -mtime -7
You can also use fractions of days to specify the date. For example, to find all files that were modified in the last 12 hours under /home/bob, you can use:
find /home/bob -mtime 0.5
You can also combine the date options with other options such as -type or -name to fine-tune your search. For example, to find all directories that were changed in the last 24 hours under /home/bob, you can use:
find /home/bob -type d -ctime 1
Find files by permissions
Another useful criterion for searching files is their permissions. You can use the -perm option followed by a number or a symbol to specify the permissions of the files you are looking for. The number represents the octal notation of the permissions, such as 644, 755, etc. The symbol represents the symbolic notation of the permissions, such as u=rwx,g=r,o=r, etc.
For example, to find all files that have exactly 644 permissions under /home/bob, you can use:
find /home/bob -perm 644
To find all files that have any of the permissions specified by the number under /home/bob, you can use the / operator before the number:
find /home/bob -perm /644
This will find all files that have at least one of the following permissions: 644, 645, 646, 647, 654, 655, 656, 657, etc.
To find all files that have all of the permissions specified by the number under /home/bob, you can use the - operator before the number:
find /home/bob -perm -644
This will find all files that have at least the following permissions: 644, 744, 754, 764, 774, etc.
To find all files that have exactly the permissions specified by the symbol under /home/bob, you can use:
find /home/bob -perm u=rwx,g=r,o=r
This will find all files that have exactly rwxr--r--
permissions.
To find all files that have any of the permissions specified by the symbol under /home/bob, you can use the / operator before the symbol:
find /home/bob -perm /u=rwx,g=r,o=r
This will find all files that have at least one of the following permissions:
rwxr--r--
, rwxr-xr--
, rwxrw-r--
, rwxrwxr--
, etc.
To find all files that have all of the permissions specified by the symbol under /home/bob, you can use the - operator before the symbol:
find /home/bob -perm -u=rwx,g=r,o=r
This will find all files that have at least rwxr--r--
permissions.
You can also combine the perm option with other options such as -type or -name to narrow down your search. For example, to find all regular files that have exactly 755 permissions and end with .sh under /home/bob, you can use:
find /home/bob -type f -perm 755 -name '*.sh'
Find files by owner or group
Another common criterion for searching files is their owner or group. You can use the -user option followed by a username or a user ID to specify the owner of the files you are looking for. You can also use the -group option followed by a group name or a group ID to specify the group of the files you are looking for.
For example, to find all files that are owned by bob under /home/bob, you can use:
find /home/bob -user bob
To find all files that belong to dev under /home/bob, you can use:
find /home/bob -group dev
You can also combine the user and group options with other options such as -type or -name to refine your search. For example, to find all directories that are owned by bob and belong to dev under /home/bob, you can use:
find /home/bob -type d -user bob -group dev
Find files by content
Another useful criterion for searching files is their content. You can use the
-exec option followed by a command to execute on each matched file. The
command can be any valid Linux command, such as grep
, sed
, awk
, etc.
The command must be terminated by a semicolon ; or a plus sign + . The
semicolon ; will execute the command once for each matched file, while the
plus sign + will execute the command once for all matched files. The {} symbol
represents the name of the matched file.
For example, to find all files that contain the word “linux” under /home/bob, you can use:
find /home/bob -exec grep -l linux {} \;
This will execute the grep command with the -l option (which prints only the file name) and the word “linux” on each matched file. The ; symbol escapes the semicolon from being interpreted by the shell.
To find and replace the word “linux” with “Linux” in all files under /home/bob, you can use:
find /home/bob -exec sed -i 's/linux/Linux/g' {} \;
This will execute the sed command with the -i option (which edits the file in place) and the substitution expression ‘s/linux/Linux/g’ on each matched file.
To find and print the first line of each file under /home/bob, you can use:
find /home/bob -exec head -n 1 {} +
This will execute the head command with the -n 1 option (which prints only the first line) and all matched files as arguments.
Perform Actions on Found Files
The find command can also perform actions on the found files such as executing commands, deleting files, or printing them. To perform an action on a file, you need to use one of the following options followed by a command or an argument:
-exec
: execute a command on each found file-xargs
: pass the found files as arguments to another command-delete
: delete each found file-print
: print the name of each found file-fprint
: write the name of each found file to a file
For example, to execute a command on each found file, you need to use the -exec option followed by the command and a pair of curly braces {} that represent the name of each found file. You also need to end the command with a semicolon (;) or a plus sign (+). The semicolon (;) will run the command once for each found file, while the plus sign (+) will run the command once with all found files as arguments. For example, to count the number of lines in each .txt file in the current directory, you need to use:
find . -type f
To pass the found files as arguments to another command, you need to use the -xargs option followed by the command. The xargs command will take the input from the find command and split it into chunks that fit into the system’s maximum command-line length. For example, to compress all .log files in the /var/log directory using gzip , you need to use:
find /var/log -type f -name '*.log' | xargs gzip
To delete each found file, you need to use the -delete option. Be careful when using this option, as there is no confirmation or undo option. For example, to delete all files that end with .bak in the /home/bob directory, you need to use:
find /home/bob -type f -name '*.bak' -delete
To print the name of each found file, you need to use the -print option. This is the default action of the find command, so you don’t need to specify it explicitly. For example, to print the name of all files that are larger than 1 megabyte in the current directory, you need to use:
find . -type f -size +1M
To write the name of each found file to a file, you need to use the -fprint option followed by the name of the file. For example, to write the name of all files that are older than 30 days in the /home/bob directory to a file named old_files.txt , you need to use:
find /home/bob -type f -mtime +30 -fprint old_files.txt
Conclusion
The find command is a powerful and versatile tool that can help you locate and manipulate files and directories based on various criteria. You can also combine it with other commands to perform complex tasks on your files.
To learn more about the find command, you can check its man page by typing:
man find
We hope you found this article helpful and informative. If you have any questions or feedback, please feel free to contact us.