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.
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.
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.
Find all the files whose name is complexsql.txt in a current working directory.
# find . -name complexsql.txt ./complexsql.txt
Find all the files under /home directory with name complexsql.txt.
# find /home -name complexsql.txt /home/complexsql.txt
# find /home -iname complexsql.txt ./complexsql.txt ./complexsql.txt
Find all directories whose name is complexsql in / directory.
# find / -type d -name complexsql /complexsql
Find all java files whose name is complexsql.java in a current working directory.
# find . -type f -name complexsql.java ./complexsql.java
Find all java files in a directory.
# find . -type f -name "*.java" ./complexsql.java ./login.java ./index.java
Find all the files whose permissions are 777.
# find . -type f -perm 0777 -print
Find all the files without permission 777.
# find / -type f ! -perm 777
Find all the SGID bit files whose permissions set to 644.
# find / -perm 2644
Find all the Sticky Bit set files whose permission are 551.
# find / -perm 1551
Find all SUID set files.
# find / -perm /u=s
Find all SGID set files.
# find / -perm /g=s
Find all Read Only files.
# find / -perm /u=r
Find all Executable files.
# find / -perm /a=x
Find all 777 permission files and use chmod command to set permissions to 644.
# find / -type f -perm 0777 -print -exec chmod 644 {} \;
Find all 777 permission directories and use chmod command to set permissions to 755.
# find / -type d -perm 777 -print -exec chmod 755 {} \;
To find a single file called complexsql.txtand remove it.
# find . -type f -name "complexsql.txt" -exec rm -f {} \;
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 {} \;
To find all empty files under certain path.
# find /tmp -type f -empty
To file all empty directories under certain path.
# find /tmp -type d -empty
To find all hidden files, use below command.
# find /tmp -type f -name ".*"
To find all or single file called Complexsql.txt under / root directory of owner root.
# find / -user root -name complexsql.txt
To find all files that belongs to user complexsql under /home directory.
# find /home -user complexsql
To find all files that belongs to group tester under /home directory.
# find /home -group tester
To find all .txt files of user Complexsql under /home directory.
# find /home -user complexsql -iname "*.txt"
To find all the files which are modified 25 days back.
# find / -mtime 25
To find all the files which are accessed 150 days back.
# find / -atime 150
To find all the files which are modified more than 50 days back and less than 100days.
# find / -mtime +50 –mtime -100
To find all the files which are changed in last 4 hour.
# find / -cmin -240
To find all the files which are modified in last 1 hour.
# find / -mmin -30
To find all the files which are accessed in last 45 min.
# find / -amin -45
To find all 25MB files, use.
# find / -size 25M
To find all the files which are greater than 50MB and less than 100MB.
# find / -size +50M -size -100M
To find all 100MB files and delete them using one single command.
# find / -size +100M -exec rm -rf {} \;
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.
In my previous articles I have given the roles and responsibilities of L1,L2 and L3…
In my previous articles i have given the hierarchy of production support in real company…
In this article i would like to provide information about production support organization structure or…
In my previous article I have given roles for L1 and L2 support engineer with…
I have started this new series of how to become application support engineer. This article…
In this series we are starting with Roles and responsibilities of L1 support engineer .…