Welcome to our guide on using find command to search for files and directories in Linux. find is a command-line utility that searches one or more directory trees of a file system, locates files based on some user-specified criteria and applies a user-specified action on each matched file. It can also be use with other Linux/Unix commands to apply further actions on files or directories found.
How to Use Find Command in Linux
We will walk through various examples on how to search for files and directories in Linux using find command.
Search for Files Using Find command
To search for a file using find command, you need to speficy the directory where to search. For example, to search for a file called file.txt on the directory, /home/user.
find /home/user -name file.txtTo explicitly specify that the file you are searching for is a file, use -type f where f specifies that what is being searched for should be a file.
find /home/user -name file.txt -type fTo search for all files with the extensions .txt on the directory /;
find /r -name "*.txt" -type fTo search for all the files that begins with the keyword foo in the current directory;
find . -name "foo.*" -type fTo search for files in multiple directories;
find /usr /etc /home/user -name file.txt -type fSearch for Directories Using Find Command
To search for directiries, use -type d. For example to search for directory with the name squid.
find / -name squid -type dTo search for directories with the keyword php;
find / -name "php*" -type dSearch for Empty Files and Directories Using Find Command
To search for empty files in the root file system tree;
find / -type f -emptyTo search for empty directories in the root file system tree;
find / -type d -emptyTo search for all files beginning with the keyword php and are empty;
find / -name "php*" -type f -emptyPerform Case Insensitive Search Using Find Command
To perform a case insensitive search using find command, use the -iname option.
find . -iname file.txtfind / -iname file.txt -type ffind /etc -iname txt-file -type dFind and Process Files and Directories
To find and process files using find command, use the -exec option. The -exec option enables you to run other commands against all the files returned by your current find term.
For example, to find all files that ends with .sh extension in the current directory and make them executable;
find . -name "*.sh" -type f -exec chmod +x {} \;It is however much more secure and recommended to use the -execdir option which run the specified command in the subdirectory containing the matched file instead of the directory in which you started find.
find . -name "*.sh" -type f -execdir chmod +x {} \;To remove all empty files in the current directory;
find . -type f -empty -exec rm -rf {} \;To remove all empty directories in the current directory;
find . -type d -empty -exec rmdir {} \;Find and Delete Files and Directories Using Find Command
You can find and delete files and directories using the -delete option.
For example, to delete empty directories on the current directory.
Use this option when you are certain of the matches as this is similar to executing the rm -rf.
find . -type d -empty -deleteTo find and delete all files with the .txt extension,
find . -name "*.txt" -type f -empty -deleteFind files by Modification time
Use the -newerXY option to find file change date.
Where XY can be any of the;
- a – The access time of the file reference
- B – The birth time of the file reference
- c – The inode status change time of reference
- m – The modification time of the file reference
- t – reference is interpreted directly as a time
The syntax is as follows:
find /dir/ -type f -newerXY 'yyyy-mm-dd'
To find and list the files;
find /dir/ -type f -newerXY 'yyyy-mm-dd' -ls
Let us create three files and directories for example, with custom modification time;
touch -d "2019-01-01" testfile1
touch -d "2019-03-01" testfile1
touch -d "2019-05-01" testfile1Find files modified on 2019-01-01.
find . -type f -newermt 2019-01-01Find files modified between 2019-01-01 and 2019-05-01.
find . -type f -newermt 2019-01-01 ! -newermt 2019-05-01Find files accessed on 2019-05-01.
find . -type f -newerat 2019-05-01Find the files accessed 7 days ago.
find /etc -iname "*.conf" -atime -7 -type ffind /etc -type d -atime -7or
find /etc -iname "*.conf" -atime -7 -type f -lsFind files accessed more than 7 days ago
find /etc -iname "*.conf" -atime +7 -type ffind /etc -type d -atime +7or
find /etc -iname "*.conf" -atime +7 -type f -lsList all the files accessed exactly 7 days ago.
find /etc -iname "*.conf" -atime 7 -type ffind /etc -iname "*.conf" -atime 7 -type f -lsList all the directories accessed exactly 7 days ago.
find /etc -type d -atime 7Find files that don’t match a pattern
Use the -not argument of the find command to find all files that don’t match a filename pattern.
find . -type f -not -name "*.txt"Find All Files of a Particular Size
To locate files with specific sizes, use the -size argument. The units that can be used to specify the sizes;
- b – for 512-byte blocks (this is the default if no suffix is used)
- c – for bytes
- w – for two-byte words
- k – for Kilobytes (units of 1024 bytes)
- M – for Megabytes (units of 1048576 bytes)
- G – for Gigabytes (units of 1073741824 bytes
For example, to locate all the files with 10 bytes in the current directory;
find . -type f -size 10c -exec ls {} \;For directories;
find . -type d -size +10cTo find all the files and directories with more than 10 Kilobytes;
find . -type f -size +10k -exec ls -lh {} \;find / -type d -size +10k -exec ls -alhd {} \;Find files or directories that are less than 10k;
find . -type f -size -10k -exec ls {} \;find / -type d -size -10k -exec ls -alhd {} \;Find Files Based On their Permissions
find any file with group write permission;
find -perm -g=wNote that you can use + instead of =.
Find files writable by the file owner;
find -perm -u=wFind file which are writable by all (owner, group, world);
find -perm -a=wFind files which are writable by both their owner and their group;
find -perm -g+w,u+wFind Files Based On octal Permissions
Find files with read, write, execute permissions for owner, group and world (777).
find -perm 777Find files with read and write permissions for owner and group
find -perm 550To locate files in current directory that are not owned by any user;
find . -type f -nouserLocate Programs with SUID/SGID Permissions set
Set User ID and Set Group ID are Linux permissions that are applied to executable programs. This sometimes poses a security threat in the sense that it causes the system to treat programs with these bits set to be executed with the permissions of the program owner (SUID) or group owner (SGUID) rather than with the permissions of the user running the program itself.
Similarly, you can locate these programs using find command with the -perm mode option.
For example, to search for all the file with SUID and SGID bits set, run the command below;
find / -perm +6000 -type fWell, we have so far covered few examples of using find command to search for directories and files on Linux.
Read more on find command manual pages.
Other Tutorials
Extract Log Lines of Specific Dates from a Log File
Delete Lines Matching a Specific Pattern in a File using SED
Delete Lines Matching Specific Pattern in a File using VIM


Hi,
Great article
Thanks a lot