Linux Commands Examples

A great documentation place for Linux commands

locate

find files by name


see also : updatedb

Synopsis

locate [OPTION]... PATTERN...


add an example, a script, a trick and tips

: email address (won't be displayed)
: name

Step 2

Thanks for this example ! - It will be moderated and published shortly.

Feel free to post other examples
Oops ! There is a tiny cockup. A damn 404 cockup. Please contact the loosy team who maintains and develops this wonderful site by clicking in the mighty feedback button on the side of the page. Say what happened. Thanks!

examples

0

To search for a file named exactly NAME (not *NAME*), use

locate -b ’\NAME

Because \ is a globbing character, this disables the implicit replacement of NAME by *NAME*.


0
source
            
locate "*/$1"
0
source
            
locate "$@"|grep -v media
0
source

What's the equivalent of Linux's updatedb command for the Mac?

It's locate.updatedb on Mac.

sudo /usr/libexec/locate.updatedb

For more information see the locate.updatedb man page.

0
source

How to use locate in cygwin

I think the command you want is find

Do man find to see the syntax and options

The locate command runs against a pre-built database of filenames

0
source

Faster alternatives to "find" and "locate"?

Searching for source files in a project

Use a simpler command

Generally, source for a project is likely to be in one place, perhaps in a few subdirectories nested no more than two or three deep, so you can use a (possibly) faster command such as

(cd /path/to/project; ls *.c */*.c */*/*.c)

Make use of project metadata

In a C project you'd typically have a Makefile. In other projects you may have something similar. These can be a fast way to extract a list of files (and their locations) write a script that makes use of this information to locate files. I have a "sources" script so that I can write commands like grep variable $(sources programname).

Speeding up find

Search fewer places, instead of find / … use find /path/to/project … where possible. Simplify the selection criteria as much as possible. Use pipelines to defer some selection criteria if that is more efficient.

Speeding up locate

Ensure it is indexing the locations you are interested in. Read the man page and make use of whatever options are appropriate to your task.

   -U <dir>
          Create slocate database starting at path <dir>.

   -d <path>
          --database=<path> Specifies the path of databases to search  in.


   -l <level>
          Security  level.   0  turns  security checks off. This will make
          searchs faster.   1  turns  security  checks  on.  This  is  the
          default.

Remove the need for searching

Maybe you are searching because you have forgotten where something is or were not told. In the former case, write notes (documentation), in the latter, ask? Conventions, standards and consistency can help a lot.

0
source

How to run updatedb, excluding some paths, but not removing previous indexed content for those paths?

Create several indexes

The key to this answer is that mlocate allows the user to search several database files. If you take this into consideration, then having complex options for excluding or including files from certain paths depending on whether or not the filesystem is mounted becomes less important.

The provided example can be adopted to your situation with little effort. Generally speaking, you can create several index files with updatedb and update them simultaneously or one by one as frequently you want (via cron, for example).

If there is a global /etc/updatedb.conf, then it is probably wise to exclude paths that will have their own indexes, because otherwise you will get double results when searching all indexes as suggested in the example.

When all as many indexes as required have been created, proceed with a shell function such as shown below to automatically include all indexes when doing a search:

function locate {
    /usr/bin/locate \
      -d /var/tmp/default.mlocate.db \
      -d /my-stuff/mlocate-index2.db $@ 
}

Relevant documentation

(do a man updatedb too)

man locate *scroll scroll scroll*

-d, --database DBPATH
  Replace the default database with DBPATH. DBPATH is a :-separated list of
  database file names. If more than one --database option is specified, the
  resulting path is a concatenation of the separate paths.

  An empty database file name is replaced by the default database. A database
  file name - refers to the standard input. Note that a database can be read 
  from the standard input only once.

Example

# updatedb -o /home/jaroslav/.locate/media-music.db -U /mnt/media/media/ \
    -n images \
    -n movies \
    -n steamapps \
    -n pr0n -v

# locate -i glass -d /home/jaroslav/.locate/media-music.db| wc -l
35
# locate -i glass -d /home/jaroslav/.locate/media-music.db \
                  -d /var/lib/mlocate/mlocate.db  | wc -l
363
0
source

`locate` wildcard strange behavior - why?

locate "test.*" doesn't return anything, but there are files named test in my system.

. is treated as dot, not as in regex's as an arbitrary character, so test.* does not match test, but test.foo.

locate "test*" doesn't return anything, but there are files starting with test in my system.

locate stores the full path to the file, so to find files starting with test, you should use locate "*/test*".

The last point might be confusing, as locate foo finds anything including foo, so the pattern gets interpreted as *foo*. It seems that the pattern is not enclosed in stars, if there is already one wildcard in the pattern.

Disclaimer: I did some test and these are my conclusions, I cannot prove them by citing the man page, which seems very rudimentary.

0
source

Problems with locate

locate uses a database to know which files are where. This database is normally updated nightly as a cron job.

You can manually update it with updatedb

0
source

how to use updatedb command as an ordinary user on linux?

You can just create database in home with -o argument of updatedb:

updatedb -o ~/.locate.db

And use it with slocate like this:

slocate --database=~/.locate.db <pattern>

You probably want to define an alias for slocate --database=~/.locate.db.

0
source

locate/updatedb like tool (Linux CLI) for CDs/DVDs

You can use updatedb to have it parse the removable media paths, just configure accordingly the PRUNEPATHS and PRUNEFS environment variables in /etc/updatedb.conf or equivalent. Although this will only remember the path so if you mount in the same directory a different media and have updatedb run, it'll overwrite (or append?) the files.

You can workaround this by mounting each of your CDs to be catalogued in its own directory, not very hard, but a bit of a nuisance.

0
source

How can I make locate/updatedb ignore certain file extensions?

In updatedb.conf, uncomment the PRUNENAMES line and add the extension .pyc. On my Ubuntu system by default it reads:

# PRUNENAMES=".git .bzr .hg .svn"

Change it to

PRUNENAMES=".git .bzr .hg .svn .pyc"

0
source

How to display file details (size, date, etc.) from Linux “locate” command?

Store the result of locate in a variable, check to see if it has any value, and then view that.

f=`locate ...`
[ "$f" ] && ls -al "$f"

The reason you can't get this information from locate directly is because it's not in the database that locate uses.

0
source

Linux Locate Command false info

The cache for locate is updated by updatedb. On most distributions, it's run by cron every day or week. After huge changes, you can run it manually, too.

0
source

linux locate command sort by date

How about:

ls -td $(locate something)

or

ls -td1 $(locate something)

0
source

Finding files on mounted disk with locate

As @DanD explainedin his comment, /etc/updatedb.conf contains settings for what paths and file systems to prune (not index). Indeed I found my mount point among those paths, and upon removing it everything worked fine.

description

locate reads one or more databases prepared by updatedb(8) and writes file names matching at least one of the PATTERNs to standard output, one per line.

If --regex is not specified, PATTERNs can contain globbing characters. If any PATTERN contains no globbing characters, locate behaves as if the pattern were *PATTERN*.

By default, locate does not check whether files found in database still exist (but it does require all parent directories to exist if the database was built with --require-visibility no). locate can never report files created after the most recent update of the relevant database.

options

-b, --basename

Match only the base name against the specified patterns. This is the opposite of --wholename.

-c, --count

Instead of writing file names on standard output, write the number of matching entries only.

-d, --database DBPATH

Replace the default database with DBPATH. DBPATH is a :-separated list of database file names. If more than one --database option is specified, the resulting path is a concatenation of the separate paths.

An empty database file name is replaced by the default database. A database file name - refers to the standard input. Note that a database can be read from the standard input only once.

-e, --existing

Print only entries that refer to files existing at the time locate is run.

-L, --follow

When checking whether files exist (if the --existing option is specified), follow trailing symbolic links. This causes broken symbolic links to be omitted from the output.

This is the default behavior. The opposite can be specified using --nofollow.

-h, --help

Write a summary of the available options to standard output and exit successfully.

-i, --ignore-case

Ignore case distinctions when matching patterns.

-l, --limit, -n LIMIT

Exit successfully after finding LIMIT entries. If the --count option is specified, the resulting count is also limited to LIMIT.

-m, --mmap

Ignored, for compatibility with BSD and GNU locate.

-P, --nofollow, -H

When checking whether files exist (if the --existing option is specified), do not follow trailing symbolic links. This causes broken symbolic links to be reported like other files.

This is the opposite of --follow.

-0, --null

Separate the entries on output using the ASCII NUL character instead of writing each entry on a separate line. This option is designed for interoperability with the --null option of GNU xargs(1).

-S, --statistics

Write statistics about each read database to standard output instead of searching for files and exit successfully.

-q, --quiet

Write no messages about errors encountered while reading and processing databases.

-r, --regexp REGEXP

Search for a basic regexp REGEXP. No PATTERNs are allowed if this option is used, but this option can be specified multiple times.

--regex

Interpret all PATTERNs as extended regexps.

-s, --stdio

Ignored, for compatibility with BSD and GNU locate.

-V, --version

Write information about the version and license of locate on standard output and exit successfully.

-w, --wholename

Match only the whole path name against the specified patterns.

This is the default behavior. The opposite can be specified using --basename.

environment

LOCATE_PATH

Path to additional databases, added after the default database or the databases specified using the --database option.

exit status

locate exits with status 0 if any match was found or if locate was invoked with one of the --limit 0, --help, --statistics or --version options. If no match was found or a fatal error was encountered, locate exits with status 1.

Errors encountered while reading a database are not fatal, search continues in other specified databases, if any.

files

/var/lib/mlocate/mlocate.db

The database searched by default.

notes

The order in which the requested databases are processed is unspecified, which allows locate to reorder the database path for security reasons.

locate attempts to be compatible to slocate (without the options used for creating databases) and GNU locate, in that order. This is the reason for the impractical default --follow option and for the confusing set of --regex and --regexp options.

The short spelling of the -r option is incompatible to GNU locate, where it corresponds to the --regex option. Use the long option names to avoid confusion.

The LOCATE_PATH environment variable replaces the default database in BSD and GNU locate, but it is added to other databases in this implementation and slocate.


see also

updatedb


author

Miloslav Trmac <mitr[:at:]redhat[:dot:]com>

How can this site be more helpful to YOU ?


give  feedback