Overview :
As the name suggests , find command is used to search files and directories in UNIX like operating system. Find command can search files & directories on the basis of their name,size ,type,permissions,access time,modify time and lot of other criteria.Some of the practical examples of find command is shown below :
Syntax : # find <start_at> conditions actions
According to above syntax find command searches the files on the disk from a given starting location, start_at , looking for the files & directories that matches a given conditions and then taking the given actions , such as printing out the file names.
Example:1 How to find files on the basis of their names
Syntax : # find <location-where-To-Find> <options> < File Name>
# find / -iname interfaces
In above command '-iname' option is used for case insensitive , means it will search all the files that matches interfaces.
Example:2 Find all the files & directories which have 777 permissions
# find / -perm 777
Example:3 Find all the regular files in /root and have permissions 644
# find /root -type f perm 644
Similarly find the all the files which are directories
# find /root -type d
Find all the files in /home folder which have the symbolic links
# find /home -type l
Find all the socket files in / folder
# find / -type s
Example:4 Find files based on their size
# find /home -size +1G
above command will find all the files in /home which have the size more than 1GB.
Find all the files which have exact 1GB size.
# find /home -size 1G
Find all the files which have size less than 1GB in /home folder.
# find /home -size -1G
Example:5 Execute Command after find operations
Syntax # find <where-to-search> <options> -exec <command> {} \;
Find all the files which have 644 permissions and do the listing using ls -l command
# find / -perm 644 -exec ls -l {} \;
Find all the files have ".mp3 extensions and remove it"
# find / -name "*.mp3" -exec rm -rf {} \;
Note: We ca also use ok instead exec option but in case of ok , it will ask the user first. If the user agrees, then command will execute ,Otherwise just return false.
Example:6 Search files to a specific directories level using maxdepth & mindepth
# find /root -maxdepth 2 -type f -perm 644
Above command will search all the regular files under / root folder and one level down. (i.e /root – level 1, and one sub-directory – level 2) which has 644 permissions.
Find all the regular files where maxdepth is 4 and mindepth is 2 and has permissions 644
# find / -maxdepth 4 -mindepth 2 -type f -perm 644
Example:7 Search files and directories using their inode numbers
# find -inum <inode-numbers>
Example:8 Find the files based on their modified & access time
Find the file under the directory /home that was modified more than 60 days ago.
# find /home -mtime +60
Find the files in /var/spool folder that have been accessed at least two days ago
# find /var/spool -atime +1
Example:9 Find all the empty files & directories in / folder
# find / -empty
Example:10 Print and xargs options in find Command
# find / -type f -perm 644 -print0 | xargs -0 ls -l
Above command will find all the regular files which has 644 permissions and prints all the match files and gives input to xargs.