Files in Linux can be searched with find and locate command. Find exists very long, while locate is a new Linux feature. The main difference between them is the way they work – locate uses database to search files, while find searches for files in a directory hierarchy.
How to find files using locate:
# match the whole path name against the specified patterns locate config.php # match only the base name against the specified patterns locate -b config.php # match the whole path name against the specified patterns and stop after finding 20 entries locate -n 20 config.php # match only the base name against the specified patterns and ignore case distinctions locate -i -b config.php
How to find files using find:
# find files named config.php in or below the current directory find . -name config.php # find files named util* in or below the /var/www/html directory and ignore case distinctions find /var/www/html -iname "util*" # match the whole path name against the specified patterns and ignore case distinctions find . -iwholename "*config*"
locate is faster then find, but needs database. Database is located in /var/lib/mlocate/mlocate.db and is daily updated by cron. After Linux installation, locate will not work because mlocate.db database should be initialized. You can wait until next morning or you can force initialization with the following command as root user:
# create or update a database for locate command /etc/cron.daily/mlocate.cron
This step is needed only once – if you are not patient – and after database initialization, locate will start to work. On the other hand, find will work from the beginning. It is slower but a few examples mentioned here are only the top of the iceberg. find is very powerful command and the search can be related to the file permissions, type, size, time … If you want to see how, please read the post The “find” command.