CLI Part 2. Searching

locate - Find a file

Will match filenames to a given argument. You should also bare in mind this search is made on a database which is periodically updated. If you would like to force an update on the database you can do so by running:

On OS X:

$ /usr/libexec/locate.updatedb

On Ubuntu:

$ updatedb

Once the database has been populated, the locate command can be run.

Examples:

$ locate hosts
$ locate .conf 

find - Recursive file listing

Provided with a directory as its argument, it will recursively list all files within it. Plug a grep on the end and it's great for retrieving large sets of files.

Examples:

$ find . 
$ find . | grep .conf

Further examples:

$ find . -name *.config // files with names ending in ".config"
$ find . -iname *.config // files with names ending in ".config", case insensitive
$ find . -name *.config -type f // files with names ending in ".config", files only (not directories)

Thanks to Alex Gorrod for these extended tips.

grep - Pattern matching

So when I'm looking for any instance a given string in the file system or in the output of a command, I use grep.

On its own it can be used to scan file contents, for example:

$ grep -r "Pattern matching" .

Used in conjunction with another command, it will filter out unwanted output, for example:

$ locate bin | grep mysql

Or, if there are way too many for a single page:

$ locate bin | grep mysql | more

Comments:

Leave a comment