Using locate, pipe and grep to find files with certain keywords in their names

458 Views Asked by At

I am not a expert user of any of the two commands but as per my understanding,

if I want to search "Theory of Computation" book on my computer,

locate -i computation | grep -i theory should give me all and only files with both theroy and computation in their names.

locate -i computation should output all files with computation in their names and from that list, thanks to the pipe, grep -i theory should output all the files having theory in their names, thus giving me my desired output.

However, when I run the above command, I get outputs like

/root/books/History theory and practice of the electric telegraph.pdf which means that my understanding of at least one of these commands (or pipe) is wrong. Where am I mistaken?

1

There are 1 best solutions below

1
On

I cannot see anything wrong with the commands that you are using. (And they work for me ... though obviously that doesn't explain your situation.)


One possible cause for what you are seeing is that the mlocate.db file is stale or mangled. The file is supposed to be updated on a daily basis by cron. However, if you habitually turn off your machine every night, then it might only be run very occasionally. This might cause it to report incorrect hits. Another possibility is that you have made a large number of changes to the file system since the last rebuild of the database.

I suggest that you do the following:

  • check the timestamps on the /var/lib/mlocate/mlocate.db file to see if it looks stale

  • if warranted, use updatedb to rebuild the database.