The following was removed from Chapter 9: “Finding Files, Directories, Words, & Phrases”.
Manage Results Received When Searching a Database of Filenames
locate -n
If you use locate
very much, eventually you’re going to run into something like this:
$ locate pdf
/etc/cups/pdftops.conf
/etc/xpdf
/etc/xpdf/xpdfrc-latin2
/etc/xpdf/xpdfrc-greek
/etc/xpdf/xpdfrc-turkishp
/etc/xpdf/xpdfrc
/etc/xpdf/includes
[results greatly truncated due to length]
On this computer, we get 2,373 results. Far too many! Better to use this construction instead:
$ locate pdf | less
Pipe the output of your locate
search to the pager less
(which was covered in “View Text Files a Screen at a Time,” in Chapter 5, “Viewing Files”), and you get your 2,373 results one manageable screen at a time. Inject grep
(locate pdf | grep xpdf | less
), and you’ll get even finer-tuned results.
If you just want to see the first x results, where x is an integer of your choice, use the -n
option, followed by the number of results you want to see.
$ locate -n 5 pdf
/etc/cups/pdftops.conf
/etc/xpdf
/etc/xpdf/xpdfrc-latin2
/etc/xpdf/xpdfrc-greek
/etc/xpdf/xpdfrc-turkish
This is far more manageable, and might be all you need. Don’t let locate
just spew a flood of results at you; instead, take control of the locate
command’s output and use it to your favor.