find command with its examples

It is awesome utility provided in Linux/UNIX environment to find any files residing under the OS. We can search for any files, with almost all the attributes a file has attached to it. It comes from “findutils-x.x.x” package, we have covered almost all the find options. This command is generaly found under the path “/bin/”.

Examples:

1. To find the file with name, within specified directory

$ find / -name file.txt
$ find . -name file.txt

2. To find the file of specified permissions

$ find / -perm 775
$ find / -perm a+x
$ find . -perm 775           (within current directory)

3. To have at least one write bit set (specific permission)

$ find / -perm /a+w
$ find . -perm /a+w         (within current directory)

4. To find a file which does not have any executable bit set

$ find / ! -perm /111
$ find / ! -perm /a+x
$ find . ! -perm /a+x        (within current directory)

5. To search for a world readable file, at least one write bit and not executable by anybody

$ find . -perm -a+r -perm /a+w ! -perm /a+x

6. To avoid following the symbolic links

$ find -p / -name file

7. To follow the symbolic links

$ find -p / -name file

8. Not to follow symbolic links, except while processing the command line arguments.

$ find -H / -name file

9. To find the file with specified depth in the directory structure

$ find -d 2 / -name file

10. To specify the descend level

$ find -maxdepth 2 / -name file

11. To specify the minimum depth level

$ find -mindepth 4 / -name file

12. To process each directory’s contents before the directory itself

$ find -depth / -name file

Tests:

13. To get the files access n minutes ago

$ find / -amin 10

14. To find files which were access recently than specified file

$ find / -anewer file.txt

15. To get file’s whose status was last changed n minutes ago

$ find / -cmin 10

16. To get file’s whose status was last changed more recently than file was modified.

$ find -cnewer file.txt

17. To get files whose status was last changed n*24 hours ago

$ find / -ctime 10

18. To get file which is empty or which is either a regular file or a directory

$ find / -empty

19. To get the executable files

$ find / -executable

20. To get the files from particular file system types ()

$ find / -fstype type file

21. To get files whose GID with specified

$ find / -gid 100

22. To get file which belongs to particular group

$ find / -group SUPPORT

23. To find the file which is a link having specified content

$ find / -lname PATTERN

24.To find the file which is a link having specified content with case insensitive

$ find / -ilname PATTERN

25. To find a file with specified name with case insensitive

$ find / -iname file.txt

26. To to find a file with inode number

$ find / -num 11213

27. To find a file who has specified number of links

$ find / -linkns 10

28. To find a file which was modified specified minutes ago

$ find / -mmin 20

29. To find a file which was modified last specified hours before

$ find / -mtime 24

30. To find a file with specified name

$ find / -name file.txt

31. To find a file which was modified recently that specified one

$ find / -name

32. To find a files whose group ID does not correspond to specified files one

$ find / -nogroup 1231

33. To find a files whose user ID does not correspond to specified files one

$ find / -nouser 501

34. To find a file whose name matches shell pattern pattern

$ find / -path “/mike*”

35. To find a file whose permissions are exactly as given

$ find / -perm 777

36. To find a file whose permissions are exactly as given

$ find / -perm -u+w,g+w

37. To find a file whose permissions would be any of the specified

$ find / -perm /u+w,g+w

38. To find files which are readable

$ find / -readable

39. To find file whose names matches regular expression

$ find / -regex z324x

40. To find file which refers to same inode as name

$ find / -samefile file

41. To find files with specified size

$ find / -size 10b (512 Byte blocks)
$ find / -size 10c (Bytes)
$ find / -size 10w (Two Byte Words)
$ find / -size 10k (KiloBytes)
$ find / -size 10M (Mega Bytes)
$ find / -size 10G (Giga Bytes)

42. To find a file with specified type

$ find / -type b (Block Special)
$ find / -type c                    (Character Special)
$ find / -type d                    (Directory)
$ find / -type p                   (Named Pipe)
$ find / -type f                    (Regular File)
$ find / -type l                    (Symbolic Link)
$ find / -type s                    (Socket)
$ find / -type D            (Door)

43. To find files whose numeric ID is as specified

$ find / -uid 501

44. To find file which was accessed last n days ago

$ find / -used 10

45. To find a file which is owned by a specified user

$ find / -user mike

46. To find files which are writable

$ find / -writable

47. To find files with specified SELinux context

$ find / -context

48. To find files and delete them if found

$ find / -perm 777 -delete

49. To find files and execute specified command

$ find / -perm 777 -exec rm -rf

50. To find files and write their names to specified file

$ find / -perm 777 -fls file.txt

51. To print the file in ls format

$ find / -perm 777 -ls

52. To find files and run command first asking user to run

$ find / -perm 777 -ok rm -rf

53. To find files and print the full file names, followed by new line

$ find / -perm 777 -print

54. To find files and print the full file names, followed by null character

$ find / -perm 777 -print0

55. To find files and quit if specified

$ find / -name 777 -print -quit

56. It reorders tests to speed up execution.

To optimization level 1          

$ find -0level 0

Default  optimization  level

$ find -0level 1

To perform any -type or -xtype tests are performed after any tests based only on the names of files

$ find -0level 2

To the full cost-based query optimizer is enabled

$ find -0level 3

Debug options

To get the debug option help

$ find -D help

To Show the expression tree in its original and optimized form

$ find -D tree / -name file

To print messages as files are examined with the stat and lstat system calls

$ find -D stat / -name file

To prints diagnostic information relating to the optimization to the expression tree

$ find -D opt / -name file

To Prints a summary indicating how often each predicate succeeded or failed.

$ find -D rates / -name file

To get the find command version

$ find -version
$ find –version

To get the find command help

$ find -help
$ find –help

Related Commands: locate, locatedb, updatedb, xargs, chmod, fnmatch, regex, stat, lstat, ls, printf, strftime, ctime

One thought on “find command with its examples

Leave a Reply

Your email address will not be published. Required fields are marked *