LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   locate and find (https://www.linuxquestions.org/questions/linux-newbie-8/locate-and-find-4175455091/)

thirstonlinux 03-22-2013 01:31 AM

locate and find
 
May I know the exact difference between locate and find command

jschiwal 03-22-2013 02:26 AM

Locate looks up the location of a file in a database. The database is normally updated nightly in a cron job. Newer files won't be listed. Often, locate is configured to scan system directories and not your personal files.

The find command scans directories looking for files that match the criterions you gave.
You can find files by type, size, date or name pattern. You can execute commands on the results, or print the results in different formats. You can even generate a bash script using -printf. I've done this to add " around the file names.

The info manual for find "finding files" is pretty good. I'd recommend reading through it.

kooru 03-22-2013 03:23 AM

LQ wiki can be useful

Madhu Desai 03-22-2013 03:38 AM

Aonther difference between locate and find is, locate matches the pattern even if it is in its absolute path, which find does not do. this will greatly shortlist the results.

for ex:

# locate *abcd*
/root/abcd.text
/var/ftp/pub/abcd
/var/ftp/pub/abcd/configs
/var/ftp/pub/abcd/others
/var/ftp/pub/abcd/configs/base.config
/var/ftp/pub/abcd/configs/named.config
/var/ftp/pub/abcd/configs/repo.config


# find -iname "*abcd*" 2> /dev/null
./root/abcd.text
./var/ftp/pub/abcd


to update database manually you can issue
# updatedb

shivaa 03-22-2013 04:56 AM

In simple words, locate searches for file within the system database, not personal ones, as:
Code:

~$ locate .profile
/etc/news/.profile
/usr/share/apps/profiles/klauncher.profile.xml
/usr/share/apps/profiles/konqueror.profile.xml
/usr/share/apps/profiles/noatun.profile.xml
(Truncated)

Whereas find cmd can look for any file either personal or system's configuration file. Find is more versatile and options rich than locate.
Code:

~$ find . -name ".profile" -print
./.profile


Madhu Desai 03-22-2013 06:07 AM

Quote:

In simple words, locate searches for file within the system database, not personal ones
No. it will search for all files including personal ones. when you create a new file (personal) and if it is not shown in locate, then that file has not been updated in database yet. it will get updated in next boot, or you can update manually by 'updatedb' command.

What i understand is, linux uses that database to locate for files for internal use. you use 'locate' just to see files -- say, what its like through the eyes of linux. whereas 'find' is for users to search according to their convenience and needs.

Hope i'm correct.:scratch:

salasi 03-22-2013 09:53 AM

There have been threads before on this subject, eg,
http://www.linuxquestions.org/questi...s-find-877774/
http://www.linuxquestions.org/questi...ommand-879604/
http://www.linuxquestions.org/questi...-files-937265/

I am unclear whether you looked at these threads before asking this question. In general, as well as previous threads, the man pages contain many of the answers to this type of question, although sometimes they are not the easiest of reading.

So, do you still have specific questions?

theNbomr 03-22-2013 05:13 PM

Quote:

Originally Posted by mddesai (Post 4916469)
What i understand is, linux uses that database to locate for files for internal use. you use 'locate' just to see files -- say, what its like through the eyes of linux. whereas 'find' is for users to search according to their convenience and needs.

Hope i'm correct.:scratch:

Sorry to say, you are not correct. Both tools are for users to locate specific or non-specific things in the filesystem. The locate/updatedb combination composes a database that can be quickly accessed to locate files and directories that match some specified pattern. It only searches a database, which makes it faster. In contrast, find does a live search of the filesystem, reporting files and directories that match a fairly complex set of possible constraints. Since it searches a live filesystem, it will not ever be out-of-date. It can also be faster, if the directory tree that it searches is small, or if the '-maxdepth' option is specified.
Neither system is used 'internally' in any rigorous sense (although there may be some use of find in system scripting). The kernel and other critical systems will be quite happy without locate.
find is very much optimized to work as the engine of a filesystem traversal script. It's rich set of filtering parameters makes it well suited to composing lists of files, and its output integrates well with standard scripting idiom.

--- rod.

jschiwal 03-24-2013 12:55 AM

Quote:

Originally Posted by mddesai (Post 4916469)
No. it will search for all files including personal ones. when you create a new file (personal) and if it is not shown in locate, then that file has not been updated in database yet. it will get updated in next boot, or you can update manually by 'updatedb' command.

What i understand is, linux uses that database to locate for files for internal use. you use 'locate' just to see files -- say, what its like through the eyes of linux. whereas 'find' is for users to search according to their convenience and needs.

Hope i'm correct.:scratch:

Most often the user nobody is used to scan the filesystem. The nobody user won't have access to your home directory. You can configure locate to include your personal files where another instance of updatedb runs as your user, keeping a private database. Or you could configure updatedb to be run as root. The latter option is usually configured in an /etc/sysconfig/ file.

There are hardly any similarities between locate and find. Find is much more flexible and scans the file system. Locate simply looks up values in its database.


All times are GMT -5. The time now is 06:25 AM.