LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Searching in Linux: locate vs. find (https://www.linuxquestions.org/questions/linux-newbie-8/searching-in-linux-locate-vs-find-877774/)

kienlarsen 04-29-2011 03:17 AM

Searching in Linux: locate vs. find
 
Hi, it's late night, and I'm on disk searching commands with my unixacademy DVD training. It primarily discusses two search tools: locate and find.
I understand the difference between two. What I'm asking is, I don't see "locate" to be much used as "find". I read few books and most of them discuss "find", but not "locate". I also searched on forums, most questions about "find".
What I'm asking: is "locate" a general mainstream or some kind of "exotic" command?

ButterflyMelissa 04-29-2011 03:28 AM

Quote:

What I'm asking: is "locate" a general mainstream or some kind of "exotic" command?
No, Find and Locate are two different things, the one (locate) uses a database to find stuff, after indexing the system, find just does an old search on the disk...

Quote:

Hi, it's late night, and I'm on disk searching commands with my unixacademy DVD training
Good luck with the training, and ... get some sleep too, will ya... ;)

Thor

grail 04-29-2011 05:06 AM

Well I am not so sure about locate, but find has a lot strength due to not only all the things it can do but also the exec options make it extremely powerful.
Also, locate is not generally considered standard fare on all distros.

vickyk 04-29-2011 05:23 AM

locate is useful when the database in which it has indexed all the files are up-to-date.

To update the database, use the command
updatedb

If the database is updated then locate will work faster than find command.

Note:
If you are running a "updatedb" after very long time then it will take some time to complete.

find on other hand is much more powerful and can be combined to search for very specific needs.

j1alu 04-29-2011 06:43 AM

I usually use "locate" (one reason is that "find" is above me). For me it works very well.
But it is from the same "family" of tools:
http://www.gnu.org/software/findutil...mono/find.html
So no, it is not exotic, but most people seem to prefer find.

kienlarsen 04-29-2011 11:37 AM

Hi guys, what I'been asking, is "locate" is generally accepted tool? I mean, I'm learning and IT IS ALL NEW TO ME. From my past Windows trainings I remember that various brands tend to present their own, proprietary solutions and push them for you to use. That is my only concern.

jthill 04-29-2011 01:29 PM

locate is one of the fundamental tools, beyond just 'accepted'.

So far as people pushing "brand" software regardless of quality, stop worrying. Shed a tear of relief, even: FOSS exists as a standing rebuke to that whole business model.

kienlarsen 04-30-2011 07:41 AM

Thanks to everyone!

ButterflyMelissa 04-30-2011 12:45 PM

Quote:

Thanks to everyone!
You're welcome!

kienlarsen 05-02-2011 11:14 AM

I've been experimenting a lot with "find" and I've got one question. It appears that find is really heavy on resources, at least on my laptop. I created a huge fake data and directory structure, with many thousands files just for sake of experimentation, and I run "find" against it. I also watch my system utilization with "top" is another terminal and there's clear "spike" in utilization when "find" runs.
My question is, is is always like that? Isn't it possible to index its known paths?

chrism01 05-02-2011 08:39 PM

Quote:

Isn't it possible to index its known paths?
that's what the updatedb/locate combo is for. NB: AFAIK, it's Linux thing; ie not avail on eg Solaris, HP-UX etc.
'find' OTOH is a basic Unix cmd from way back when. When updatedb is run, it does essentially the same thing as find, but stores the results. You cannot do that with find, unless you want to write your own version of 'updatedb/locate' :)

kienlarsen 05-03-2011 07:28 AM

I understand that, but "find" allows for setting conditional searches, by file size, date and many other. I don't see how it may be possible with "locate".

rizzy 05-03-2011 07:59 AM

Helpful thread, i didn't know the difference between the two commands.

How does whereis command differ from find and locate? Does it use updatedb? i read man page, but still don't get it 100%.

Code:


DESCRIPTION
      whereis locates source/binary and manuals sections for specified files.
      The  supplied  names  are first stripped of leading pathname components
      and any (single) trailing extension of the form .ext, for example,  .c.
      Prefixes  of  s.  resulting  from  use of source code control are also
      dealt with.  whereis then attempts to locate the desired program  in  a
      list of standard Linux places.


salasi 05-03-2011 08:35 AM

Quote:

Originally Posted by chrism01 (Post 4344550)
'find' OTOH is a basic Unix cmd from way back when. When updatedb is run, it does essentially the same thing as find, but stores the results.

What updatedb stores is just the paths to the files....no file sizes, modification times, etc, etc. So, there is a lot of stuff that you can do with find (as an example, finding files with a modification date that falls inside a particular date range) that you just can't do with locate.

On the other hand, locate is fast. Find, particularly if you are searching over a whole file system isn't, but it is, or can be, clever.

Even if locate doesn't directly do what you want, provided that you only need the file name to do whatever it is, you can often do it by feeding the output of locate through, eg, grep to filter the output more, and still be finished before find has really got going.

kienlarsen 05-03-2011 01:57 PM

Is there another omnipresent tool for conditional file search, one that offers as many options as "find", but as fast as "locate"? My Unix Academy training DVD teaches these two, or there's always a trade-off speed vs. flexibility?

TobiSGD 05-03-2011 02:16 PM

That wouldn't make much sense. If you would store all the options find can test files against in a database you would have to update that database constantly. find is able to find files that were last accessed n minutes ago with the -amin option. In fact, every time a file is accessed, created, deleted or changed, you would have to update the database. That is very resource-consuming, if not even practically impossible.

chrism01 05-03-2011 06:21 PM

Exactly; whatever tool you use, it first has to search for the info. With 'find' it does it in real time and is very flexible. With updatedb, this is normally run in cron at eg 4am and only stores filenames/locations.
Other search options are http://linux.die.net/man/1/which & http://linux.die.net/man/1/whereis.
They all have different expectations & limitations.
See the old quote about 'use the right tool for the job'. Best thing is to read up on all 4 options/cmds and decide when you'll use each one.

kienlarsen 05-04-2011 04:42 AM

Thanks everybody!

gnuzilla 08-03-2011 08:50 AM

Locate vs Find
 
I work as a Linux Systems Admin, so i use both very often.

When i know the filname or some part of it, and need to know where on the FileSystem something is, I use locate.
Its very fast, low resources is important on those over-shared systems.

When i need to get a list of files to do commands on, often when the parameter is not filname related, i use find.
I use this very often to change permissions.


Code:

#for just the directories from ./,#

 $ find ./ -type d | xargs -I '{}' chmod 755 '{}'

#or for just the files from ./,#

 $ find ./ -type f | xargs -I '{}' chmod 644 '{}'


if i want execute on something with a file extension (such as txt), i would not use find -name unless nessesary for multi-level directories.

Code:

#I prefer the much faster,#

 $ ls *.txt | xargs #COMMAND#

#or if ls somehow unavailable (it has happened, crazy i know)#

 $ echo *.txt | xargs #COMMAND#


so they actually serve very diffrent purposes for me.
i end up using locate much more often, a quick

Code:

#or if i need to remove unrelated listings
 $ locate #FILENAME# | grep -v #FILTEROUT# | less


Hope this helps,

TobiSGD 08-03-2011 04:13 PM

Quote:

Originally Posted by gnuzilla (Post 4432644)
if i want execute on something with a file extension (such as txt), i would not use find -name unless nessesary for multi-level directories.

[CODE]#I prefer the much faster,#

$ ls *.txt | xargs #COMMAND#

Just a side note, I would never rely on the output of ls, since its output is thought to be for humans. You may have issues here with filenames that contain spaces (which often happens when you work in heterogeneous networks, Windows and Linux mixed).


All times are GMT -5. The time now is 07:54 PM.