Why there are two so similar command in Linux? and normally what are the circumstances to use each of them?
5 Answers
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.
- 973
-
2...and
updatedbdo roughly someting likefind / -type f | gzip > locate.gz. – F. Hauri - Give Up GitHub Jan 05 '13 at 14:40 -
12http://unix.stackexchange.com/questions/60205/locate-vs-find-usage-pros-and-cons-of-each-other answers this question in more detail. – nutty about natty Mar 15 '13 at 16:59
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 withlocateis 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 useupdatedbto update the slocate database. Executinglocateagain will now find the newly created file. Thus, many Linux system administrators use acronjob 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. :)
- 154
- 151
- 1
- 3
Briefly, the 'locate' command searches in a .db file called mlocate.db, that file contains all your filesystem files path. It is up-to-date once a day I think by a corn job, so it's much faster than 'find' command because 'find' search in the directory given as parameter and all the subdirectories, that means using a lot of I/O calls.
- 11
-
Avoid posting answers to old questions that already have well received answers unless you have something substantial and new to add. – Toto Feb 06 '23 at 10:10
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.
- 176