Linux command line - Locate Vs find


Hi, I am Malathi Boggavarapu working at Volvo Group and i live in Gothenburg, Sweden. I have been working on Java since several years and had vast experience and knowledge across various technologies.

In this post we discuss about locate and find commands and understand which one is more effective from one another. Also we discuss few important options available for locate command.

It is important to note that both of these search commands works well but locate works in more convenient way as it uses more effective ways to quickly process search queries issued by the users.

Locate Vs find

locate is one of the easiest and efficient way of finding the files in the file system. Though both find and locate will find out the files in the file system, locate works in more effective and fastest way in finding the file.

locate command relies on mlocate database. It will look through that database which contains bits and parts of files and their corresponding paths on your system whereas find command will search the file system when search is initiated.

Below are few most important options available for locate command.

locate access.log - Find the file access.log in the file system
output:

/home/malathi/access.log
/home/malathi/http/access.log

locate "*.html" -n 5 - It will just show the first 20 files that end with .html
output:

/home/malathi/1.0/access.log
/home/malathi/http/1.0/access.log
/home/malathi/2.0/access.log
/home/malathi/http/2.0/access.log
/home/malathi/3.0/access.log


locate -c [access] - Display the count of all matching entries of file "access".
output: 1550

locate -i *text.txt* - It ignores case sensitive while looking up the files
output: text.txt, TEXT.txt

sudo updatedb - Since locate relies on mlocate database, it needs to be updated regularly. So we use updatedb command to update the database but using the administrative privileges and hence using sudo.

locate -i -e *text.txt* - Though we update the mlocate database, locate still find the deleted files from the file system. Inorder to avoid that, we use -e

locate -i -o *text.txt* - locate command default seperator is new line (\\n) character. Inorder to change this to ASCII NUL, you can use -o option

output:
/home/malathi/text.txt/home/malathi/1.0/text.txt

More to come--

Hope the post is helpful


Comments

Popular posts from this blog

Bash - Execute Pl/Sql script from Shell script

How to get client Ip Address using Java HttpServletRequest

How to install Portable JDK in Windows without Admin rights