[Tutorial] How to locate files in Linux with the locate command

June 12, 2016

No Comments

One way to speed up searches for files is not to search your file directories! You can do this by using a program like locate, which uses a single database of filenames and locations, and which saves time by looking in a single file instead of traversing your hard drive. Finding a file using locate is much faster than the find command because locate will go directly to the database file, find any matching filenames, and print its results. Locate is easy to use. For example, to find all the PostScript files on your system, you can enter# locate *.ps Almost instantly, the files are printed to your screen. You may even find the locate command line a little easier to use than the find command. However, there is a catch: find will work “right out of the box,” whereas with locate, you need to first build a database of all the files on your system. But don’t worry, because the procedure is almost automatic.

After you install Linux, locate can’t show any search results because it can’t locate its database. To create this database, you’ll need to use the updatedb command. Make sure you’re logged in as the root operator (or use the su command; see Hour 20, “Basic System Administration”). At the prompt, enter# updatedb It may take a minute or so for the updatedb command to finish its work, but when it’s done, locate’s database, locatedb (about 300,000 characters in size for 400MB worth of files), will reside in the /var/lib directory. The only downside to using the locate command is that after time, its database could become out of date as you add or delete files to your system. However, you can have its database u