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