40 examples of find command in Linux

Hello Geeks, In this post, we will learn about find command that is one of the important command in Linux. It can help you find anything you want to search in our machine.
In Linux, everything is in the form of files and find command can be used with a lot of options like name, permissions, modification/accessed time, matching string, finding a particular format file. Using the relevant option with find command can ease the task and you will get the results quickly.

find command is available on all the flavors of Linux. In this tutorial, we will learn examples of find command that a Linux System administrator or a Linux User should know.

Basic Syntax of find command

# find <path> <selection criteria> <action>

Search Files using Name

1. List all the files in the current directory and sub-directories

Using find without any option will list all the files in the current directory and sub-directories.
# find 
or
# find .

2. List all the files in any specific diectory

This will show all the files in that particular path specified after find.
# find /<path>
3. Find all the files of your present working directory
To list all the files (not directories) in pwd use below command
# find . -type f

4. Find all the directories in your present working directory

This option will help you find all the directories in your pwd
# find . -type d

5. Find files using name in Current Directory

If you want to search for a file of a particular name. For example, to find “new.php” file in the current directory and sub-directories
# find . -name "new.php"

6. Find files using the name in a specific directory

Specify the path after find to search in that particular directory.
# find /boot -name grub.cfg

7. Find file(s) in multiple directories

You can give multiple directories in the path.
# find /boot /etc -name grub

8. Find file using name and ignoring case

It will show both lowercase and uppercase results, hence ignoring the case.
# find <path> -iname <file or directory>

9. Find Directory using name

Specify the name of directory to be searched after -name
# find / -type d  -name LinuxforGeek

10. Find files with particular extension/format

This is very useful thing, in case you want to search for any particular file format example shell scripts ends with .sh, music formats are in .mp3, text files are in .txt
# find / -name "*.php"

11. Using maxdepth for limiting the search to a specific directory

In case you want to skip huge trees of sub directories and limit your search to one or two depth.
# find /<path> -maxdepth 2 -name "search pattern"

12. Find all the files other than mentioned type / Invert match

It will show all the results except the specified in selection criteria section.
# find /test -not -name *.php"
or
# find ./test ! -name "*.php"

13. Using combination of search criteria

It is possible to combine two search criteria by specifying name and including invert match as shown in below example.
# find /test -name 'abc*' -not -name '*.php' -not -name "*.sh"

14. Using OR condition in find command

# find / -name "abc.txt" -o -name "linuxforgeek.txt"

15. Search File names matching regular expression pattern

# find /test  -regex ".*\.\(txt\|php\)$"

16. Find all hidden files

# find ~ -type f -name ".*"

Finding files based on Permissions, owner or group

17. Finding files with permissions

# find /test -type f -perm 0777

18. Finding files that not having 777 permissions

# find /test -type f -not -perm 0777
or 
# find /test -type f ! -perm 0777

19. Find files with suid bit set

find /usr -perm /u=s

20. Find files with sgid bit set

# find /usr -perm /g=s

21. Find files with sgid bit set and 755 permissions

# find / -perm 2755

22. Find files with sticky bit set and 755 permissions

# find / -perm 1755

23. Find read only files

# find / -perm /u=r

24. Find all the executables files

# find /test -perm /a=x

25. Find all the files owned by a user

# find / -user john

26. Find all the files owned by a group

# find / -group dba

Finding files based on modification date and time

27. Find files modified 10 days back

# find / mtime 10

28. Find files accessed in last 10 days

# find /-atime 10

29. Find files modified between 5 to 20 days

# find /-mtime +5 -mtime -20

30. Find files changed in last one hour

# find / -cmin -60

31. Files modified in last one hour

# find / -mmin 60

32. Files accessed in last one hour

# find / -amin 60

Finding files based on size

33. Finding file of a particular size

# find / -size 20M

34. Finding files between size range

# find / -size +10M -size -50M

35. Finding smallest and largest file

# find / -type f -exec ls -s {} \; | sort -n -r | head -5

36. Find empty files

find /tmp -type f -empty

37. Find empty directories

# find /tmp -type d -empty

Advanced operations: Performing action on the find output

38. List all the found files

You can easily list files of any size using find command. Below example lists the files of larger than 100 MB
# find . -size +100M -exec ls -lrth {} \;

39. Find specific Files and Delete

Below example can be used to find all the mp3 files of greater than 10 MB and execute part will remove(delete) the same.
# find / -type f -name *.mp3 -size +10M -exec rm -rf {} \;

40. Find files with specific permissions and chmod (change permissions) to 755

Below command can be used to find all the files with 644 permissions and the action part is to execute “chmod 755” on all the files found.
# find / -type f -perm 644 -print -exec chmod 755 {} \;
That’s all for this tutorial, Hope you have understood find command usage with given practical examples.