A mini-tutorial on the Unix find command

How do you copyright text on a public webpage? And how exactly can you copyright instructions on using something as simple as find?
Who Knows?
-AK
A mini-tutorial on the Unix find command
©2002 by Wayne Pollock, Tampa Florida USA. All rights reserved.
Locating Files
The find command is used to locate files on a Unix system. find will search any set of directories you specify for files that match the supplied search criteria. You can search for files by name, owner, group, type, permissions, date, and other criteria. The search is recursive in that it will search all subdirectories too. The syntax looks like this:
find where-to-look criteria what-to-do
All arguments to find are optional, and there are defaults for all parts. (This may depend on which version of find is used. Here we discuss the freely available GNU version of find, which is the version available on YborStdent.) For example where-to-look defaults to . (that is, the current working directory), criteria defaults to none (that is, show all files), and what-to-do defaults to -print (that is, display found files to standard output).
For example:
find
will display all files in the current directory and all subdirectories. The commands
find . -print
find .
do the exact same thing.
find / -name foo
will search the whole system for any files named foo and display them. Here we are using the criteria -name with the arugment foo to tell find to perform a name search for the filename foo. The output might look like this:
/home/wpollock/foo
/home/ua02/foo
/tmp/foo
If find doesn't locate any matching files, it produces no output at all.
The above example said to search the whole system, by specifing the root directory ("/") to search. If you don't run this command as root, find will report each directory that you don't have read permission on. This can be a lot of messages, and the matching files that are found may scroll right off your screen. A good way to deal with this problem is to redirect the error messages so you don't have to see them at all:
find / -name foo 2>/dev/null
Other Features And Applications
You can use wildcards in the search:
find . -name foo\*bar
This will search from the current directory down for foo*bar (that is, any filename that begins with foo and ends with bar). Note that wildcards in the name argument must be quoted so the shell doesn't expand them before passing them to find.
You can search for other criteria (or combinations of criteria) beside the name. For example:
find / -type f -mtime -7 | xargs tar -rf weekly_incremental.tar
gzip weekly_incremental.tar
will find and archive any files ("-type f") modified seven or fewer days ago ("-mtime -7"). Note the use of xargs, a handy utility that coverts a stream of input (in this case the output of find) into command line arguments for the supplied command (in this case tar) 1.
Another use of xargs is illustrated below. This command will efficiently remove all files named core from your system (provided you run the command as root of course):
find / -name core | xargs /bin/rm -f 2>/dev/null
One of my favorite find criteria is to locate files modified less than 10 minutes ago. I use this right after using some GUI or command line system administration tool, to learn which files got changed by that tool:
find / -mmin -10
Another common use is to locate all files owned by a given user. This is useful when deleting user accounts at the end of each term.
The find command can be amazingly useful. See the man page to learn all the criterial you can use.

Footnotes:
1. Using the tar option "-c" is dangerous here; xargs may invoke tar several times if there are many files found and each "-c" will cause tar to over-write the previous invocation. The "-r" option appends files to an archive. Other options such as those that would permit filenames containing spaces would be useful in a "production quality" backup script. Back


0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home