How to Search for Files and Directories using "find" Command in Linux

You can use the find command to locate files or directories in the directory hierarchy. The find command searches using criteria, such as file name, size, owner, modification time, and type.

Using the find Command

The find command recursively descends the directory tree in the pathname list, looking for the files that match the search criteria. As the find command locates the files that match those criteria, the path to each file is displayed on the screen.

The syntax for the find command is:

$ find pathnames expressions actions

The table below shows the pathname, expression, and action arguments for the find command.

Argument Definition
pathname The absolute or relative path where the search originates.
expression The search criteria specified by one or more options. Specifying multiple options causes the find command to use the boolean operator and, so all listed expressions must be verified as true.
action The action required after the files have been located. The default action is to print all path names matching the criteria to the screen.

The table below describes some of the expressions that you can use with the find command.

Expression Definition
-name filename Finds files matching the specified filename. Metacharacters are acceptable if placed inside " “.
-size [+ -] n
-atime [+ -] n
-mtime [+ -] n
-user loginID Finds all files that are owned by the loginID name.
-type Finds a file type, for example, f (file) or d (directory).
-perm Finds files that have certain access permission bits.

The table below describes the action arguments for the find command:

Action Definition
-exec command {} \; Runs the specified command on each file located. A set of braces, {}, delimits where the file name is passed to the command from the preceding expressions. A space, backslash, and semicolon ( \;) delimits the end of the command. There must be a space before the backslash (\).
-ok command {} \; Requires confirmation before the find command applies the command to each file located. This is the interactive form of the -exec command.
-print Instructs the find command to print the current pathname to the terminal screen. This is the default.
-ls Displays the current pathname and associated statistics, such as the inode number, the size in kilobytes, protection mode, the number of hard links, and the user.

To search for a file called dante starting in your home directory, perform the command:

$ find ~ -name dante
/export/home/user1/dante
$

To search from your home directory, looking for files or directories called removefile and asking before deleting any matches to the pattern, perform the command:

$ touch removefile
$ find ~ -name file1 -OK rm {} \;
? y
$ ls removefile
$

To search from your home directory, looking for files or directories called removefile and deleting any matches to the pattern, perform the command:

$ touch removefile
$ find ~ -name core -exec rm {} \;
? y
$ ls removefile
$

To look for all files that have not been modified in the last two days starting in the current directory, perform the command:

$ find . -mtime +2

To find files larger than 10 blocks (512-byte blocks) starting in your home directory, perform the command:

$ find ~ -size +10
/export/home/user1/.sh_history
/export/home/user1/dir1/coffees/beans
/export/home/user1/tutor.vi

NOTE: Your output will vary depending on the activity performed in your home directory.