19. Linux's Find Command
Here's a tip that's a bit tough to explain but that's well worth your time to figure out. Just like it sounds, you use the Linux Find command to locate one or more files. It works best when you know the name of the file or directory you're looking for. Proper syntax is as follows:

find [path...] [expression]

If no path is specified, it searches the current directory and any subdirectories.

The Find command is used alongside other commands like Locate (or "slocate") and Whereis. Find, though, has some flexibility, including support for wildcard operators. So, you can use Find with an asterisk for a wildcard with one limitation. The asterisk will not replace a dot in file name. So to find the file "lilo.conf", you type:

find lilo.* (not lilo*)

For more information on Locate and Whereis, check out Scot's Newsletter Forums.

For now, though, keep reading and then experiment a bit to see how it works for you.


IMPORTANT: The tips in this document require the use of command-line commands. For more information about how to read and execute Linux command-line prompts and commands, please check the Linux Clues Linux Cheat Sheet, especially Linux Prompt Basics and Linux Command-Line Nomenclature.


Here is the basic way to use Find. You might type something like this:

$ find -type f -name dummy

No path is given, so it looks in the present directory and its subdirectories. You use "-type f" to tell Find you're looking for a file (what the "f" stands for) and not a directory (d) or link (l). The "-name dummy" tells Find you're looking for a file named dummy. You should see it find the answer by displaying:


Because you didn't use a wildcard, it finds a specific filename. If you weren't sure of the name, you could type something like:

$ find -type f -name *ummy

That would find all files in the current directory that end in "ummy", including dummy, rummy, strummy, etc.

Next, type:

$ find / -type f -name dummy 2>/dev/null

This time Find searches the local directory because the first argument (path) includes the forward slash. It will also search the complete file system. That takes a while. And if you give the command as normal user, not as root, you would see several permission denied messages. To avoid that, you add that last bit: 2>/dev/null

See the SN Forums' information on the Black Hole for a deeper explanation:

On Bruno's Mandrake installation, the results to the last command are:


You can also add commands that will act on the results of the Find command. To see how this works, type this command:

$ find / -type f -name dummy 2>/dev/null -exec cat {} \;

The "-exec cat" expression executes the cat command on the Find operation's results, which are represented by the {} (or open bracket, close bracket). The \; at the end of the line signals the end of the exec command.

On Bruno's system, the results of the last command look like this:


Now the final step, type:

$ find / -type f -name dummy 2>/dev/null -exec cat {} \; >tesst.txt

This version of the command performs all the actions previously described and then writes the results into a file called tesst.txt. It's a command that's particularly handy for reviewing config files. So if you do not know where your lilo.conf file is located, but you want a copy of it to review in your home directory, type:

$ find / -type f -name lilo.conf 2>/dev/null -exec cat {} \; >lilo.txt

Once you run that, you'll find a file called lilo.txt in your /home directory.

Because I know you want to know more, here is the fun step:

$ find /home -type f -name *.sxw -atime -3 -user bruno

Translated, this command finds all OpenOffice sxw files in the /home directory that have been accessed in the last three days and are owned by the user bruno. (This is a good way to check if anyone's been messing with your files.) To find all the same files that have *not* been accessed in the last three days, use the expression "-atime 3".

Tip: If you try searching for a text file, using *.txt and get an error, use *.TXT instead. (We don't know why this works. It's just something we've found while searching for text files over the years.)

For more information about Find, check out its man page in Linux:

man find

You'll find more detail about the -atime and -name expressions, as well as other expressions you can use.