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/)

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 10:10 PM.