What is the difference between 'locate' and 'find' in Linux?
Why there are two so similar command in Linux? and normally what are the circumstances to use each of them?
Solution 1:
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.
Solution 2:
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 withlocate
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 useupdatedb
to update the slocate database. Executinglocate
again will now find the newly created file. Thus, many Linux system administrators use acron
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. :)
Solution 3:
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.