Introduction

In this tutorial, we are going to talk about find command that can help you find files and directories.

The find command.

The command we are going to cover is the find command.

find [path...] [expression]

You supply a path to find and a search expression, the find will return the results that match the path and expression. If you don't tell find what to look for it just returns all the files that are in your current directory and all the sub-directories below that directory.

Options

You can give find a search pattern by using the -name [pattern] , and it will return all the files and directories that match the pattern.

If you use -iname [pattern] that tells find to ignore case.

If you provide the -ls option to find, it will run ls -l , a long listing format display of the files and directories that it finds.

Options Meaning
-name pattern Find files and directories that match pattern.
-iname pattern Same as -name, but ignores case.
-ls Performs an ls on each of the found items.

You can search for files by modification time by using the -mtime option to find. You can also search for files based on size using -size. To find files in directories that were created after another file, you can give that file to find with -newer. If you want to execute a command against all the results that find returns, you can use the -exec option.

Options Meaning
-mtime days Find files that are days old.
-size num Find files that are of size num.
-newer file Find files that are newer than file.
-exec command {} \; Execute command against all the results.

Another widely used command to find files and directories is locate command. It is much faster than find, but does the lookup in the index, so it is not real time. The details about locate is covered locate command.

Examples

Example 1: run find without option

If you run find without any options, it just finds everything in your present working directory and below.

$ find
./directoryOne
./directoryOne/cat
./song.mp3
......

It is the same as find ., the dot represents this directory.

Example 2: look up a specified name
$ find /directoryOne -name MAKEDEV
/tmp/directoryOne/MAKEDEV

You can also run

$ find /directoryOne -iname makedev

It will ignore the case. So when use -name, it matches case, when use -iname, it ignores case.

Example 3: match anything

To look any file that end with 'v', you also need to use * to match anything.

$ find /directoryOne -name *v
Example 4: look up files with specified days old

To look up files between 10-13 days old in current directory

$ find . -mtime +10 -mtime -13
Example 5: use multiple options at the same time

To find file begin with 's' in current directory and display the details:

$ find . -name s* -ls
Example 6: with size option

To find a find over than 1 megabyte in current directory

$ find . -size +1M 
Example 7: with newer option

To find all directories in current directory that are newer than file.txt.

$ find . -type d -newer file.txt
Example 8: execute command

To execute file command against all the search results.

$ find . -exec file {} \;

The file command just tell what kind of file it is.

References & Resources