LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 07-07-2010, 07:41 AM   #1
skaushal_lk
LQ Newbie
 
Registered: Jul 2010
Distribution: Ubuntu, CentOS
Posts: 27

Rep: Reputation: 4
locate cmd couldnt search file but find did


Hi All,

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.

Its gr8 to be in LQ!

Thanks,
Sk
 
Old 07-07-2010, 07:44 AM   #2
zordrak
Member
 
Registered: Feb 2008
Distribution: Slackware
Posts: 595

Rep: Reputation: 116Reputation: 116
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.
 
Old 07-07-2010, 07:50 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
When was the file created?

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.
 
Old 07-07-2010, 08:05 AM   #4
skaushal_lk
LQ Newbie
 
Registered: Jul 2010
Distribution: Ubuntu, CentOS
Posts: 27

Original Poster
Rep: Reputation: 4
Thanks Zordak and David for reply.

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.
 
Old 07-07-2010, 08:16 AM   #5
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
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).
 
Old 07-07-2010, 08:28 AM   #6
skaushal_lk
LQ Newbie
 
Registered: Jul 2010
Distribution: Ubuntu, CentOS
Posts: 27

Original Poster
Rep: Reputation: 4
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?
 
Old 07-07-2010, 09:08 AM   #7
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by skaushal_lk View Post
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.
 
Old 07-07-2010, 09:24 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.

Code:
find . -path ./definitely-not-here -prune -o -name "foobar.txt" -print
-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.
 
Old 07-08-2010, 06:44 AM   #9
skaushal_lk
LQ Newbie
 
Registered: Jul 2010
Distribution: Ubuntu, CentOS
Posts: 27

Original Poster
Rep: Reputation: 4
Today I tried with the locate cmd and yes it was able to locate the file abc.txt.
So DB was not updated.

Thanks everyone for quick response!

~SK
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
couldnt locate vmscan.c in linux/mm/ sumit deshmukh Linux - Newbie 1 04-07-2010 06:08 PM
Find/locate an exact file stefaandk Linux - General 2 12-27-2005 07:56 AM
determine whether 'find' cmd found the file or not mufy Linux - General 7 03-16-2005 07:24 AM
Find File broken, need search utility, where does WineX install, KDE file roller? Ohmn Mandriva 6 07-05-2004 10:34 PM
No 'locate' cmd in SuSE 9.0?? pH* Linux - Newbie 12 01-18-2004 01:08 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration