Using Linux Find Command

Lets take a look at few examples using Linux powerful find command. In general find command recursively searches directories and finds files that match specific criteria. This powerful tool can look for files based on name, inode number, modification time, file types and even size. Find command can also perform some action on found files. Lets look at few examples.

Find myfile.txt file by searching root directory and all sub directories.

find / -name myfile.txt

Find is case sensitive command so lets find same files with case-insensitive option

find / -iname myfile.txt

In this example we will limit depth of directory traversal. Find command will traverse down entire directory structure, in some cases in may be beneficial to limit this to one or two directories.

find /home -maxdepth 2 -name "myfile.txt"

We can also search for file based on size. In this example we will look for all files in home directory smaller then 10M

find /home -size -10M

To find files larger then 10M

find /home -size +10M

In this example we will find configuration files modified more then 5 days ago

find /etc -mtime +5

In this example we will see files modified exactly 5 days ago

find /etc -mtime 5

Below example will show files modified between 5 and 10 days ago

find /home -mtime +5 –mtime -10

In this example we will find files changed in last 10 min

find /home -cmin -10

In this example we will search for all files belonging to group apache

find /var/www/html/ -group apache

It is also possible to combine search using -o switch

find -name '*.txt' -o -name '*.bin'

Find can also be used to perform action on found files. In this example we will remove all .html files from web directory

find /var/www/html -type f -name "*.html" -exec rm -f {} \;

Find command has much more options than we described. In order to see extensive option list look over man pages