COM-FSM Seal College of Micronesia-FSM
Federated States of Micronesia
Computing · Directory · Home · Employment · Library · News

Technology Policy

Virus Information
Web Tools

UNIX Commands

UNIX Command of the Day - find

Topic: Exploring files and directories

If you haven't discovered that UNIX systems are large, complex collections of software you haven't been trying to really use the system yet. There are literally hundreds of thousands of files in hundreds of directories, all needed for some aspect of a complete UNIX system. Trying to figure out where a particular file is hiding can be a painful process. Sometimes we don't even know the name of the file we're looking for, only what it should contain.

Today, we'll consider only the first problem: locating a file that we know the name of, or at least part of the name. The second problem we'll look at tomorrow.

Basic Searching

Probably the most common situation I deal with is locating a file that I know exists. Maybe I've found the name in a manual page, but it didn't tell me which directory it was located in, or it may evenbe a file that I created and forgot where I stored it. Either way there is a file somewhere, I know what it's called, and I probably have some general idea of where it should be located.

My tool of choice is the find command. It's only job is to look through various files and directories and tell me where things are. If, as I have described above, I know of a file named "lamb.txt" that is somewhere in my directory (but buried in a some folder where I can't easily see it), here's what I try:

  $ cd  (to return to my home directory)
  $ find . -name lamb.txt -print

Yes, the options for find are a little different. The documentation for find refers to these as the expression instead of options, and as we'll see later, that's an apt description.

At any rate, the command I've used instructs find to locate files starting in this directory (the period indicates the current directory, from which all subdirectories are searched) named lamb.txt, and when a match is found to print it's location. Find located one file a couple subdirectories deep.

Suppose you weren't sure what you called the file? Maybe it could be "lamb.doc" or "lamb-chops.txt." In that case, we can broaden the search by using wildcards:

  $ find . -name lamb\* -print

This works the same as last time, except the file name only has to begin with "lamb," so we found another file. Notice that the asterisk was escaped when it was used. This prevents the shell from attempting to expand the wildcard into a list of matching files, which would prevent the command from working as expected.

Note that the -name expression is case sensitive, so if you aren't sure if you've capitalized any part of the file name use -iname instead, which ignores case.

More advanced options

Often you don't know the name of a file you're looking for, or maybe need to generate a list of files. Find supports a large number of ways to identify files, including when they were created or modified, their size, or permissions. For example:

  find /var/spool/mail -atime +30 -print

would list mailboxes that haven't been checked (based on their access time) in over 30 days. If you changed the +30 to -30 this would have meant the opposite, mailboxes that have been checked in the last 30 days. Another example is handy after you have removed some UNIX accounts:

  find /var/spool/mail -nouser -print

which would list mailboxes for users that no longer exist on the system.

Each of the tests that find supports can be combined in multiple ways. For example you could look for files that have a couple possible names like this:

  find . -name lamb\* -o -name chicken\* -print

The -o indicates that if either -name lamb\* or -name chicken\* are true, the file name should be printed. A -a (and) indicates that both conditions must be true.

One form I use commonly is this:

  find . -mtime -2 -print

This will print a list of files modified less than two days ago. When you've been working on something and have forgotten the name of it, this sometimes helps!

A consideration

It is possible to have find search every directory on the computer for a file by starting the search in the root directory. Please don't. Not only will you get a lot of error messages about inaccessible directories, searching those that are accessible will take long, long time. At last count there were over 300,000 files on our computer and find would have to consider each and every one for your search.

The Manual Page for find

Back to UNIX Command of the Day

Navigate graphicTo COM-FSM Home Page