Which command in linux with example

12 Linux Which Command, Whatis Command, Whereis Command Examples

This Linux tutorial will explain the three “W” commands. The three “W”s are whatis, whereis and which commands.

You already know how to use find command to efficiently fo find a file. Now, these three W commands will help you to locate more stuff from Linux command line.

I. Linux whatis Command

Whatis command is helpful to get brief information about Linux commands or functions. Whatis command displays man page single line description for command that matches string passed as a command line argument to whatis command. Whatis command searches for string in its index databases which is maintained by mandb program. Whatis command picks short description of NAME section of man page of command that matches to input given to the whatis command. Whatis provides several command line options to help user in getting brief information of specific Linux commands as per their need or interest. Syntax:

$ whatis write write (1) - send a message to another user write (2) - write to a file descriptor

1. Get information from specific sections of man pages using -s option

If we want to get Linux command information from specific section of man pages, then we can provide sections list using “-s or —sections or –section” option. It will restrict whatis command to display brief information from specified man page section only.

$ whatis -s "1","2" open open (1) - start a program on a new virtual terminal (VT). open (2) - open and possibly create a file or device
$ whatis -s "2" open open (2) - open and possibly create a file or device

2. Search information through wild-cards using -w option

If we want to search Linux commands or functions information using wild card, then whatis command gives “-w or –wildcard” option. It will make your search specific as per user’s need.

$ whatis -w 'ab*' abort (3) - cause abnormal process termination abs (3) - compute the absolute value of an integer
$ whatis -w 'ab?' abs (3) - compute the absolute value of an integer

It displays brief information of Linux commands or functions which start from “ab” and followed by any single character.

3. Search information through regular expressions using -r option

If we want to search Linux commands or functions information using regular expressions, then whatis command gives “-r or –regex” option. It will give flexibility to customize your search for Linux commands or functions throughout the Linux system.

$ whatis -r '^ab' abort (3) - cause abnormal process termination abs (3) - compute the absolute value of an integer
$ whatis -r 'ab$' anacrontab (5) - configuration file for anacron baobab (1) - A graphical tool to analyse disk usage crontab (1) - maintain crontab files for individual users (Vixie Cron) crontab (5) - tables for driving cron fstab (5) - static information about the filesystems inittab (5) - init daemon configuration swab (3) - swap adjacent bytes tc-stab (8) - Generic size table manipulations

4. Disable trimmed output using -l option

Generally whatis command trims long output of Linux commands or functions information to avoid “Not good” output display on terminal that is going beyond screen. To allow whatis command to show complete output on screen, “-l or –long” option can be used.

$ whatis ssh-import-id ssh-import-id (1) - retrieve one or more public keys from a public keyserver (Launchpad.net by default) and append them to the current user's authorized_keys file (or some other specifie.
$ whatis -l ssh-import-id ssh-import-id (1) - retrieve one or more public keys from a public keyserver (Launchpad.net by default) and append them to the current user's authorized_keys file (or some other specified file)

5. Restrict search up to specified path using -M option

By default, whatis command uses $MANPATH environment variable. But whatis provides “-M or –manpath” option to restrict search up to specified path of man pages.

$ whatis -M /usr/share/man hexdump hexdump (1) - ASCII, decimal, hexadecimal, octal dump

It displays brief information of Linux hexdump command from man pages available at path /usr/share/man.

$ whatis -M /usr/man hexdump hexdump: nothing appropriate.

II. Linux whereis Command

Whereis command is helpful to locate binary, source and manual pages of commands in the Linux system. It is very simple utility and provides several options which are given below with examples. Syntax:

$ whereis open open: /bin/open /usr/share/man/man1/open.1.gz /usr/share/man/man2/open.2.gz

It locates binary, source and man pages of “open” command and here it displayed paths where binary, man pages of open command is available in the system.

Читайте также:  Linux cat dev null

6. Locate binaries using -b option

$ whereis -b whereis whereis: /usr/bin/whereis /usr/bin/X11/whereis

It locates binary of “whereis” command and displays paths where binary of command is available in the system.

7. Locate man pages for a command using -m option

$ whereis -m whereis whereis: /usr/share/man/man1/whereis.1.gz

It locates man page of “whereis” command and displays path where man page of command is available in the system.

8. Locate source of a command using -s option

$ whereis -s whereis whereis:

It locates source of “whereis” command, but source of “whereis” command does not exist in the system, so it did not display path for source of command in the system.

9. Locate unusual entries using -u option

This option is something different that searches for unusual entries. These entries are those command whose source, binary or man page does not exist in the system as per options “[-bms]” specified along with “–u”.

$ whereis -m -u wcgrep wcgrep:

It checks if specified command (i.e. wcgrep) man page does not exist in the system. Whereis command with options “-m and -u” locates for the commands in the system whose man page does not exist.

Here, whereis command with same options is applied on “grep” command whose man page exists in the system, so whereis returned nothing and exits normally.

10. Locate binaries in a specified path using -B option

If user wants to search for binary and wants to limit the scope of search for whereis command up to specified path, then use “-B” option.

$ whereis -B /bin -f for_loop for_loop: /bin/for_loop
$ whereis -B /usr -f open open: /usr/share/man/man1/open.1.gz /usr/share/man/man2/open.2.gz

If open command’s binary is not found at specified path, then it is not shown but whereis command by default searches for other types (i.e. man page and source) of specified command (i.e. open) and displays them if found.

Читайте также:  Kali linux создать root пользователя

11. Locate man pages with limited scope using -M option

If user wants to search for man pages and wants to limit the scope of search for whereis command up to specified path, then use “-M” option.

$ whereis -M /usr/share/man/man1 -f open open: /bin/open /usr/share/man/man1/open.1.gz $ whereis -M /usr/share/man/man2 -f open open: /bin/open /usr/share/man/man2/open.2.gz $ whereis -M /usr/share/man/man3 -f open open: /bin/open

Here, it is observed that whereis command is displaying man page of “open” command which is available in specified path only. But, whereis command by default searches for other types (i.e. binary and source) of specified command (i.e. open) and displays them if found.

III. Linux which Command

Which command is very small and simple command to locate executables in the system. It allows user to pass several command names as arguments to get their paths in the system. “which” commands searches the path of executable in system paths set in $PATH environment variable. Syntax:

$ which ls gdb open grep /bin/ls /usr/bin/gdb /bin/open /bin/grep

It locates command names – “ls”, “gdb”, “open” and “grep” specified as arguments to “which” command and displays paths of each executable where it exists in the system.

12. Display all the paths using -a option

Above will search display the executable “echo” from all paths set in $PATH environment variable and displays the first path where echo executable is found. It may be case that executable is placed at other paths of $PATH environment variable as well. To get all paths where executable is present in the system, “-a” option can be used.

$ which -a echo /usr/sbin/echo /bin/echo

Источник

How to Use the which Command in Linux

The which command allows users to search the list of paths in the $PATH environment variable and outputs the full path of the command specified as an argument. The command works by locating the executable file matching the given command.

In this tutorial, you will learn to use the which command.

How to use the which command in Linux, with examples.

Linux which Command Syntax and Options

The syntax for the which command is:

The [argument] variable specifies the command or commands you want to find.

For example, the following command outputs the location of the cat command:

Find the location of the cat command executable file using which.

The which command has only one option, -a . It is optional and used to print all the matches it finds.

The command searches for matches from left to right. If there are multiple matches found in the directories listed in $PATH , which prints only the first one. The -a option instructs which to print all the matches.

Читайте также:  Networking mac and linux

Important: On many Linux distributions, which excludes the shell built-in commands and does not output their location.

List all instances of a command using which.

Having multiple matches sometimes means one match is a symlink to the other. However, it is possible to have two versions of the same command in different locations or two different commands using the same name.

Note: Unlike many other commands, which has no —help option. To see the command description and help, run man which .

Exit Status

The which command returns one of the following values that indicate its exit status:

  • 0 . All arguments were found and executable.
  • 1 . One or more arguments don’t exist or aren’t executable.
  • 2 . An invalid option has been specified.

Linux which Command Examples

The following examples showcase how the which command works and how to use the available option.

1. Display the Path of Any Executable File

To display the path of any command, pass the command name as an argument after which .

Find the tr command executable with which.

The output shows the path to the tr command executable file, located in /usr/bin/tr.

2. Display Multiple Paths of Executable Files

which accepts multiple arguments and outputs the path to each one in the specified order.

Find the paths to multiple commands using which.

The command works through the supplied list and outputs the results for the nc command, mount command, and sort command, separating each result with a newline character.

3. List All Instances

which only shows the first match it finds in the $PATH variable directory list. Use the -a option to show every match for the specified command.

For example, searching for instances of the less command outputs two results when using the -a option:

Display the path to all instances of a command.

Use the ls command to check file details and determine if both versions are executable files. Run:

ls -lh /usr/bin/less ls -lh /bin/less

Check the details of a command

The output shows two identical versions of the same command in two locations, both 176 KB large, and both executable.

Note: The /bin directory contains executables that can be used by the system administrator and any other user, and which are required for emergency system repairs. The /usr/bin directory is the primary directory for executable commands on the system.

Using the -a option lists all the paths containing an instance of the specified program. While multiple versions of the same program can exist on a system, sometimes one of the instances is only a symbolic link and not a binary file.

For example, running the following command outputs two instances of the atq command:

Find all instances of the atq command.

Again, use the ls command to check the details for both files. Run:

ls -lh /usr/bin/atq ls -lh /bin/atq

Finding symbolic links using the ls command.

The output shows that both files are symbolic links ( -> ) only 2 bytes large and pointing to the at command.

5. Exclude Shell Built-ins

As previously mentioned, the which command excludes shell built-ins from its output.

For example, asking for the location of the read and man commands only outputs the location for the man command executable file, as read is a bash shell command.

The which command excluding a shell built-in from its output.

This tutorial showed how to use the which command in Linux to find the path to a command’s executable binary. See and download our Linux commands cheat sheet for other essential Linux commands and examples of using them.

Источник

Оцените статью
Adblock
detector