Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


find: -name Option

The -name option enables us to specify either an exact or partial filename for find to match. find checks for a match only in the filename and not in the directory portion of the pathname.

find / -name alpha -print

/tmp/alpha has a matching filename and would be displayed by this command. /reports/alpha/file2 would not be displayed because find ignores the directory portion of the pathname.

To specify a partial pathname, use filename substitution wildcards (refer to Chapter 8). For instance,

find / -name '*alpha*' -print

This displays all files that contain alpha anywhere within the filename. Here is some sample output:

/reports/1998/alpha
/reports/1998/alpha2
/reports/1998/old-alpha
/reports/1998/region2/alpha
/tmp/alpha
/usr/fredp/ralphadams

All the wildcards covered in Chapter 8 can be used:

* ? [characters] [!characters]

You must enclose the filename containing these wildcards within single quotes (see Chapter 9); otherwise, your find command does not always give you the desired results.

find: -type Option

The -type option enables us to specify the type of file to search for, as in this example:

find / -type d -print

-type d indicates directories, so only files that are directories are displayed. In this example, all directories in the whole system are displayed. Notice that no -name option has been given, so you display all directories regardless of their names. Table 18.2 lists other types that are available.

Table 18.2 Types Available for the find Command

Type Description

f Regular or normal file
d Directory
b Block special device file
c Character special device file (raw)
l Symbolic link
p Named pipe

find: -mtime, -atime, -ctime

The find -mtime option enables us to locate files that were last modified recently or have not been modified in a long time:

find / -mtime -5 -print

-mtime takes an integer argument that is measured in days. This find command locates files that were last modified fewer than five days ago. This is a useful option when you are sure you modified a file recently but can’t remember its name or directory.

Following -mtime you must specify an integer value:

+n Find only files last modified more than n days ago
n Find only files last modified exactly n days ago
-n Find only files last modified fewer than n days ago

To find files that have not been modified in the last n days, look for files that were last modified more than n days ago:

find / -mtime +90 -print

This shows all files that were last modified more than 90 days ago: that is, files that have not been modified in the last 90 days.

There are three forms of date checking and each takes +n, n, or -n as an argument:

-mtime Finds files last modified more than, exactly, or fewer than n days ago
-atime Finds files last accessed more than, exactly, or fewer than n days ago
-ctime Finds files whose inode was last changed more than, exactly, or fewer than n days ago. An inode is an entry in a disk table that contains information about a file such as its owner, size, and last access date. The inode is changed when the file is first created and also later if the owner, group, or permissions are changed.

-atime Is Often Defeated by Nightly Backups

In theory, find’s -atime option is useful if you are short of disk space and want to find files that have not been accessed in a long time so that you can archive them and delete them. However some backup programs, such as tar, prevent -atime from being useful because all files are accessed nightly during the system backup. cpio provides an -a option that remembers each file’s last access date and time and restores it after the file has been backed up so that find’s -atime option is still useful.

find: -size Option

The find -size option enables us to locate files based on the number of blocks in the file:

find / -size +2000 -print

This find command prints the names of all files that contain more than 2,000 blocks. It is useful when you want to find the largest files that are consuming disk space.

Following -size, you must specify an integer number:

+n Finds only files that contain more than n blocks
n Finds only files that contain exactly n blocks
-n Finds only files that contain fewer than n blocks


Tip:  
It is a very rare occasion when you need to search for files that contain an exact number of blocks. Usually you look for files that contain more than n blocks or fewer than n blocks. UNIX neophytes often forget the plus or minus sign for these types of find options and then wonder why find did not locate the expected files.


Previous Table of Contents Next