Useful find Commands

The find command is very verstile, and I use it all the time. Here are the parameters that I use most often:
-name <string> Finds files in specified directory whose name matches (case-sensitive) string
-iname <string> Finds files in specified directory whose name matches (case-insensitive) string
-exec Executes a command on the files found (see ls -l example below)
-perm <mode> Finds files in specified directory that match the permission mode
-size Finds files in specified directory
-print Finds the files and prints out the filename. This is useful in a find . -exec grep
-regex<pattern> Finds files in specified directory with name that matches regular expression pattern.
-atime <+-><n> Finds files in specified directory that were accessed less than, more than, or exactly n minutes ago
-amin <+-><n> Finds files in the specified directory that were accessed less than, more than, or exactly n minutes ago
-mmin <+-><n> Finds files in specified directory that were last modified less than, more than, or exactly n minutes ago
-mtime <+-><n> Finds files in specified directory that were last modified less than, more than, or exactly n*24 hours ago
-type <filetype> Find files in the specified directory of type:
b block (buffered) special
c character (unbuffered) special
d directory
p named pipe (FIFO)
f regular file
l symbolic link; this is never true if the -L option
or the -follow option is in effect, unless the
symbolic link is broken. If you want to search for
symbolic links when -L is in effect, use -xtype.
s socket

Examples:
Find files in current directory that end in .txt:
find . -name "*.txt"

Find files in current directory that were created within the last hour:
find . -ctime -60

Find files in current directory not owned by root:
find . \! -user root -print

Find files in current directory with 0777 permissions and chmod them to 644:
find . -type f -perm 0777 -print -exec chmod 644 {} \;

Find files in current directory with owner apache and change to www-data:
find . -user apache -exec chown www-data {} \;

Find all files in current directory that contain string and output their names:
find . -exec grep string {} \; -print

Find files in the current directory with ACLs set:
find . -type f -exec ls -l {} \; | grep -v "\+"

Find files older than 7 days matching string “vicki_test2 and move to new directory
mkdir -p /var/mitto/data/vicki_dir && find /var/mitto/data/ -regextype posix-extended -regex '^.*vicki_test2.*\.[^.]+$' -mtime +7 -exec mv {} /var/mitto/data/vicki_dir \;


About vicki

Welcome to the Sovereign Republic of Vickistan. I am the President here. Lucky me! No taxes or laws yet. Lucky you!
This entry was posted in Linux. Bookmark the permalink.