20 find COMMAND Examples

Search for files in a directory hierarchy.


20 find COMMAND Examples

find - search for files in a directory hierarchy.

find . -name file.txt
./file.txt
  • find command
  • . means it will search all the directory you are in and sub directories.
  • -name will look for that name exactly, if a character is different or upper or lower case, it will ignore it.
  • file.txt filename

2 - Search ignoring case sensitive

find /home/ -iname file.txt
./file.txt
./File.txt
  • /home/ will only search this directory recursively (sub directories)
  • upper or lower case name

3 - Search Directories

find / -type -d -name Photos file.txt
/home/user/Photos
find /home/ -name *ile*
./home/file.txt
./home/File.txt
./home/My_Files-NEWS.txt
./home/file.sh
  • finds all files that have the word file at the beginning, middle or end

5 - Search by File Type

find / -type -f -name Photos *.odt
./file.odt
./terminalroot.odt
  • -f only files with extension odt, in that case, even without -f would also find

6 - Search by permission and find all files that have permission 777

find . -type f -perm 0777 -print

7 - Search other than permission finds all files that do not have permission 777

find / -type f ! -perm 777

8 - Search for files in empty directories without telling if it is - type d or - type f it searches both

find MyDir/ -empty
MyDir/EmptyDir
MyDir/EmptyFile.txt
  • -type d search only directories
find MyDir/ -type d -empty
MyDir/EmptyDir
  • -type f looks for files only
find MyDir/ -type f -empty
MyDir/EmptyFile.txt

9 - Search for hidden folders

find /tmp -type f -name ".*"

10 - Search by size will find all files larger than 10 MB

find . -type f -size +10M

You will find all files smaller than 10MB

find. -type f -size -10M

11 - Search and remove with -exec

find . -type f -name EmptyFile.txt -exec rm -f {} \;

Or with xargs

find . -type f -name EmptyFile.txt | xargs rm -f

12 - Search by Name Within File

find My_Files/ -name "*.*" -exec grep -Hin "Anomalies" {} \;
My_Files/file.txt:1:Anomalies

13 - Searches for ACCESSED (atime) files in the last 24 hours (for more than 3 days, use +3)

find . -type f -atime -1 -exec ls -l {} \;

14 - Searches for ACCESSED (amin) files in last 5 minutes

find . -type f -amin -5

15 - Searches for CREATED (ctime) files in the last 12 hours

find . -type f -ctime -0.5 -exec ls -l {} \;

16 - Searches for MODIFIED (mtime) files in the last 6 hours

find . -type f -mtime -0.25

17 - Search for Sticky Bit Files with 551 Permission

find / -perm 1551

18 - Search for SUID Files

find / -perm /u=s

19 - Search for SGID Files

find / -perm /g+s

20 - Search for executable files

find / -perm /a=x

or read only

find / -perm /u=r

There are more possibilities, you can see them all in the command manual:

man find
find --help

Thanks for reading!


find command shell unix linux


Share


YouTube channel

Subscribe


Marcos Oliveira

Marcos Oliveira

Software developer
https://github.com/terroo

Related articles