Find Command to Search for Files With Examples

Find Command

The find command performs a real-time search in the local file systems to find files based on various search criteria like when the file status was last changed, permissions, user ownership, group ownership, size etc.

Syntax

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path…] [expression]

 

Example 1.) List All Files In Current Working Directory And Sub Directories

This command lists out all the files in the current working directory as well as the subdirectories in the current working directory.

[[email protected] Desktop]$ find
.
./aa
./aa/numbers.txt
./aa/numbers1.txt
./hello.txt

Example 2.) Find Specific File Under Current Working Directory

This command will find all the files named numbers.txt in a current working directory as well as the subdirectories in the current working directory.

[[email protected] Desktop]$ find . -name numbers.txt
./aa/numbers.txt

Example 3.) Find Files Under ETC Directory

This command lists out all the files starting with name “cron” but ends with whatever else.

[[email protected] ~]# find /etc -name 'cron*'
/etc/cron.hourly
/etc/selinux/targeted/modules/active/modules/cron.pp
/etc/sysconfig/crond
/etc/systemd/system/multi-user.target.wants/crond.service
/etc/cron.daily
/etc/pam.d/crond
/etc/cron.d
/etc/cron.deny
/etc/cron.monthly
/etc/cron.weekly
/etc/crontab

Example 4.) Find Only Files of type file

If you want to find only files no directories in your system, then use the following command:

[[email protected] ~]# find /etc -type f -name 'cron*'
/etc/selinux/targeted/modules/active/modules/cron.pp
/etc/sysconfig/crond
/etc/pam.d/crond
/etc/cron.deny
/etc/crontab

-type f option tell find command to list only regular files.

Example 5.) Find Only Directories

If you want to search only Directories no files in your system, then use the following command:

[[email protected] ~]# find /etc -type d -name 'cron*'
/etc/cron.hourly
/etc/cron.daily
/etc/cron.d
/etc/cron.monthly
/etc/cron.weekly

This command will display only directories starting with name cron and ending with whatever else under /etc directory.

Example 6.) Find Files With Permissions 777

This command will find all the files and directories with permission 777. Now if you are not familiar with permission 777 it means anybody can read, write and execute that file. It’s a very insecure permission setting.

[[email protected] ~]$ find . -perm 777
./newfile2
./file8

Example 7.) Change Permissions Of All Files With 777 to 555

If you want to replace files and directories having permission 777 to 555 with find command then use the following syntax:

[[email protected] ~]# find . -perm 777 -exec chmod 555 {} \;

Example 8.) Search For Files With A Size Of Exactly 10 Megabytes

[[email protected] ~]# find -size 10M

Example 9.) List All Files With A Size Less Than 10 Kilobytes

[[email protected] ~]# find -size -10k

Example 10.) The -mmin Option, Followed By The Time In Minutes

To find all files that had their file content changed more than 120 minutes ago.

[[email protected] ~]# find . -mmin +120

To find all files that had their file content changed less than 120 minutes ago.

[[email protected] ~]# find . -mmin -120

To find all files that had their file content changed exactly 120 minutes ago

[[email protected] ~]# find . -mmin 120

Example 11.) Find a list of any file or directory in or below the current directory owned by the user named user7

[[email protected] ~]# find . -user user7

Example 12.) Find a list of any file or directory in or below the current directory owned by the group named user7

[[email protected] ~]# find . -group user7

Example 13.) Find a file, starting with named “number”, within the current directory and only search 2 directories deep

[[email protected] ~]# find . -name number* -maxdepth 2 -print

Example  14.) Search directories “./user4” and “./user5” for a file “number.txt”

[[email protected] ~]# find ./user4 ./user5 -name number.txt

Example 15.) Search for files that contain the phrase “number” but do not end in “.txt”

[[email protected] ~]# find . -name '*number*' ! -name '*.txt' -type f

Example 16.) Find a file without showing “Permission Denied” messages

[[email protected] ~]# find / -name number* -print 2>/dev/null

Example 17.) Delete all matching files

[[email protected] ~]# find . -type f -name "number*" -exec rm -f {} \;

Example 18.) Delete all matching directories

[[email protected] ~]# find . -type d -name "dir1*" -exec rm -f {} \;

to delete recursively use -r flag

Example 19.) Find all Empty Directories

[[email protected] ~]# find /tmp -type d -empty

Example 20.) Find all Empty Files

[[email protected] ~]# find /tmp -type f -empty

Example 21.) Find Size between 10MB – 100MB

[[email protected] ~]# find / -size +10M -size -100M

Example 22.) Find Particular Files of User

[[email protected] ~]# find /home -user user4 -iname "number*

Example 23.) File all Hidden Files

[[email protected] ~]# find /tmp -type f -name ".*"

Summary

So that was a quick tutorial on the linux find command. Have any questions? Leave a comment below.

Related Commands

locate
which
whereis

Add Comment