Searching for Files and Directories Using the find command

The find command recursively descends the directory tree in the path name 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 shows the pathname, expression, and action arguments for the find command.

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 describes some of the expressions that you can use with the find command.

Expressions for the find Command
Expression Definition
-name filename
Finds files matching the specified filename. Metacharacters are acceptable if placed inside " ".
-size [+|-]n
Finds files that are larger than +n, smaller than -n, or exactly n. The n represents 512-byte blocks.
-atime [+|-]n
Finds files that have been accessed more than +n days, less than -n days, or exactly n days.
-mtime [+|-]n
Finds files that have been modified more than +n days ago, less than -n days ago, or exactly n days ago.
-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 describes the action arguments for the find command.

Actions 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 path name to the terminal screen. This is the default.
-ls
Displays the current path name 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 {} ;
< rm ... /export/home/user1/removefile >? 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 {} ;
< rm ... /export/home/user1/removefile >? 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
<output will vary on each system>

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
<example output from command, output will vary on each system>
Note: Your output will vary depending on the activity performed in your home directory.

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA *