Using find

The find command has a bewildering number of options, and you will sometimes see complex examples of the use of find. But in most cases where you will want to use it, the situation will be something like this: You believe that a file with a certain name exists somewhere below a certain directory (say ~ /temp/). Change to that directory and type:

You will see everything below the current directory being listed. So if you are looking for afile and you type:

[email protected]:~/temp> find . | grep afile you will get some output showing the path to afile (or any file whose name includes the string afile) if it exists anywhere under the directory ~ /temp. In most cases this is all you need, although you will read elsewhere that the "correct" way to use the find command is to do this:

[email protected]:~/temp> find . -name afile -print

This finds any file below the current directory with the exact name afile.

On occasion, you may want to pipe the output of the find command to another program such as cpio (see more about cpio later in the chapter) or use the built-in -exec option of find to perform another command on the list of files find produces. For example:

This executes the lpr command and prints any file that is found by the find command. Here the {} (curly brackets) represent each file that was in the output of find. The \; is needed to terminate the command.

Was this article helpful?

0 0

Post a comment