• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

locate Command Examples in Linux

by admin

There will be times that you create a file and forget wherein the directory structure you put it. There will also be times when you don’t know the exact location of files created by the system, applications, or other users. In Linux, you have several powerful tools for finding the files you’re looking for.

The locate Command

The locate command performs a quick search for any specified string in file names and paths stored in the mlocate database. This database must be updated regularly for the search to be effective. The results displayed may be restricted to files that users have permission to access or execute.

Syntax

The syntax of the locate command is:

# locate [options] {string}

locate Command Options

The locate command supports different options that enable you to make your search more effective. Some of the options are described in the table.

Option Used To
-r Search for file names using regular expressions.
-c Display only the number of matching entries found, rather than the file names.
-e Return only files that exist at the time of search.
-i Ignore the casing in file names or paths.
-n {number of entries} Return only the first few matches up to the specified number.

THE updatedb COMMAND

The updatedb command is used to build a database of files based on the /etc/ updatedb.conf file. This command is used to update the /var/lib/mlocate/mlocate.db database. The /etc/updatedb.conf file consists of the paths that should be excluded while building the database. To add a path that needs to be excluded while building the database, open the /etc/updatedb.conf file and, in the PRUNEPATH variable, specify the path that need not be included while building the database. For example, PRUNEPATH=”/etc” will exclude the /etc directory while building the database.

Though this is the default database searched by the locate command, there may be more databases containing file paths. If the database is not updated before performing a search, all files created after the last update will be excluded from the search.

locate Command Examples

1. To locate any file:

# locate file.txt 

2. To match only the basename against the pattern:

# locate -b file.txt
# locate --basename file.txt 

3. To get the counts for matching entries:

# locate -c file.txt
# locate --count file.txt

4. To replace the default database with given:

# locate -d 
# locate --database

5. To print the entries those exist at the time when locate was fired:

# locate -e filename
# locate --existing filename

6. To follow trailing symbolix links:

# locate -L text
# locate --follow text

7. To get the locate help:

# locate -h
# locate --help

8. To exit successfully after finding specified number of entries:

# locate -l 10 text
# locate -n 10 text
# locate --limit 10 text

9. To avoid following symbolic links:

# locate -P text
# locate --nofollow text
# locate -H text 

10. To seperate the output entries by ASCI NULL character:

# locate -0 text
# locate --null text 

11. To get the statistices about the read database:

# locate -S text
# locate --statistics text 

12. To suppress any errors if occurred:

# locate -q text
# locate --quiet text

13. To get the version info:

# locate -V
# locate --version

14. To match the whole pathname:

# locate -w text
# locate --wholename text

Filed Under: Linux

Some more articles you might also be interested in …

  1. CentOS / RHEL 5,6 : How to reinstall GRUB loader from rescue mode
  2. ctr: command not found
  3. How to Use Udev Rules to Create oracleasm Disks in CentOS/RHEL 8
  4. lvcreate Command Examples in Linux
  5. How to configure IPtables to open Ports in CentOS / RHEL
  6. touch Command Examples in Linux
  7. How to Verify if a Disk/Partition is in Use by Oracle ASM, was used by Oracle ASM or is never used by Oracle ASM
  8. Linux OS Service ‘kudzu’
  9. How to extract RPM package without installing it
  10. Archiving Failures with ORA-16038, ORA-19504, ORA-00312

You May Also Like

Primary Sidebar

Recent Posts

  • qm Command Examples in Linux
  • qm wait Command Examples in Linux
  • qm start Command Examples in Linux
  • qm snapshot Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright