Thursday, December 16, 2004

Using "find" command

The basic format of the find command is:

$ find startdirectory options matchcriteria [actionoptions]

If you know the name of a file, or even part of the name, but not the directory it is in, you can do this:

$ find . -name 'test*'
./test
./jdevhome/mywork/EmpWS/EmpBC4J/test


Unlike classic UNIX systems, the -print action at the end is not required in Linux, as it is assumed if no other action option is designated. A dot ( . ) in the startdirectory position causes find to begin a search in your working directory. A double dot, .., begins a search in the parent directory. You can start a search in any directory.

Note that you can use wildcards as part of the search criteria as long as you enclose the whole term in single quotes.

$ find . -name 'test*' -print
./test.out
./test2.out

To produce a list of files with the .out extension:

$ find /home -name '*.out'


Remember, however, that you will probably get numerous "Permission denied" error messages unless you run the command as supersuser.

One of the most powerful search tools is the -exec action used with grep:

$ find . -name '*.html' -exec grep 'mailto:foo@yahoo.com' {} \;

Here we have asked find to start in the current directory,., look for an html file, *.html, and execute -exec the grep command on the current file, {}. When using the -exec action, a semicolon, ;, is required, as it is for a few other actions when using find. The backslash, \, and quotes are needed to ensure that BASH passes these terms through so they are interpreted by the command rather than the shell.

0 Comments:

Post a Comment

<< Home