locate Command Tutorial With Examples For Linux To Find Files – POFTUT

locate Command Tutorial With Examples For Linux To Find Files

Linux has different tools to search and find files and directories. locate is one of them which is very popular. The alternative for locate is find command. But they work differently from each other. Find command searches in real time but locate command uses a database which holds all files and directories of the system. In this tutorial, we will look at how to use locate.

Syntax

Help

We can list help information about the locate command with the -h option.

Help

Help

Update Database

As we stated in the introduction locate command uses a database to hold whole file system directories and files. This database is not updated automatically. So we should update the database to use more effectively. As there are some files requires root privileges to read and update database we should provide root privileges for the updatedb command.

Update Database

Update Database

Generally, this update operation is not taking too much time in periodic updated. But if the update runs for the first time this may some more times to setup database.

Search With Locate

Now we have updated our database we can search for some terms. We will use locate command the provide the search term. In the example, we will search for a.txt term.

Search

Search

As we can see from results the term a.txt is not search for files it is simply looked in the full path string.

Display Only Existing Results

As we know locate command looks the database to find files and folders about the search term. What if the database is updated one year ago and there is a result those not exists anymore. Locate command has the ability to double-check if the result is existing currently. We will use -e option to activate this feature.

LEARN MORE  How To Get The Size Of Directory In Linux From Command Line?

Display Only Existing Results

As we can see the first result /home/ismail/a.txt not listed in the results because it is removed in the file system but the related record exists in the database. With -e option we have corrected the result.

Limit Result

Some times there may be a lot of results which will fill our terminal. This is generally an unwanted situation. There is an option which will limit the results count those will be printed to the terminal. We will use -l option with the number. In the example, we only want to list 5 results about search.

Display Result Count

Display Result Count

Display Only Result Count

Some times we only interested in the count of results. This can be done in various ways with helper commands. But locate command all ready provides an option for this. We will use -c option to only list the count of results.

OR

Display Only Result Count

Display Only Result Count

Display Statistics and Database Information

The database of locate will hold a lot of records. We can get information about this like statistics with-S option. this will list directories count, files count, size of file names and the total size of the database.

 Display Statistics and Database Information

Display Statistics and Database Information

Version

The version of locate command can be displayed with --version option like below.

Version

Version

1 Response

  1. 13/05/2019

    […] locate Command Tutorial With Examples For Linux To Find Files […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)