This is just a quick reference page for using find to do basic things.
Find is a pretty powerful tool that accepts a bunch of options for narrowing down your search. Some basic examples of stuff you can do are below.
Find a specific file type and extension older than 300 days and remove them
This will find files:
– Older than 300 days
– Is a file
– Match *.jpg
– Will not go into sub directories
This also works for those pesky directories that have millions of files.
First, always confirm the command will work before blindly removing files:
[[email protected] ~]# cd /path/to/directory [[email protected] ~]# find . -maxdepth 1 -type f -name '*.jpg' -mtime +300 | xargs ls -al
Once you verified that the files displayed are the ones you want removed, remove them by running:
[[email protected] ~]# cd /path/to/directory [[email protected] ~]# find . -maxdepth 1 -type f -name '*.jpg' -mtime +300 | xargs rm -f
Find files with 777 permissions
This will find all files that have 777 permissions:
[[email protected] ~]# cd /path/to/directory [[email protected] ~]# find . -type f -perm 0777 -print
This will find all files that do NOT have 777 permissions
[[email protected] ~]# cd /path/to/directory [[email protected] ~]# find / -type f ! -perm 777
Find Files with 777 Permissions and change to 644
Use caution with this, this is generally not smart to run blindly as it will go into subdirectories unless you set maxdepth.
[[email protected] ~]# cd /path/to/directory [[email protected] ~]# find . -type f -perm 0777 -print -exec chmod 644 {} \;
Find Directories with 777 Permissions and change to 755
Use caution with this, this is generally not smart to run blindly as it will go into subdirectories unless you set maxdepth.
[[email protected] ~]# cd /path/to/directory [root@web01 ~]# find . -type d -perm 777 -print -exec chmod 755 {} \;
Find empty directories
[[email protected] ~]# cd /path/to/directory [[email protected] ~]# find /tmp -type d -empty
Find all hidden files within a directory
[[email protected] ~]# find /path/to/directory -type f -name ".*"
Find files owned by user or group
[[email protected] ~]# cd /path/to/directory [[email protected] ~]# find /var/www -user apache [[email protected] ~]# find /var/www -group apache
Find files that were modified in the last 30 days
[[email protected] ~]# find / -mtime 30
Find files that were modified in the last hour
[[email protected] ~]# find / -mmin -60
Find files that were changed within the last hour
Note, this one is specified in minutes only!
[[email protected] ~]# find / -cmin -60
Find files that were accessed in the last 5 days
[[email protected] ~]# find / -atime 5
Find files that were accessed within the last hour
Note, this one is specified in minutes only!
[[email protected] ~]# find / -amin -60
Count files per directory with find
This one is useful when you need to find the top 10 directories that contain the most amount of files.
[[email protected] ~]# vim count-files-per-directory.sh #!/bin/bash if [ $# -ne 1 ];then echo "Usage: `basename $0` DIRECTORY" exit 1 fi echo "Please wait..." find "$@" -type d -print0 2>/dev/null | while IFS= read -r -d '' file; do echo -e `ls -A "$file" 2>/dev/null | wc -l` "files in:\t $file" done | sort -nr | head | awk '{print NR".", "\t", $0}' exit 0
Now run it against the / directory:
[[email protected] ~]# bash count-files-per-directory.sh / Please wait... 1. 768 files in: /usr/share/man/man1 2. 631 files in: /usr/lib64/python2.6 3. 575 files in: /usr/share/locale 4. 566 files in: /usr/share/vim/vim74/syntax 5. 496 files in: /usr/bin 6. 487 files in: /usr/share/man/man8 7. 393 files in: /usr/share/perl5/unicore/lib/gc_sc 8. 380 files in: /usr/include/linux 9. 354 files in: /usr/lib64/python2.6/encodings 10. 334 files in: /usr/share/man/man3
Or if you only need to run the search in a specific directory:
[[email protected] ~]# bash count-files-per-directory.sh /usr/share/man Please wait... 1. 768 files in: /usr/share/man/man1 2. 487 files in: /usr/share/man/man8 3. 334 files in: /usr/share/man/man3 4. 124 files in: /usr/share/man/man5 5. 49 files in: /usr/share/man 6. 35 files in: /usr/share/man/ru/man8 7. 31 files in: /usr/share/man/man7 8. 27 files in: /usr/share/man/fr/man8 9. 25 files in: /usr/share/man/de/man8 10. 22 files in: /usr/share/man/ja/man8