The Linux find command is a useful tool for searching through your system. It is used to find files and directories and perform successive operations on them.
The find command is used to search for files in a directory hierarchy based on their type, date, permissions, ownership, size, and many more. It can be used alongside other Linux utilities such as grep and sed.
In this article, we will show you how to use the find command in your Linux system.
Find command syntax
The general command syntax is as shown below.
find [options] [path...] [expression]
find – searches the directory tree by evaluating the given expression from left to right.
The options – control the treatment of symbolic links. These options control the behavior of find but are specified immediately after the last pathname.
The path… – defines the starting directory or directories where find will search the files.
The expression attribute is made up of options, search patterns, and actions separated by operators.
To search through a file or a directory the user invoking the find command must have read permissions to that file or directory.
We will now look at how to use the find command in Linux
1.Find files by name.
This is the common way of using the find command. To find a file by its name, employ -name option followed by the name of the file being searched for.
To search for a file named “Businessregistration.pdf” in the My Documents folder.
find /home/Jil/Documents/ -type f -name Businessregistration.pdf
The command execution is as shown below.
[[email protected] ~]$ find /home/Jil/Documents/ -type f -name Businessregistration.pdf /home/Jil/Documents/Businessregistration.pdf
To search for a case sensitive file replace the -name option with -iname as shown
find /home/Jil/Documents/ -type f -iname Businessregistration.pdf
The command output is as shown below.
[[email protected] ~]$ find /home/Jil/Documents/ -type f -iname Businessregistration.pdf /home/Jil/Documents/Businessregistration.pdf /home/Jil/Documents/BUSINESSREGISTRATION.pdf
Notice the output above matches all the files with “Businessregistration.pdf” and “BUSINESSREGISTRATION.pdf”
2. Find files by type.
To search for specific file types, e.g symbolic links, directories, or regular files employ -type option followed by descriptors.
Descriptors define the file type. They include the following:-
- b: for block devices.
- c: for character devices.
- d: for directory.
- f: for regular file.
- l: for symbolic link
- p: for named pipe
- s: for socket
To find all the directories located in /home/Jil/Documents, execute the command :
find /home/Jil/Documents -type d
The command output looks something like this.
[[email protected] ~]$ find /home/Jil/Documents/ -type d /home/Jil/Documents/ /home/Jil/Documents/sao tome /home/Jil/Documents/Philip-published-articles-monthly - Google Drive_files
3. Find files by size.
To find files by size, we employ the -size option followed by the size criteria. Use the following suffixes to specify the file size.
- b: for 512-byte blocks (this is the default if no suffix is used).
- c: for bytes
- W: for two-bytes words
- K: kilobytes
- M: for megabytes
- G: for gigabytes
To find all files greater than 1 Megabyte in /home/Jil/Documents , run the command below.
find /home/Jil/Documents/ -type f -size +1M
The output is as below.
[[email protected] ~]$ find /home/Jil/Documents/ -type f -size +1M /home/Jil/Documents/sarah passport application.pdf /home/Jil/Documents/muchiri passport application.pdf /home/Jil/Documents/receipt - muchiri passport application.pdf /home/Jil/Documents/George Invoice .pdf /home/Jil/Documents/receipt - benard .pdf /home/Jil/Documents/sao tome/INTERIM RETURNS COPY .pdf /home/Jil/Documents/Philip-published-articles-monthly - Google Drive_files/2947586374-waffle_k_ltr.css
To search for files less than 1 Megabyte in the /home/Jil/Documents run the command below.
find /home/Jil/Documents/ -type f -size -1M
4. Find Files by their permissions.
To search for files by their permissions, we use the -perm option.
find /var/www/html/ -perm 644
This gives the output below.
[[email protected] ~]$ find /var/www/html/ -perm 644 /var/www/html/info.php
The numeric mode can be prefixed with either minus (-) or slash (/)
When the prefix is a / then at least one category (user, group or others ) must have at least the respective bits set for a file to match.
Example to demonstrate the use of /
find . -perm /444
The command above will match all the files with read permissions set for either user, group, or others.
When the prefix is a –, for the file to match, at least the specified bits must be set.
Example to demonstrate the use of –
find . -perm -664
The command above will search for files with read and write permissions for owner and group and are readable by others.
5. Find Files by Owner.
To search for files owned by a given user or group, use the -user and -group options.
To search for all files and directories owned by user Jil, run the command below.
find / -user Jil
6. Find and delete files
To find and delete all matching files, append -delete option at the end of the match expression. This option must be used with lots of caution and you must be sure of the files to be deleted as once the files are deleted, they cannot be recovered. Ensure you have a backup before deleting the files.
To delete all files ending with .temp in the /var/log/ execute the command below.
find /var/log/ -name `*.temp` -delete
When used on directories, the find command only deletes empty directories same as rmdir.
7. Find files by extension.
Searching for files by extension is the same as searching files by name.
To search for all files with a .gz extension in the /var/log/nginx/ directory, we run the command below.
sudo find /var/log/nginx -type f -name '*.gz'
The output is as shown below.
[[email protected] ~]$ sudo find /var/log/nginx -type f -name '*.gz' /var/log/nginx/access.log-20210910.gz /var/log/nginx/error.log-20210910.gz
Note that you must either quote the pattern or escape the asterisk * symbol with backslash \ so that it doesn’t get interpreted by the shell when you use the wildcard character.
To find files that don’t end in a given expression, i.e files that don’t match an expression, use -not option.
sudo find /var/log/nginx -type f -not -name '*.gz'
The command output.
[[email protected] ~]$ sudo find /var/log/nginx -type f -not -name '*.gz' /var/log/nginx/access.log /var/log/nginx/error.log
That sums up our article on How to use find command in Linux. For more information be sure to check man pages. We are glad you have followed along. We trust the article will be helpful as you practice. For individuals preparing for LPIC 1 certification, this article will be very helpful to you. Thank you.