
The find
command is a useful and frequently used command line utility for Linux administrators. Find command is used to search and locate the list of files and directories based on a user given conditions and match the arguments.
It is also possible to search files and directories based on their type, date, ownership, size, and more. It can also be used in combination with other tools like grep and more.
Linux find Command Syntax
Following is the common syntax of find
command:
find [options] [path...] [expression]
options
– Treat the symbolic links, debugging options, and optimization method.path…
It defines the directories where find will search the files.expression
– search patterns, and actions separated by operators.
1. Find Files by Name
It’s a common requirement to find files by name. To find the file by its name, use the -name
option followed by the name of the file you want to search.
For instance, to search file name with index.html
in the /var/www
directory you would use the following command:
find /var/www -type f -name index.html
To find the file by name and ignoring case, change the -name
option with -iname
:
find /var/www -type f -iname index.html
2. Find Files by Name in Current Directory
Find all the files whose name is data.txt
in a current working directory.
find . -name data.txt
3. Find Files by Extension
To find files by extension is the same as search file by name. For instance, to find all files ending with .pdf
use:
find /home/tecnstuff -type f -name '*.pdf'
You can also search for reverse expression by adding -not
option. For example, to find all files that don’t ending with *.php
you can use:
find /home/tecnstuff -type f -not -name '*.pdf'
4. Find Files by Type
You can search for specific file types like symlinks, directories or normal files. Use -type
option to search file by its type. Following are the descriptors to specify the file type:
f
: a regular filed
: directoryl
: symbolic linkc
: character devicesb
: block devicesp
: named pipes
: socket
For example, to change the file permissions to 644
and directory permissions to 755
using the chmod
command, you would type:
find /var/www/example.com -type d -exec chmod 0755 {} \;
find /var/www/example.com -type f -exec chmod 0644 {} \;
Above commands will change the permissions for all files and directories to 644
and 755
, respectively.
5. Find Files by Size
Sometimes you might need to find files by size, pass -size
parameter along with the size criteria. Below are the suffixes to specify the file size:
b
: 512-byte blocksc
: bytesw
: two-byte wordsk
: KilobytesM
: MegabytesG
: Gigabytes
Below command will find all files of exactly 2M
inside the /home/data
directory:
find /home/data -type f -size 2M
In the following example, we are searching for all files which size between 2M
and 5M
inside current working directory.
find . -type f -size +2M -size -5M
If you want to search file with size less than 2MB
, then you need to use the minus -
symbol before the size value:
find . -type f -size -2M
For search file with size greater than 5MB
, you need to add plus +
symbol before size value:
find . -type f -size +5M
6. Find Files by Modification Date
It is very useful and most common to find files by their last modification, access, or change time. To find .php
files which have been modified in the last ten days:
find /var/www/html -name "*.php" -mtime 10
Another example, to get list of files which were modified before 15
or more days ago. You would use the -daystart
option:
find /var/www/html -mtime +15 -daystart
7. Find Files by Permissions
The -perm
option is used to search files based on the file permissions. For example, to find files with permissions of 644
inside the /var/www/html
directory, you would use:
find /var/www/html -type f -perm 644
To find files which have not 644
permissions, you have to add !
symbol as following:
find /var/www/html -type f ! -perm 644
The above command will list the files which don’t have 644
permissions.
8. Find Files by Owner
Find files owned by a particular user or group, you would use -user
and -group
options.
For instance, to search all files and directories owned by user tecnstuff
, type:
find / -user tecnstuff
9. Find Specific Files and Delete
To delete the matched files, you can add -delete
option at the end of the match expression. Make sure your are confident that matched result no more needed.
For example to delete all files ending with .tmp
from the /var/www/html/temp
you would use:
find /var/www/html/temp/ -name '*.tmp' -delete
10. Find Files by Size and Delete
Find all .log
files with more than 5MB and delete them using one single command.
find / -type f -name *.log -size +5M -exec rm {} \;
Conclusion
This tutorial shown you how to find command with different options and expressions. To learn more about other useful options of find command visit find
man page.
If you have any question or feedback, please leave a comment below.
Leave a Reply