LinuxCommandLibrary

loc

Find files by name

TLDR

Print lines of code in the current directory

$ loc
copy

Print lines of code in the target directory
$ loc [path/to/directory]
copy

Print lines of code with stats for individual files
$ loc --files
copy

Print lines of code without .gitignore (etc.) files (e.g. two -u flags will additionally count hidden files and dirs)
$ loc [[-u|--unrestricted]]
copy

SYNOPSIS

locate [OPTION]... PATTERN...

PARAMETERS

-b, --basename
    Match only the base name of a path.

-c, --count
    Only print a count of matching entries.

-d, --database DBPATH
    Use DBPATH instead of the default database (/var/lib/mlocate/mlocate.db).

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

-i, --ignore-case
    Ignore case distinctions when matching patterns.

-l, --limit NUM, -n NUM
    Limit output to NUM entries.

-m, --mmap
    Use memory-mapped I/O to read the database.

-0, --null
    Separate entries with NUL rather than newline.

-P, --nofollow
    Do not follow symbolic links when checking if files exist (implies -e).

-r, --regexp REGEXP
    Find entries matching REGEXP using basic regular expressions.

--regex
    Interpret all patterns as extended regular expressions.

-q, --quiet
    Report no error messages about reading databases.

-S, --statistics
    Print statistics about each locate database.

-V, --version
    Print version information and exit.

-w, --wholename
    Match only the whole path name.

DESCRIPTION

The `locate` command in Linux is used to find files by name. It's significantly faster than using `find` because it relies on a database (usually `/var/lib/mlocate/mlocate.db`) that contains an index of the file system. This database is updated periodically (usually by a cron job) using the `updatedb` command. Therefore, `locate` may not show recently created files until the database is updated. The results are often broader as it matches substrings; for precise matching, combine locate with other tools or consider find.

CAVEATS

The database used by `locate` is not updated in real-time. You must run `updatedb` (usually as root) to update the database. Output may include files that no longer exist or exclude recently created files.

DATABASE UPDATES

The `updatedb` command builds the database used by `locate`. It's typically run as a cron job, usually daily or weekly. You can manually run `updatedb` as root to immediately update the database after creating new files or directories.

SECURITY IMPLICATIONS

Because `locate` searches a database, it may show files that a user does not have permission to access, especially if using older implementations.
Mlocate, a common implementation, addresses this by only indexing files for users who can access them.

MATCHING

By default, locate searches for substrings. Using the `-b` option restricts matches to the base name of files. Regular expressions (-r or --regex) offer more precise control over matching.

HISTORY

The `locate` command has been a staple of Unix-like systems for quickly finding files by name. Its primary advantage over `find` is its speed, achieved by searching a pre-built database of file names. The original implementation and its variations have evolved over time, with `mlocate` being a popular and often default implementation known for its security enhancements (e.g., not indexing files with restricted permissions for users who don't have access). The core concept remains the same: efficient file name lookup through a background-updated database.

SEE ALSO

find(1), updatedb(1), grep(1)

Copied to clipboard