Using Find Command to Search for Files and Directories in Linux

|
Last Updated:
|
|

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.txt

To 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 f

To search for all files with the extensions .txt on the directory /;

find /r -name "*.txt" -type f

To search for all the files that begins with the keyword foo in the current directory;

find . -name "foo.*" -type f

To search for files in multiple directories;

find /usr /etc /home/user -name file.txt -type f

Search 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 d

To search for directories with the keyword php;

find / -name "php*" -type d

Search for Empty Files and Directories Using Find Command

To search for empty files in the root file system tree;

find / -type f -empty

To search for empty directories in the root file system tree;

find / -type d -empty

To search for all files beginning with the keyword php and are empty;

find / -name "php*" -type f -empty

Perform Case Insensitive Search Using Find Command

To perform a case insensitive search using find command, use the -iname option.

find . -iname file.txt
find / -iname file.txt -type f
find /etc -iname txt-file -type d

Find 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 -delete

To find and delete all files with the .txt extension,

find . -name "*.txt" -type f -empty -delete

Find 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" testfile1

Find files modified on 2019-01-01.

find . -type f -newermt 2019-01-01

Find files modified between 2019-01-01 and 2019-05-01.

find . -type f -newermt 2019-01-01 ! -newermt 2019-05-01

Find files accessed on 2019-05-01.

find . -type f -newerat 2019-05-01

Find the files accessed 7 days ago.

find /etc -iname "*.conf" -atime -7 -type f
find /etc -type d -atime -7

or

find /etc -iname "*.conf" -atime -7 -type f -ls

Find files accessed more than 7 days ago

find /etc -iname "*.conf" -atime +7 -type f
find /etc -type d -atime +7

or

find /etc -iname "*.conf" -atime +7 -type f -ls

List all the files accessed exactly 7 days ago.

find /etc -iname "*.conf" -atime 7 -type f
find /etc -iname "*.conf" -atime 7 -type f -ls

List all the directories accessed exactly 7 days ago.

find /etc -type d -atime 7

Find 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 +10c

To 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=w

Note that you can use + instead of =.

Find files writable by the file owner;

find -perm -u=w

Find file which are writable by all (owner, group, world);

find -perm -a=w

Find files which are writable by both their owner and their group;

find -perm -g+w,u+w

Find Files Based On octal Permissions

Find files with read, write, execute permissions for owner, group and world (777).

find -perm 777

Find files with read and write permissions for owner and group

find -perm 550

To locate files in current directory that are not owned by any user;

find . -type f -nouser

Locate 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 f

Well, 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.

man find

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

How to Install and Use 7zip File Archiver on Ubuntu 18.04

Viewing System Processes using ps and top commands

SUPPORT US VIA A VIRTUAL CUP OF COFFEE

We're passionate about sharing our knowledge and experiences with you through our blog. If you appreciate our efforts, consider buying us a virtual coffee. Your support keeps us motivated and enables us to continually improve, ensuring that we can provide you with the best content possible. Thank you for being a coffee-fueled champion of our work!

Photo of author
koromicha
I am the Co-founder of Kifarunix.com, Linux and the whole FOSS enthusiast, Linux System Admin and a Blue Teamer who loves to share technological tips and hacks with others as a way of sharing knowledge as: "In vain have you acquired knowledge if you have not imparted it to others".

1 thought on “Using Find Command to Search for Files and Directories in Linux”

Leave a Comment