40 practical examples of Linux Find command

This article will share with you 40 practical examples of the most commonly used find command.

Syntax

find [paths] [expression] [actions]

Examples

linux find by name

  1. Find files and directories named “keywords” in the current directory
➜  ~ find . -name "keyword"

# whose names start with "keyword"
➜  ~ find . -name "keyword*"

# whose names contain "keyword
➜  ~ find . -name "*keyword*"

2. Find the file named “keyword” in the current directory

➜  ~ find . -name "keyword" -type f

3. Find the directory named “keyword” in the current directory

➜  ~ find . -name "keyword" -type d

4. Find files whose names start with “keyword1” or “keyword2” in the current directory

➜ find . -name "keyword1*" -o -name "keyword2*" -type f

# Search directory 
➜ find . -name "keyword1*" -o -name "keyword2*" -type d

5. Find empty files or directories in the current directory

➜ find . -empty

# Search only for empty files
➜ find . -empty -type f

# Search only for empty directories
➜ find . -empty -type d

6. Find all types of files in the current directory except the directory

➜ find . ! -type d

linux find by time

7. Find files in the current directory that have been accessed within 2 minutes

➜ find . -amin -2

# 10 minutes ago
➜ find . -amin +10

8. Find files in the current directory that have been accessed within 1 day

➜ find . -atime -1

# 7 days ago
➜ find . -atime +7

9. Find files that have been visited and modified within 2 minutes in the current directory

➜ find . -mmin -2

10. Find the files in the current directory that have been visited and modified within 1 day

➜ find . -mtime -1

11. Find files in the current directory that have a more recent modification time than test.log

➜ find . -newer test.log

12. Find files in the current directory that have access and modification times more recent than test.log

➜ find . -neweram test.log

find . -newe syntax

find -newerXY file

The letters X and Y can be any of the following letters:
       a : last access time
       B : creation time
       c : change time
       m : modification time
       t : interpreted directly as a time

linux find by size

find -size syntax

find /path -size n[kMGTP]
      
   # Find according to file size
     -n   is smaller than a file of size n
      +n   is greater than a file of size n
   # scale indicator
      k       kilobytes (1024 bytes)
      M       megabytes (1024 kilobytes)
      G       gigabytes (1024 megabytes)
      T       terabytes (1024 gigabytes)
      P       petabytes (1024 terabytes)

13. Find for empty files or directories in the current directory

➜ find . -empty

# Search only for empty files
➜ find . -empty -type f

# Search only for empty directories
➜ find . -empty -type d

14. Find files with file size greater than 512k in the current directory

➜  find . -size +512k

# less than 512K
➜  find . -size -512k

linux find by permissions

15. Find files or directories with 700 permissions in the current directory

➜ find . -perm 0700

16. Find files with permission 0755 in the current directory

➜ find . -perm 0755 -type f

linux find by type

Syntax

find /path -type [t]

      b       block special
      c       character special
      d       directory
      f       regular file
      l       symbolic link
      p       FIFO
      s       socket

17. List symbolic links in the current directory

➜ find . -type l

18. Find files or directories with hard links greater than 2 in the current directory

➜ find . -links +2

linux find by owner

19. List files or directories belonging to a user in the current directory

➜ find . -user userName

20. List files or directories belonging to a user id in the current directory

➜ find . -uid 502

21. List files or directories belonging to a certain group in the current directory

➜ find . -group groupName

22. List files or directories belonging to a certain group id in the current directory

➜ find . -gid 20

23. List files or directories belonging to unknown users in the current directory

➜ find . -nouser

24. List files or directories belonging to an unknown group in the current directory

➜ find . -nogroup

linux find xargs

25. Find and delete all *.log files in the current directory

➜ find . -name "*.log" -type f  | xargs rm

26. Find all *.log files in the current directory and search for files containing a certain keyword

➜ find . -name "*.log" -type f  | xargs grep "keyword"

27. Find and delete files with size 0 in the current directory

➜ find . -size 0 | xargs rm

linux find exec

28. Find and delete all *.log files in the current directory

➜ find . -name "*.log" -type f -print -exec rm {} \;

29. Find all *.log files in the current directory and search for files containing a certain keyword

➜ find . -name "*.log" -type f -print -exec grep "keyword" {} \;

30. Delete nginx logs older than 5 days

➜ find /data/nginx/log/ -ctime +5 -exec rm -f {} \;

31. In the current directory, find and delete files larger than 100M

➜ find . -type f -size +100M -exec rm -f {} \;

32. In the current directory, find files and directories larger than 100M and smaller than 500M

➜ find . -size +100M -size -500M

linux find other examples

33. Find all hidden files and directories in the current directory

➜ find . -name ".*"

34. Search for files starting with “keyword” in the current directory, the query depth is up to 3 levels

➜  find . -name "keyword*" -maxdepth 3

35. Search for files starting with “keyword” in the current directory, starting from the second level

➜ find . -name "keyword*" -mindepth 2

36. Find all files in the current directory, but exclude the test directory.

➜ find . -path ./test -prune -o -type f

37. Find all files in the current directory, but exclude the test directory and the opt directory.

➜ find . -path ./test -prune -o -path ./opt -prune -o -type f 

38. Find all files in the current directory, but exclude the test directory and the opt directory, but the owner is ylspirit

➜ find . -path ./test -prune -o -path ./opt -prune -o -type f -a -user ylspirit

linux find logical operators

find logical operation symbol

     -a         and (by default)
     -o         or
     -not |!      non

39. Find files larger than 50M in the current directory, but exclude files whose names end with “deb” and “vmdk

➜  find . -type f -size +50M ! -name "*deb" ! -name "*vmdk"

# OR

➜  find . -type f -size +50M ! -name "*deb" -a ! -name "*vmdk"

40. Find the “.log” and “.txt” files in the current directory, but exclude files with the “php” keyword in their names

➜ find . -name "*.log" -a -name "*.txt" -a -not -name "*.php"

Add a Comment

Your email address will not be published.