Find Command in Unix ! Unix Find Command Examples

Find Command in Unix :

   In my previous articles i have given the brief idea about different unix commands with real life examples.In this article i would like to give you information about the Find Command in Unix with its examples. I would like to explain the working of Find command in Unix and how it is used.I would also like to give you different option of Find Command in Unix.The most basic use of Find Command is to search the file from Unix directory hierarchy,The Find command is one of the most important as well as most used command in unix.Find Command in unix not only used to find the specific file from the directory structure but also it uses to find the files with specific criteria like-

file permissions,date,users,usergroups,size e.t.c.

Find Command in Unix Syntax and Options :

In this section i would like to give you the find command syntax with its main options. This section will give the basic understanding of Find command.

Syntax :

find[The Point to start Searching from][Options] [path…] [expression];

 

The basic syntax of Find command in Unix specifies above.

There are following important Options of Find Command :

  1 : -exec CMD:

The file being searched which meets the above criteria and returns 0 for as its exit status for successful command execution.

2: -ok CMD :

It works same as -exec except the user is prompted first.

3:-inum N :

This option used to search for the file with inode of number ‘N’.

4: -links N :

This option is used to search for files with ‘N’ links.

5:-name demo :

This option is used to search for files that are specified by ‘demo’.

6.-newer file :

This option is used to search for files that were modified/created after ‘file’.

7.-perm octal :

This option is used to search for the file if permission is ‘octal’.

8.-print :

This option is used to display the path name of the files found by using the rest of the criteria.

9.-empty :

This option is used to search for empty files and directories.

10.-size +N/-N :

This option is used to 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.

11.-user name :

This option is used to search for files owned by user name or ID ‘name’.

12.\(expr \) :

This option is used to check for True if ‘expr’ is true; used for grouping criteria combined with OR or AND.

13.! expr :

This option is used to return True if ‘expr’ is false.

Find command Examples :

In this section I would like to give you multiple examples of Find command in Unix.These examples are used in day to day life in industry for so many purpose.

1. Find Files Using Name in Current Directory

Find all the files whose name is complexsql.txt in a current working directory.

# find . -name complexsql.txt

./complexsql.txt

2. Find Files Under Home Directory

Find all the files under /home directory with name complexsql.txt.

# find /home -name complexsql.txt

/home/complexsql.txt

3. Find Files Using Name and Ignoring Case

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

# find /home -iname complexsql.txt

./complexsql.txt
./complexsql.txt

4. Find Directories Using Name

Find all directories whose name is complexsql in / directory.

# find / -type d -name complexsql

/complexsql

5. Find java Files Using Name

Find all java files whose name is complexsql.java in a current working directory.

# find . -type f -name complexsql.java

./complexsql.java

6. Find all java Files in Directory

Find all java files in a directory.

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

./complexsql.java
./login.java
./index.java

7. Find Files With 777 Permissions

Find all the files whose permissions are 777.

# find . -type f -perm 0777 -print

8. Find Files Without 777 Permissions

Find all the files without permission 777.

# find / -type f ! -perm 777

9. Find SGID Files with 644 Permissions

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

# find / -perm 2644

10. Find Sticky Bit Files with 551 Permissions

Find all the Sticky Bit set files whose permission are 551.

# find / -perm 1551

11. Find SUID Files

Find all SUID set files.

# find / -perm /u=s

12. Find SGID Files

Find all SGID set files.

# find / -perm /g=s

13. Find Read Only Files

Find all Read Only files.

# find / -perm /u=r

14. Find Executable Files

Find all Executable files.

# find / -perm /a=x

15. Find Files with 777 Permissions and Chmod to 644

Find all 777 permission files and use chmod command to set permissions to 644.

# find / -type f -perm 0777 -print -exec chmod 644 {} \;

16. Find Directories with 777 Permissions and Chmod to 755

Find all 777 permission directories and use 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 complexsql.txtand remove it.

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

18. Find and remove Multiple File

To find and remove multiple files such as .mp3 or .txt, then use.

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

OR

# find . -type f -name "*.mp3" -exec rm -f {} \;

19. Find all Empty Files

To find all empty files under certain path.

# find /tmp -type f -empty

20. Find all Empty Directories

To file all empty directories under certain path.

# find /tmp -type d -empty

21. File all Hidden Files

To find all hidden files, use below command.

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

22. Find Single File Based on User

To find all or single file called Complexsql.txt under / root directory of owner root.

# find / -user root -name complexsql.txt

23. Find all Files Based on User

To find all files that belongs to user complexsql under /home directory.

# find /home -user complexsql

24. Find all Files Based on Group

To find all files that belongs to group tester under /home directory.

# find /home -group tester

25. Find Particular Files of User

To find all .txt files of user Complexsql under  /home directory.

# find /home -user complexsql -iname "*.txt"

26. Find Last 25 Days Modified Files

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

# find / -mtime 25

27. Find Last 150 Days Accessed Files

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

# find / -atime 150

28. Find Last 50-100 Days Modified Files

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

# find / -mtime +50 –mtime -100

29. Find Changed Files in Last 4 Hour

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

# find / -cmin -240

30. Find Modified Files in Last 1/2 Hour

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

# find / -mmin -30

31. Find Accessed Files in Last 45 min

To find all the files which are accessed in last 45 min.

# find / -amin -45

32. Find 25MB Files

To find all 25MB files, use.

# find / -size 25M

33. Find Size between 50MB – 100MB

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

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

34. Find and Delete 100MB Files

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

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

35. Find Specific Files and Delete

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

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

These are some most important examples of Find command in Unix. I hope you like this article on Find command in Unix. If you like this article on Find command in Unix or If you have any suggestions kindly comment in to comment Section.

Leave a Reply