find command in Linux

find command in Linux

The find command in UNIX or Linux is one of the most important and frequently used command. It’s a command-line utility for walking a file hierarchy.

It can be used to find files and directories and perform subsequent operations on them.

It supports searching by file, folder, name, creation date, modification date, owner, and permissions.

Syntax:

$ find [where to start searching from]
 [expression determines what to find] [-options] [what to find]

 

Options :

  • -exec CMD: The file being searched which meets the above criteria and returns 0 for as its exit status for successful command execution.
  • -ok CMD : It works same as -exec except the user is prompted first.
  • -inum N : Search for files with inode number ‘N’.
  • -links N : Search for files with ‘N’ links.
  • -name demo : Search for files that are specified by ‘demo’.
  • -newer file : Search for files that were modified/created after ‘file’.
  • -perm octal : Search for the file if permission is ‘octal’.
  • -print : Display the path name of the files found by using the rest of the criteria.
  • -empty : Search for empty files and directories.
  • -size +N/-N : Search for files of ‘N’ blocks; ‘N’ followed by ‘c’can be used to measure size in characters; ‘+N’ means size > ‘N’ blocks and ‘-N’ means size < ‘N’ blocks.
  • -user name : Search for files owned by user name or ID ‘name’.
  • \(expr \) : True if ‘expr’ is true; used for grouping criteria combined with OR or AND.
  • ! expr : True if ‘expr’ is false.

Example:

1. Search a file with a specific name.

$ find ./GFG -name sample.txt

It will search for sample.txt in GFG directory.

2. Search a file with pattern.

$ find ./GFG -name *.txt

It will give all files which have ‘.txt’ at the end.

3. How to find and delete a file with confirmation.

$ find ./GFG -name sample.txt -exec rm -i {} \;

When this command is entered, a prompt will come for confirmation, if you want to delete sample.txt or not. if you enter ‘Y/y’ it will delete the file.

4. Search for empty files and directories.

$ find ./GFG -empty

This command find all empty folders and files in the entered directory or sub-directories.

5. Search for file with entered permissions.

$ find ./GFG -perm 664

This command find all the files in the GFG directory or sub-directory with the given permissions.

6. Search text within multiple files.

$ find ./ -type f -name "*.txt" -exec grep 'Geek'  {} \;

This command print lines which have ‘Geek’ in them and ‘-type f’ specifies the input type is a file.

7. Find Files Using Name and Ignoring Case

Find all the files whose name is eduguru.txt and contains both capital and small letters in /home directory.

# find /home -iname eduguru.txt

8. Find Directories Using Name

Find all directories whose name is Eduguru in / directory.

# find / -type d -name Eduguru

9. Find PHP Files Using Name

Find all php files whose name is eduguru.php in a current working directory.

# find . -type f -name eduguru.php

10. Find all PHP Files in the Directory

Find all php files in a directory.

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

11. Find Files With 777 Permissions

Find all the files whose permissions are 777.

# find . -type f -perm 0777 -print

12. Find Files Without 777 Permissions

Find all the files without permission 777.

# find / -type f ! -perm 777

13. Find SGID Files with 644 Permissions

Find all the SGID bit files whose permissions are set to 644.

# find / -perm 2644

14. Find Read-Only Files

Find all Read-Only files.

# find / -perm /u=r

15. Find Executable Files

Find all Executable files.

# find / -perm /a=x

16. Find Directories with 777 Permissions and Chmod to 755

Find all 777 permission directories and use the chmod command to set permissions to 755.

# find / -type d -perm 777 -print -exec chmod 755 {} \;

17. Find and remove single File

To find a single file called eduguru.txt and remove it.

# find . -type f -name "eduguru.txt" -exec rm -f {} \;

18. File all Hidden Files

To find all hidden files, use the below command.

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

19. Find all Files Based on User

To find all files that belong to user Eduguru under /home directory.

# find /home -user eduguru

20. Find all Files Based on Group

To find all files that belong to the group Developer under /home directory.

# find /home -group developer

21. Find Last 50 Days Modified Files

To find all the files which are modified 50 days back.

# find / -mtime 50

22. Find Last 50 Days Accessed Files

To find all the files which are accessed 50 days back.

# find / -atime 50

23. Find Last 50-100 Days Modified Files

To find all the files which are modified more than 50 days back and less than 100 days.

# find / -mtime +50 –mtime -100

24. Find Changed Files in Last 1 Hour

To find all the files which are changed in the last 1 hour.

# find / -cmin -60

25. Find Modified Files in Last 1 Hour

To find all the files which are modified in the last 1 hour.

# find / -mmin -60

27. Find Accessed Files in Last 1 Hour

To find all the files which are accessed in the last 1 hour.

# find / -amin -60

28. Find 50MB Files

To find all 50MB files, use.

# find / -size 50M

29. Find Size between 50MB – 100MB

To find all the files which are greater than 50MB and less than 100MB.

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

30. Find and Delete 100MB Files

To find all 100MB files and delete them using one single command.

# find / -type f -size +100M -exec rm -f {} \;

31. Find Specific Files and Delete

Find all .mp3 files with more than 10MB and delete them using one single command.

# find / -type f -name *.mp3 -size +10M -exec rm {} \;

Leave a Reply