The find command lists all of the files within a directory and its subdirectories that match a set of conditions. This command is most commonly used to find all of the files that have a certain name.

Options

The general form of the command is:

find (starting directory) (matching criteria and actions)

The find command will begin looking in the starting directory you specify and proceed to search through all accessible subdirectories. You may specify more than one starting directory for searching.

You have several options for matching criteria:

-atime n

File was accessed n days ago

-mtime n

File was modified n days ago

-size n

File is n blocks big (a block is 512 bytes)

-type c

Specifies file type: f=plain text, d=directory

-fstype typ

Specifies file system type: 4.2 or nfs

-name nam

The filename is nam

-user usr

The file’s owner is usr

-group grp

The file’s group owner is grp

-perm p

The file’s access mode is p (where p is an integer)

You can use  +  (plus) and  -  (minus) modifiers with the atime, mtime, and size criteria to increase their usefulness, for example:

-mtime +7

Matches files modified more than seven days ago

-atime -2

Matches files accessed less than two days ago

-size +100

Matches files larger than 100 blocks (50KB)

By default, multiple options are joined by "and". You may specify "or" with the  -o  flag and the use of grouped parentheses. To match all files modified more than 7 days ago and accessed more than 30 days ago, use:

\( -mtime +7 -o -atime +30 \)

You may specify "not" with an exclamation point. To match all files ending in .txt except the file notme.txt, use:

\! -name notme.txt -name \*.txt

You can specify the following actions for the list of files that the find command locates:

-print

Display pathnames of matching files.

-exec cmd

Execute command cmd on a file.

-ok cmd

Prompt before executing the command cmd on a file.

-mount

(System V) Restrict to file system of starting directory.

-xdev

(BSD) Restrict to file system of starting directory.

-prune

(BSD) Don’t descend into subdirectories.

Executed commands must end with  \;  (a backslash and semi-colon) and may use  {}  (curly braces) as a placeholder for each file that the find command locates. For example, for a long listing of each file found, use:

-exec ls -l {} \;

Matching criteria and actions may appear in any order and are evaluated from left to right.

Full examples

  • To find and report all C language source code files starting at the current directory, enter: find . -name \*.c -print
  • To report all files starting in the directories /mydir1 and /mydir2 larger than 2000 blocks (about 1000KB) and that have not been accessed in over 30 days, enter: find /mydir1 /mydir2 -size +2000 -atime +30 -print
  • To remove (with prompting) all files starting in the /mydir directory that have not been accessed in over 100 days, enter: find /mydir -atime +100 -ok rm {} \;
  • To show a long listing starting in /mydir of files not modified in over 20 days or not accessed in over 40 days, enter: find /mydir \(-mtime +20 -o -atime +40\) -exec ls -l {} \;
  • To list and remove all regular files named core starting in the directory /prog that are larger than 500KB, enter: find /prog -type f -size +1000 -print -name core -exec rm {} \;

To find all of the files named myfile.txt in your current directory and all of its subdirectories, enter:

find . -name myfile.txt -print

To look in your current directory and its subdirectories for all of the files that end in the extension .txt , enter:

find . -name "*.txt" -print

In these examples, the  .  (period) represents your current directory. It can be replaced by the full pathname of another directory to search. For instance, to search for files named myfile.txt in the directory /home/user/myusername and its subdirectories, enter:

find /home/user/myusername/ -name myfile.txt -print

On some systems, omitting the final  /  (slash) after the directory name can cause find to fail to return any results.

As a shortcut for searching in your home directory, enter:

find "$HOME/" -name myfile.txt –print

Related Posts

  1. UNIX Command Tips for Developers
  2. Basic UNIX Commands – Part 1
  3. Basic Unix Commands – Part 2
  4. Commands Options and Modes in UNIX VI Editor
  5. Error: SQL1263N Archive file name not valid

Tags: , , ,

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>