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 ...
Get Classic Shell Scripting now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.