Online Book Reader

Home Category

Classic Shell Scripting - Arnold Robbins [152]

By Root 937 0
family type command does the job:

$ type gcc

Where is gcc?

gcc is /usr/local/bin/gcc

$ type type

What is type?

type is a shell builtin

$ type newgcc

What is newgcc?

newgcc is an alias for /usr/local/test/bin/gcc

$ type mypwd

What is mypwd?

mypwd is a function

$ type foobar

What is this (nonexistent) command?

foobar not found

Notice that type is an internal shell command, so it knows about aliases and functions as well.

The pathfind command that we presented in Example 8-1 provides another way to search for files in any directory path, not just the PATH list that type searches.

The find Command

If you want to select, say, files larger than a certain size, or modified in the last three days, belonging to you, or having three or more hard links, you need the find command, one of the most powerful in the Unix toolbox.

Implementations of find offer as many as 60 different options, so we can discuss only a few of them. The sidebar in this section summarizes the important find options.

If you need to go swinging through the branches of directory trees looking for something, find can probably do the job for you, but you may first have to slog through its manual pages to find out how. The GNU version has an extensive manual, and we recommend it for detailed study.

* * *

find


Usage

find [ files-or-directories ] [ options ]

Purpose

Find files matching specified name patterns, or having given attributes.

Major options

See the text for a description of the numbers mask and n that follow some of these options:

-atime n

Select files with access times of n days.

-ctime n

Select files with inode-change times of n days.

-follow

Follow symbolic links.

-group g

Select files in group g (a name or numeric group ID).

-links n

Select files with n hard links.

-ls

Produce a listing similar to the ls long form, rather than just filenames.

-mtime n

Select files with modification times of n days.

-name `pattern`

Select files matching the shell wildcard pattern (quoted to protect it from shell interpretation).

-perm mask

Select files matching the specified octal permission mask.

-prune

Do not descend recursively into directory trees.

-size n

Select files of size n.

-type t

Select files of type t, a single letter: d (directory), f (file), or l (symbolic link). There are letters for other file types, but they are not needed often.

-user u

Select files owned by user u (a name or numeric user ID).

* * *

* * *

find (continued)


Behavior

find descends into directory trees, finding all files in those trees. It then applies selectors defined by its command-line options to choose files for further action, normally printing their names or producing an ls-like verbose listing.

Caveats

Because of find's default directory descent, it potentially can take a long time to run in a large filesystem.

find's output is not sorted.

find has additional options that can be used to carry out arbitrary actions on the selected files. Because this is potentially dangerous, we do not recommend their use except in tightly controlled situations.

* * *

Using the find command

The most unusual thing about find as a Unix command is that the files and directories to search come first in the argument list, and directories are (almost) always descended into recursively. The options that select names for ultimate display or action come at the end of the command line.

Unlike ls and the shells, find has no concept of hidden files: if a dotted filename is present, find will find it.

Also unlike ls, find does not sort filenames. It just takes them in whatever order they are found in directories that it reads, and that order is effectively random.[7] Thus, you'll likely want to include a sort stage in a pipeline following the find command.

Again, unlike ls, when find has a directory to process, it dives with gusto recursively into that directory to find everything below it, unless you tell it not to with the -prune option.

When find finds a file

Return Main Page Previous Page Next Page

®Online Book Reader