Monday, May 09, 2005

Command Line: find

One of the most valuable commands at your fingertips when using Linux or Unix is the find command. This versatile command can be used for a variety of tasks, from listing the contents of a directory or filesystem to indexing your entire filesystem. Find can be difficult for the novice to master, especially when there is no instruction available. The man pages don't really show the friendly side of the system:

FIND(1L) FIND(1L)

NAME

find - search for files in a directory hierarchy

SYNOPSIS

find [path...] [expression]


Some basics to find are as follows:

A simple find command will list all files and folders recursively in your current working directory (CWD):


secondary ~ # find
.
./bin
./bin/update-today
./bin/httpd-block
./.rnd
./.ssh
./.ssh/known_hosts
./Mail
./.keep
./index.html
./.viminfo
./.bash_history
./.maildir


The second argument to find is the path, which, if blank, is assumed to be your CWD, as shown previously. You can also explicitly give the path:


secondary ~ # find .
.
./bin
./bin/update-today
./bin/httpd-block
./.rnd
./.ssh
./.ssh/known_hosts
./Mail
./.keep
./index.html
./.viminfo
./.bash_history
./.maildir


From the man page, we can see that after the path, we can give find an expression. This is where most people have trouble when starting out. The tendancy of most people is to limit themselves to a regular expression-type of expression when the bigger picture is that the expression possibilities are immense. Take the following, for example:


secondary ~ # find / -type d -name sbin
/usr/sbin
/usr/local/sbin
/sbin


As you can see, this command listed all of the directories in the filesystem by the name of sbin. The -type command was used to specify the type of file to find, in this case it was a directory. The options available are:


-type c
File is of type c:

b block (buffered) special

c character (unbuffered) special

d directory

p named pipe (FIFO)

f regular file

l symbolic link (never true if the -L option or the -follow option is in effect, unless the
symbolic link is broken).

s socket

D door (Solaris)


The two most used are going to be the file and directory options. The next option used was the -name option, which can be used to specify the name or a part of a name to search for. You can use a wildcard to find variations, since the '-name bin' option will not find 'sbin' or 'bind', but '-name *bin*' will find all of them. Note that using the -regex option can be very complicated, so it is easier to use the '-name' option and possibly a wildcard or two.

While these few options are enough to get you started, they are nowhere near tapping the resources of this powerful command. I recommend exploring and using this command frequently, as it will make your CLI experience much more rewarding!

No comments: