[SOLVED] locate cmd couldnt search file but find did
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I used locate cmd to search file abc.txt (for example) as I didn't know in which directory it would be present and couldn't find it. But when I did find . -name abc.txt, it traced the file, luckily the file was in the same directory.
I am wondering how find was able to search abc.txt and locate not. How these two are behavinb differently?
Usually I use locate when I don't know the path to the parent dir.
locate uses a database that has to be updated (usually automated at certain times). It is also usually configured by default to omit certain direcctories from the scan.
find works as you would expect by actively scanning the filesystem at search-time looking for files that meet the specified criteria.
Locate uses a database file that needs to be updated periodically. That's why it's able to give you your results so quickly. But any files created after the last update will not be in the index. Find, on the other hand, scans the file tree each time it's called.
The locate "updatedb" command is usually configured to run from a cron job once or twice a day.
I created the file maybe an half hour back before using the locate search. So as per your comments I think the DB was not updated when I did the search. Maybe I will try to use locate tomorrow and verify it.
There's the chance that your system is not configured to run updatedb automatically, though. In that rare case you won't see anything tomorrow either.
You could run updatedb as root manually, or configure cron to run it (cron must be running of course).
The thing with these tools is speed vs. reliability. locate is quite fast, since it doesn't actually scan your fs to look for a file. However the data it provides might not be accurate. locate could list files that no longer exist, and it can omit files that have been created recently. If a file has been moved to another folder it will be reported on the wrong location by locate.
find will always work, but it has to look inside the fs, and that takes quite a lot of time, overall if you do recursive searches on large directory trees and you don't specify a max depth (or it's too big).
I am thinking for scenario where I don't know where to search for the file abc.txt then how to go about searching that file without locate. I can think of using find cmd:
1) search in the current working directory
2) search around the dir present near working directly
I am thinking for scenario where I don't know where to search for the file abc.txt then how to go about searching that file without locate. I can think of using find cmd:
1) search in the current working directory
2) search around the dir present near working directly
Any better approaches?
If you need accurate results you can either:
1.- run updatedb, then use locate
2.- use find, it can do *anything*, but you need to read the man page and learn how to use it
3.- use any of the tools that modern desktops have for this purpose, like beagle, strigi, etc. These use a similar approach to locate, but they should use inotify which means that -at least theoretically- the database should stay up to date. I haven't ever used these, never felt the need to.
If locate's database isn't up-to-date, then you're pretty much limited to fnd or a similar search tool to find what you want. Note that you can simply run "updatedb" any time you want, but it usually takes several minutes to complete. Not to mention that it needs to be run as root if you want to index the whole system.
You might however configure a personal updatedb command to index only the files in a specific tree, such as your home directory, and corresponding locate command to access it. This should update faster than your whole-system db. Take a look at the locate and updatedb man pages to learn how to tweak them to your specific needs.
As for find, I'm not sure if I'm imagining it, but I've noticed that it often takes less time to bring up its hits after the first time it's run on a directory tree, so I'm guessing it caches the results temporarily, or at least they remain in memory for it to use again.
So assuming I'm correct I'd say a reasonable technique for locating a file would be to first search the current directory, then if it doesn't turn up, cd up one level and try again. The previous searched parts should complete quickly, and only the expanded parts of the tree would need to be searched in detail.
Code:
find . -name "foobar.txt" -print
cd ..
find . -name "foobar.txt" -print
cd ..
etc...
Another way to narrow down the search is to use the -prune option, to tell find to ignore areas you know it couldn't be in.
-prune will force it to ignore all paths matched by the commands in front of it, then the -o (or) will print any files matching the following tests in the rest of the search area.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.