The Computer Oracle

What is the difference between 'locate' and 'find' in Linux?

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Darkness Approaches Looping

--

Chapters
00:00 What Is The Difference Between 'Locate' And 'Find' In Linux?
00:15 Accepted Answer Score 56
00:40 Answer 2 Score 5
01:59 Answer 3 Score 2
02:15 Answer 4 Score 0
02:44 Thank you

--

Full question
https://superuser.com/questions/199472/w...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#linux

#avk47



ACCEPTED ANSWER

Score 56


find searches in the real system. Is slower but always up-to-date and has more options (size, modification time,...)

locate uses a previously built database (command updatedb). Is much faster, but uses an 'older' database and searches only names or parts of them.

In any case, man find and man locate will help you further.




ANSWER 2

Score 5


Both the locate and find commands will find a file, but they work in quite different ways.

locate will work in an offline mode:

  • For a simple explanation, the file indexing database in Unix system called slocate will list the locations of all files which ship with the Unix system. When you execute locate, it'll use that database to search for a particular file. The problem with locate is if you just created a file which you now want to search for, locate will not work because the slocate database is not up-to-date. To overcome this problem, you can use updatedb to update the slocate database. Executing locate again will now find the newly created file. Thus, many Linux system administrators use a cron job to regularly update the slocate database.

find will work in an online/"in real time" mode.

  • It will actually go and search all the directories to find the particular file specified and it examine each file one-by-one. Therefore, it requires a lot of I/O calls.

So based on the nature, it is clear that locate is faster than find but find is real time.

Hope this will help to clear the idea. All the best. :)




ANSWER 3

Score 2


locate simply looks its database and reports the file location.

find does not use a database, it traverses all the directories and their sub directories and looks for files matching the given criterion.




ANSWER 4

Score 0


An alternative to using find is the locate command. This command is often quicker and can search the entire file system with ease. You can install the command with apt-get:

sudo apt-get update
sudo apt-get install mlocate

The reason locate is faster than find is because it relies on a database of the files on the filesystem. The database is usually updated once a day with a cron script, but you can update it manually by typing:

sudo updatedb

Run this command now. Remember, the database must always be up-to-date if you want to find recently acquired or created files.