GNU/Linux Shell Locate a file on your system


Using bash you can easily locate a file with the locate command. For example say you are looking for the file mykey.pem:

locate mykey.pem

Sometimes files have strange names for example you might have a file like random7897_mykey_0fidw.pem. Let's say you're looking for this file but you only remember the mykey and pem parts. You could combine the locate command with grep using a pipe like this:

locate pem | grep mykey

Which would bring up all results which contain both of these pieces.

Note that not all systems have the locate utility installed, and many that do have not enabled it. locate is fast and efficient because it periodically scans your system and caches the names and locations for every file on it, but if that data collection is not enabled then it cannot tell you anything. You can use updatedb to manually initiate the filesystem scan in order to update the cached info about files on your filesystem.

Should you not have a working locate, you can fall back on the find utility:

find / -name mykey.pem -print

is roughly equivalent to locate mykey.pem but has to scan your filesystem(s) each time you run it for the file in question, rather than using cached data. This is obviously slower and less efficient, but more real-time. The find utility can do much more than find files, but a full description of its capabilities is beyond the scope of this example.