=================================================== Searching for and finding files by name, size, etc. =================================================== -Ian! D. Allen - idallen@idallen.ca - www.idallen.com > 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? People confuse "grep", which looks for text inside files (and doesn't look at the file names), with "find", which finds files by name (and doesn't look inside the files). The grep command looks for patterns *inside* files whose names you already know. It isn't useful by itself for finding and generating the *names* of those pathnames. (To use grep to find the full name of a pathname, you would first have to generate a list of *all* pathnames and feed that to grep as standard input - see the example below.) You can tell grep to search the contents of an entire directory tree of files by turning on the grep "recursive" opion; but, that won't help you find the name of a file in that directory. Grep searches content inside files, not names of files. To find files by name, or size, or modify date (etc.) use the "find" command. ---------- Using find ---------- You can generate a list of all pathnames under a given directory using the "find" command, 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 locate or 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 much faster than "find", even using a modified GLOB pattern, e.g. $ locate passwd | less ... see all the names containing the string "passwd" here ... $ locate '/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 locate 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 "locate" database lookup is much, much faster than "find /". If you are looking for a new pathname that isn't in the locate 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 in a pipeline. 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 -maxdepth 1 -user root -type d /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". See the man page. -- | Ian! D. Allen - idallen@idallen.ca - Ottawa, Ontario, Canada | Home Page: http://idallen.com/ Contact Improv: http://contactimprov.ca/ | College professor (Free/Libre GNU+Linux) at: http://teaching.idallen.com/ | Defend digital freedom: http://eff.org/ and have fun: http://fools.ca/