How to find files in Linux using locate or find commands

How to find files in Linux using locate or find commands

In Linux, there are a variety of commands to locate files, each with its own set of advantages and disadvantages. Some are only available in certain distributions, and others are not installed by default.

Let’s start with the quickest approach to find files with locate, and then go on to find: a very powerful application that is available in virtually all Linux distributions to help users in finding files.


locate

Usagelocate <what you are looking for>
AdvantagesLooks through a database, therefore, is really fast
Disadvantagesthe database has to be updated in order to find new files

Install:

yum install mlocate

Then update the database with the command:

sudo updatedb && locate -e bench-repo

Otherwise you will get an error: locate: can not stat () `/var/lib/mlocate/mlocate.db’: No such file or directory

Example:

locate whois
image 114 - How to find files in Linux using locate or find commands

find

Usagefind <path> -name “<what you are looking for>”
AdvantagesNo database to use and has lots of advanced features
DisadvantagesCan be slow and requires more typing even for a simple search

Example:

Search for exact matches of “whois”

find / -name "whois"

Search for files that have “whois” in their name

find / -name "*whois*"
image 115 - How to find files in Linux using locate or find commands

Both of the above methods used for finding files in Linux can have many parameters. You can read the man page of locate or man page for find to read more ways to run the commands to find files.

whoami
Stefan Pejcic
Join the discussion

I enjoy constructive responses and professional comments to my posts, and invite anyone to comment or link to my site.