HOWTO: Find Files Using Locate
A reader wrote in and asked for more tutorial-type posts. I’m nothing if not mildly-non-apathetic, so I’ll kind of oblige. I say “kind of” because I purposely didn’t create a tutorial category when I formed New Linux User because I don’t think tutorials are particularily suited for blogs. I’m much more likely to find good tutorials and link to them instead. Lazy? Possibly…
To kick off my non-tutorial series, I’ve selected a command from the list and today I present to you the locate command. I actually use locate and its brother find quite a bit so this is a good topic.
Your friendly Linux box keeps a running database of the files on your system. Using locate tells your system to search its database for some filespec that you’ve specified. For example:
locate *.html
will turn up a list of all the files on your machine ending with .html in directories that you have permissions to.
You can limit your search to a set number of results:
locate *.html -n 10
will only return the first 10 files ending in .html in directories that you have permissions to.
As with all good things - there is a catch. The database that locate uses must be up to date. The first time you run the command, your system will likely tell you that there is no database and you need to create it. To create the database, most systems use the updatebd command run as root.
It’s also likely that your database will get out of date from time to time and you will have to update it to see your most recent files. Here’s a snippet from my purplepodcaster.com site where I am searching for a new video I just uploaded (wget.asf), but am unable to find it until I update the database:
jonzbox:/var/www/thelinuxlearningstation.com/videos# locate asf
/usr/include/linux/sdlasfm.h
jonzbox:/var/www/thelinuxlearningstation.com/videos# updatedb
jonzbox:/var/www/thelinuxlearningstation.com/videos# locate asf
/usr/include/linux/sdlasfm.h
/var/www/thelinuxlearningstation.com/videos/wget.asf
jonzbox:/var/www/thelinuxlearningstation.com/videos#
As with all Linux commands, locate has a lot of switches and options. A pretty decent list (and aforementioned tutorial) can be found on Code Coffee here.
Related Stories
POSTED IN: How To
0 opinions for HOWTO: Find Files Using Locate
No one has left a comment yet. You know what this means, right? You could be first!
Have an opinion? Leave a comment: