O'Reilly logo

Classic Shell Scripting by Nelson H. F. Beebe, Arnold Robbins

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Finding Files

Shell pattern matching is not powerful enough to match files recursively through an entire file tree, and ls and stat provide no way to select files other than by shell patterns. Fortunately, Unix provides some other tools that go beyond those commands.

Finding Files Quickly

locate , first introduced in Berkeley Unix, was reimplemented for the GNU findutils package.[5] locate uses a compressed database of all of the filenames in the filesystem to quickly find filenames that match shell-like wildcard patterns, without having to search a possibly huge directory tree. The database is created by updatedb in a suitably privileged job, usually run nightly via cron. locate can be invaluable for users, allowing them to answer questions like, Where does the system manager store the gcc distribution?:

$ locate gcc-3.3.tar                     
               Find the gcc-3.3 release
/home/gnu/src/gcc/gcc-3.3.tar-lst
/home/gnu/src/gcc/gcc-3.3.tar.gz

In the absence of wildcard patterns, locate reports files that contain the argument as a substring; here, two files matched.

Because locate's output can be voluminous, it is often piped into a pager, such as less, or a search filter, such as grep:

$ locate gcc-3.3 | fgrep .tar.gz         
               Find gcc-3.3, but report only its distribution archives
/home/gnu/src/gcc/gcc-3.3.tar.gz

Wildcard patterns must be protected from shell expansion so that locate can handle them itself:

$ locate '*gcc-3.3*.tar*'                
               Find gcc-3.3 using wildcard matching inside locate ... /home/gnu/src/gcc/gcc-3.3.tar.gz ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required