Locating files in the system

Use the command locate <substring> to find all files in the system whose names contains the string <substring>. An example: Suppose you want to find a file that has the string “duck” in its name. So you type in the following command and see what output you get on your system!

[alice@onyx ~]$ locate duck
/home/JimConrad/CS121/projects/p1/solution/ducks-animation
/home/JimConrad/CS121/projects/p1/solution/ducks-animation/README
/home/JimConrad/CS121/projects/p1/solution/ducks-animation/TrafficAnimation.java
/home/JimConrad/CS121/projects/p1/solution/ducks-animation/sun.png
/home/alice/cs121/duck.png
/usr/share/kde4/services/searchproviders/duckduckgo.desktop
/usr/share/kde4/services/searchproviders/duckduckgo_info.desktop
/usr/share/kde4/services/searchproviders/duckduckgo_shopping.desktop
[alice@onyx ~]$

The locate command accesses a database of names of all files on the system to perform the search. That is what makes it fast. The database is usually updated once a day automatically by the system.

If we provide a generic substring, then locate may find hundreds or even thousands of files. In this case, it is handy to pipe the output to grep to filter out the results of interest (more on pipes in a later module!). For example (note that | is the character that represents the pipe connecting the two commands):

[alice@onyx ~]$ locate duck | grep alice 
/home/alice/cs121/duck.png
[alice@onyx ~]$