Searching Files

The "find" command enables you to find a file based on its name, type,

owner, creation time, size, group, and other criteria. The find command

looks as follows:

find directory -option criteria

Searching by Name

To locate a file by its name, use the -name option. For example, the

following command locates a file called report in the forecast

directory:

$find forecast -name report

The -print option instructs find to display the file(s) it locates and

their paths on the standard output. For example:

$find summer -name july_report.dat -print

summer/july_report.dat

If you specify an absolute path in the command, find will display

absolute paths, too:

$find /home/jane/summer -name july_report.dat -print

/home/jane/summer/july_report.dat

The special characters . and .. represent your current working directory

and its parent directory, respectively. To locate a file called

invoices.txt in the current directory and display the results on the

screen, type:

$find . -name invoices.txt -print

./invoices.txt

Searching by Type

The -type option enables you to locate a file according to its type.

-type takes a single character that specifies the file type as follows:

Symbol Meaning

b block device file

c character device file

d directory file

f regular file

p named pipe

l symbolic link

For example, to find the directory customers located under the /home

directory, type:

$find /home -name customers -type d -print

/home/letters/customers

Miscellaneous Criteria

You can locate files according to their size by using the -size option.

For example, to find a file whose size is 3 blocks, type:

$find customers -size 3

To specify a file's size in bytes rather than blocks, append the letter

'c' to the size value:

$find /home -size 2048c

To locate a file by its group, use the -group option:

$find /home -name outline -group developers

To locate all the files created during the last 15 minutes in the

current directory, use the -mmin option like this:

$find -mmin +0 -mmin -15

To find files that have been modified within the last two days, type:

$find -mtime -2

Wildcards

You may use shell wildcards as a filename. The following command locates

all the files that have a .h extension under the directory sources:

$find sources -name '*.h'

You must quote the filename when using wildcards. Otherwise, the shell

would attempt to evaluate the filename before passing it to the command.

Insider: How the basic tech behind the Internet works
Join the discussion
Be the first to comment on this article. Our Commenting Policies