======================================= Searching for and finding files by name ======================================= -IAN! idallen@idallen.ca > How can we look for a file name? > What if we don't know which directory that file is located in? > Can we start a grep at the root and ask the grep command to look in > all subdirectories? The grep command looks for patterns *inside* files whose names you already know. It isn't useful by itself for finding the *names* of pathnames, unless you feed it a list of pathnames in which to look. First, you have to generate a list of pathnames. ---------- Using find ---------- You can generate a list of all pathnames under a given directory using the "find" comand, and you can then use grep on that piped output, e.g. $ find ... all pathnames under the current directory list here ... $ find "$HOME" ... all pathnames under your HOME directory list here ... $ find /bin | wc -l 107 $ find /bin | grep sh /bin/bash2 /bin/csh /bin/rbash /bin/sh /bin/bash /bin/tcsh Yes, you can do "find /" (find, starting at the ROOT) and it will generate a list of all the pathnames on the whole machine that you have permissions to see - tens of thousands of them. This will take a long time. (You will also see many error messages about permissions, since your userid does not have permissions to look in every directory on the whole system.) Don't run "find /" on a shared computer unless you really have to. (But feel free to try it on your own machine!) ------------- Using slocate ------------- Many Unix systems run a weekly or nightly "find /" late at night and save the results in a small database. The "locate" or "slocate" commands can quickly search that saved database for you, even using a modified GLOB pattern, e.g. $ slocate passwd | less ... see all the names containing the string "passwd" here ... $ slocate '/etc/*passwd*' /etc/pam.d/passwd /etc/rc.d/init.d/yppasswdd /etc/samba/smbpasswd /etc/passwd /etc/passwd.OLD Note the use of quotes to stop the shell from interpreting the GLOB pattern. (We want slocate to process the GLOB pattern against the pathnames in the database; we do not want the shell to process the GLOB pattern against current pathnames in the file system.) If you are looking for a pathname that has been around for a while and is entered into the database, the "slocate" database lookup is much, much faster than "find /". If you are looking for a new pathname that isn't in the slocate database, only "find" will find it for you. --------------------- More features of find --------------------- The "find" command actually has its own huge set of operators for finding pathnames more efficiently than using grep on its output. For example: $ find /bin -name '*sh' /bin/bash /bin/rbash /bin/sh /bin/csh /bin/tcsh /bin/ksh /bin/zsh $ find /bin -type f -size +500k /bin/bash /bin/fbmngplay.static /bin/fbtruetype.static $ find /tmp -user root -type d -maxdepth 1 /tmp /tmp/.ICE-unix /tmp/.X11-unix /tmp/screens The "find" command can find pathnames based on any combination of any of the atributes you see in the output of "ls -lsia".