Searching for files
In spite of files being arranged in a directory structure, the
complexity of the file structure is still high. Using
ls may not be an easy way of finding some files -
suppose, for instance, that you had a large number of files and
many subdirectories, and that somewhere you had created a file
myfile . How would you find it? In any event, searching
for files other than by name is hit-and-miss using ls
- how could you print out the names of all your executable files of
size greater than 1k, for instance? You would, at this stage, have
to list all your files, send the output to a file, and edit that
file.
There is a command find which can be used to
examine all files within a directory (and all subdirectories of it)
and select them according to criteria such as the group
the file belongs to, the time of last modification, its
name, its access permissions, its size,
and so on. The syntax is find , followed by a pathname
(which should normally be a directory, but find will
work if its argument is just a file) and then the criteria
find is to use. For instance, to print the pathnames
of all files in your home directory called myfile , you
could have:
$ find ~ -name myfile
-print
This will search your home directory (~ ), looking
(-name ) is myfile , and display
(-print ) the full pathname of each such file to
standard output.
Note that the criteria for find selecting files are
real words, not single letters, and that the criteria for
find are simply arguments to find , not
options for files whose name Note also that in order for
find actually to print out the names of the files
found, you must explicitly state this by using
-print .
Instead of printing the names of the files found, you can tell
UNIX to run any other command on those files. It is likely that you
will normally only use find to display the names of
files, but instead of simply displaying names, find
can be instructed to perform other actions on files it has
selected. The following instructs find to perform
wc on all files in the current directory (and any
subdirectories) owned by chris :
$ find . -user chris -exec wc {}
\;
The directory find is searching is .
(the current directory), the criterion it uses to select files is
-user chris , meaning files owned by
chris . The action it takes when it has selected a file
is to execute (-exec ) the command wc on
the file. The notation {} is shorthand for the name of
that file. The semicolon terminates the action to be taken
(otherwise the shell command used as the action for
find would get confused with the find
command itself), and must be escaped with a \ . The
arguments to find are of three varieties:
options which affect the command's behaviour,
tests which specify which files are to be selected, and
actions which specify what will be done with the selected
files.
Worked example
Remove all files named core from your
filespace.
Solution: Use find to locate the
files, and then -exec to call rm to
delete them:
$ find ~ -name core -exec rm {}
\;
|