find : walk a file hierarchy
find command is useful in searching files and directories recursively. It supports
filtering of files and directories based on meta-data, names, user, group, permissions and
also allows to perform operations on the filtered result.
Below are few best questions on find
Q. List all the files and directories in current and sub-directories?
find .
Q. Find particular file/s in /home directory recursively?
find /home/ -type f -name [fileName]
Q. Find particular file irrespective of lower or upper case letter?
find . -type f -iname [fileName]
Q. Find particular directories in current and sub-directories?
find . -type d -name [DirectoryName]
Q. List all the files and directories in current and 1 level sub-directory?
find . -maxdepth 2
Q. List all the files and directories in 2 level sub-directory and all their sub-directories?
find . -mindepth 2
Q. Find all the files which ends with "py"?
find . -type f -name "*py"
Q. List all the files and directory which has full permissions(777)?
find . -perm 0777
Q. List all the files and directory which does not have full permissions(777)?
find . ! -perm 0777
Q. Find all the empty files and directories?
find . -empty
Q. Find all the empty directories and print their long listing?
find . -type d -empty -exec ls -lad {} \;
Q. Find all the empty files and remove/delete them?
find . -type f -empty -exec rm -f {} \;
stat [fileName or DirectoryName]
Access - the last time the file was read.
Modify - the last time the file was modified (content has been modified).
Change - the last time meta data of the file was changed (e.g. permissions, ownership etc.)
Q. Find all the files which are modified in last 30mins?
find . -type f -mmin -30
Q. Find all the directories which were changed before 30mins?
find . -type d -cmin +30
Q. Find all the files which are accessed before 30mins till 1 hour?
If current time is 4pm, find files which were accessed between 3pm to 3:30pm.
find . -type f -amin +30 -amin -60
Q. Find all the files which were changed exactly at 30th min from now?
find . -type f -cmin 30
Q. Find all the files which are modified in the last 10days?
find . -type f -mtime -10
Q. Find all the directories which were accessed before 10days?
find . -type d -atime +10
Q. Find all the files which were changed before 5th day till 10th day?
If current date is 20th May, find files which were changed between 11th to 15th May.
find . -type f -ctime +5 -ctime -10
Q. Find all the files/directories which were modified exactly at 5th day from now?
find . -mtime 5
Q. Find all the files having size less then 6MB?
find . -type f -size -6M
Q. Find all the files having size more then 6KB?
find . -type f -size +6k
Q. Find all the files owned by root user?
find . -type f -user root
Q. Find all the files having home as group?
find . -type f -group home
Q. Find all the files ends with .log and rename it with .log.bkp?
find . -type f -name "*.log" -exec mv {} {}.bkp \;