Some more examples of find command
Here I am posting down a some of the way I use find in Unix or Linux box frequently, I trust this would help somebody who is new with in UNIX find command or any engineer who has begun working on UNIX environment.
See Also:
Filtering by Time and Size
We add a suffix on the end of our value that specifies how we are counting. These are some popular options:
Size:
Find all 60MB files, use following command.
# find / -size 60M
If you want to find Size between 60MB – 150MB, use following command
# find / -size +60M -size -150M
Time:
Linux stores time data about access times, modification times, and change times.
Some time we want to find those files which are modified in last 15 days.
# find / -mtime 15
Find those files which are accessed in last 14 days.
# find / -atime 14
To find all the files which are altered over 60 days back and under 120 days.
# find / -mtime +60 –mtime -120
To find all the files which are changed in last 1 hour, use following command.
# find / -cmin -60
To find all the files which are modified in last 1 hour.
# find / -mmin -60
To find all the files which are accessed in last 1 hour.
# find / -amin -60
Finding by Owner and Permissions
Owner:
You can also search for files by the file user or group. You do this by using the “- user” and “-group” parameters separately.
Find all files called technical.txt under “/” directory of owner Dennis.
# find / -user dennis -name technical.txt
Find all files that belongs to user Dennis under /home directory, use following command:
# find /home -user dennis
Same as If you want to find all files that belongs to group Administrator under /home directory, use following command:
# find /home -group developer
To find all .txt files of user Tecmint under /home directory.
# find /home -user tecmint -iname "*.txt"
Permissions:
We can also search for files with specific permissions.
Find all the files whose permissions are 644.
# find . -type f -perm 0644
Find all files whose permission is not 644.
# find / -type f ! -perm 777
Find all the SGID bit files whose permissions set to 755.
# find / -perm 2755
Same as SGID if you want to find Sticky Bit Files with 644 Permissions.
# find / -perm 1644
Find all the SGID bit files.
# find / -perm /u=s
Same as SGID if you want to find Sticky Bit Files
# find / -perm /g+s
Find all Read Only files.
# find / -perm /u=r
Filtering by Depth
Some times we search a file but don’t want to go more than 2 or 3 levels down in the sub directories. This is done using the maxdepth option.
# find ./ -maxdepth 2 -name "*.php" Or # find ./ -maxdepth 3 -name "*.php"
Note: In the first example uses maxdepth of 2, which means it will not go lower than 2 level deep, either only in the current directory and same as on The second example uses maxdepth of 3, which means it will not go lower than 3 level deep, either only in the current directory.
Executing and Combining Find Commands
Some time you require to change all files permission who has 777 permission using single command.
# find / -type f -perm 0777 -exec chmod 644 {} \;
Same as for directory also.
# find / -type d -perm 777 -exec chmod 755 {} \;
Copy single file to multiple location.
# find dir1 dir2 dir3 -type d -exec cp index.php {} \;
To find a single file called technical.txt and remove it using single command.
# find . -type f -name "technical.txt" -exec rm -f {} \;
Same as If you want to find and remove multiple files, then use.
# find . -type f -name "*.txt" -exec rm -f {} \;
Find all files whose size is more that 100 MB and remove them using single command.
# find / -size +100M -exec rm -rf {} \;
If you want to delete any specify format files, use following commans.
# find / -type f -name *.mp4 -exec rm {} \;
Find all .mp4 files with more than 15MB and delete them using single command.
# find / -type f -name *.mp4 -size +15M -exec rm {} \;
Empty Files & Directories:
To file all empty files under certain path.
# find /tmp -type f -empty
Same as for directories.
# find /tmp -type d -empty
Enjoy it!