How To Find Files In Linux Using ‘locate’
- 2
- Add a Comment
- No Related Post
The simplest way to find files under Linux is to use the locate program. This article explains how to use the locate tool to find your files easily. When you run locate for the first time, you might get an error message. To get it working, refer to the note at the bottom of the page.
Procedure:
Here are various ways to use locate tool
$ locate index.html
It would produce a list of the locations where you could find files that are named index.html. This might produce results as follows:
/home/pamela/index.html
/usr/local/games/pam/index.html
You could use the -q option to suppress error messages. Error messages would typically be messages stating that permission to access files was not given since you are only a user (not superuser). The -q option would suppress any other error messages, as well.
$ locate “*.dat” -q
You could use the -n option to limit the number of returned results to a specific number. You could ask for only ten search results by the following command:
$ locate “*.c” -n 10
This would return the first ten files that end in .c that Linux finds.
You could use the -i option in case you want to perform a case insensitive search. The case of the filenames would not be considered.
$ locate INDEX.HTML -i
You could make your search faster by typing the following:
$ locate index.html -l 0
Typing -l 1 takes longer for the search to complete, but is more secure. This is the default action that takes place when the -l option is not mentioned.
There are a few more parameters that can be used with locate, but they are generally not used. You can find them in the man pages.
Note : When you run ‘locate’ for the first time, you will get a message stating that the db is not available. Linux requires a database of all the files that locate uses to find the files. The message would also state that you can log in as root and run a particular command. Simply doing so would get locate to work perfectly. To change to the superuser mode, type ’su’ at the prompt. Then type the command you were asked to run (as indicated in the message). Type Ctrl+D to exit the superuser mode. Now you can continue with locate commands as normal.
[Source]

2 Comments
Thomas
November 24th, 2008
at 1:57am
Good post.Thank you.
Thomas Colliers
December 24th, 2008
at 6:08pm
Updating the database used by the locate command can be done running ‘updatedb’ as root.