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.
2 thoughts on “How to find files in Linux”
Hi … how do you do? I’m really happy when read out yr site… all of your guide in here have helped me anymore. I’m a beginner in learning database system as mysql etc. I always tried to find someone who is expert in database system. I have found yr website. I’ll always visit your website. Thank you …
Thank you Bety, I’m glad my site is useful to you.