Department of Electrical Engineering and Computer Science



Search commands

This document describes three commands for searching for files/directories. For additional information about these commands, log onto an EECS machine and type man command.

Files in a directory hierarchy

This section describes two commands to search for files in specific directories – one for Unix and Linux, and the other specifically for Linux.

Generic Unix command

The find command searches for files in specific directories. The basic syntax for find is:

find path expression

The path argument is the starting directory from which find will search. The expression argument describes the type of file to find. Here are some examples:

CommandEffect
find /dir -name code.c Finds all files in the /dir directory named code.c.
find ~ -name myfile Finds all files in your home area (~) named myfile.
find mydir -type d Finds all directories (denoted by -type d) in the mydir directory.

You can also use wildcards to search for filenames containing a certain pattern:

CommandEffect
find ~ -name “*homework*” Finds all files in your home area with a name containing the phrase homework.

There are many more options for find; consult the manual page for more information.

Linux alternative

Under Linux, the locate command is similar to the find command. Here is an example:

prompt> locate xpdf
/usr/share/doc/xpdf-0.92
/usr/share/doc/xpdf-0.92/CHANGES
/usr/share/doc/xpdf-0.92/README
/usr/share/man/man1/xpdf.1.gz
/usr/bin/xpdf-handle-url
/usr/bin/xpdf
/etc/X11/applnk/Graphics/xpdf.desktop
/etc/current/db_current/headers/xpdf-0.92-5.i386.hdr.gz

By default locate finds any files containing the given search string, whereas wildcards must be used with find (e.g., find *xpdf*).

Files in the command path

The which command is similar to find, except which only searches for binaries (i.e., executables) that are located in your shell's command path. The which command is useful for determining which version of an executable you are using. Here is an example:

prompt> echo $PATH 
/usr/local/bin:/usr/local/gnu/bin:/usr/local/sfw/bin:/usr/openwin/bin:/usr/bin:/sbin:/bin

prompt> ls -la /usr/local/gnu/bin/ls /usr/bin/ls
-r-xr-xr-x 1 root bin 18844 Jan 5 2000 /usr/bin/ls
lrwxrwxrwx 1 root root 25 Jan 2 11:36 /usr/local/gnu/bin/ls

prompt> which ls
/usr/local/gnu/bin/ls

The first command displays the shell's command path. The second command shows that there are two binaries named ls – one in /usr/bin and the other in /usr/local/gnu/bin. (Note that both of these directories are in the command path.) The third command shows that the ls command corresponds to usr/local/gnu/bin/ls. Because /usr/local/gnu/bin is listed before /usr/bin in the command path, when ls is typed the /usr/local/gnu/bin/ls is used.


 

The University of Tennessee, Knoxville. Big Orange. Big Ideas.

Knoxville, Tennessee 37996 | 865-974-1000
The flagship campus of the University of Tennessee System