locate vs type vs which vs whereis#

Comparison of different methods to locate commands and files in Linux.

Comparison#

  • type: The most reliable way to determine what the shell will execute. Since it is a shell built-in, it identifies shell built-ins, aliases, and functions that external tools miss.
  • which: Locates an executable in the user’s PATH. It does not show shell built-ins or aliases by default.
  • whereis: Locates the binary, source, and manual page files for a command. It searches a predefined list of standard directories.
  • locate: Searches a pre-built database (mlocate.db) of the entire filesystem. It is extremely fast but may not find recently created files until the database is updated (typically via updatedb).

Usage#

type#

  • -a: Display all locations containing an executable named <name>.
  • -t: Print a single word describing the type of the command (eg. alias, keyword, function, builtin, file).

locate#

  • -i: Ignore case distinctions.
  • -r: Search using a basic regular expression.

Examples#

  • Identify command type:
    $ type cd
    cd is a shell builtin
  • Show all locations:
    $ type -a ls
    ls is aliased to `ls --color=auto'
    ls is /usr/bin/ls
  • Find binary and man page:
    $ whereis grep
    grep: /usr/bin/grep /usr/share/man/man1/grep.1.gz
  • Locate all files with a specific name:
    $ locate -i <filename>