Specific use of Linux which command

Specific use of Linux which command

We often want to find a file in Linux, but we don't know where it is. We can use the following commands to search:

  • which View the location of the executable file.
  • whereis checks the location of the file.
  • locate cooperates with the database to view the file location.
  • find actually searches the hard disk for the file name.

01. Command Overview

Find files in environment variables

The which command is used to find and display the absolute path of a given command. The environment variable PATH stores the directories that need to be traversed when searching for commands. The which command searches for matching files in the directories set by the environment variable $PATH. In other words, by using the which command, you can see whether a system command exists and where the command is executed.

02. Command format

which [選項] 執行文件名[…]

03. Common options

Search for files in environment variables -a Search all contents instead of the first file -n <file name length> Specify the file name length. The specified length must be greater than or equal to the longest file name among all files. 
-p <filename length> is the same as the -n parameter, but the <filename length> here includes the file path. 
-w specifies the width of the output columns. 
-V Display version information.

--version, -[vV] show version information and exit --help show this help message and exit --skip-dot skip directories in PATH that begin with a dot --skip-tilde skip directories in PATH that begin with a tilde --show-dot do not expand dot to current directory in output --show-tilde print tilde for HOME directory (non-root) --tty-only if not on a tty, stop processing options to the right --all, -a print all matches in PATH, not just the first --read-alias, -i read alias list from stdin --skip-alias ignore option --read-alias; do not read stdin
--read-functions Read shell functions from stdin --skip-functions Ignore option --read-functions; do not read stdin

04. Reference examples

4.1 Display command path

[deng@localhost test]$ which bash
/usr/bin/bash

Description: which searches for executable files based on the directories in the PATH variable configured by the user! Therefore, different PATH configuration contents may find different commands.

4.2 Display command aliases

[deng@localhost test]$ which which
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
    /usr/bin/alias
    /usr/bin/which
[deng@localhost test]$ 

4.3 Ordinary users and root users have different search paths

Common user search scenarios

[deng@localhost test]$ which pwd
/usr/bin/pwd

Root user search scenario

[root@localhost ~]# which pwd
/bin/pwd
[root@localhost ~]#

4.4 Cannot find built-in commands

[root@localhost ~]# which type
/usr/bin/which: no type in (/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
[root@localhost ~]#

This is the end of this article about the specific usage of the Linux which command. For more relevant Linux which content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • The difference between Linux command search commands whereis and which

<<:  Echarts tutorial on how to implement tree charts

>>:  Detailed explanation of common commands in MySQL 8.0+

Recommend

Master the CSS property display:flow-root declaration in one article

byzhangxinxu from https://www.zhangxinxu.com/word...

The process of building lamp architecture through docker container

Table of contents 1. Pull the centos image 2. Bui...

Discussion on default margin and padding values ​​of common elements

Today we discussed the issue of what the margin v...

Teach you to implement a simple promise step by step

Table of contents Step 1: Build the framework Ste...

How to implement Docker container self-start

Container auto-start Docker provides a restart po...

Distinguishing between Linux hard links and soft links

In Linux, there are two types of file connections...

A simple example of mysql searching for data within N kilometers

According to the coefficient of pi and the radius...

Perfect solution to the problem of webpack packaging css background image path

Inside the style tag of the vue component, there ...

In-depth understanding of the vertical-align property and baseline issues in CSS

vertical-align attribute is mainly used to change...

MySQL5.7.21 decompressed version installation detailed tutorial diagram

Since I often install the system, I have to reins...

How to install Elasticsearch7.6 cluster in docker and set password

Table of contents Some basic configuration About ...

Solution to the garbled problem of web pages when the encoding is set to utf-8

Recently, when I was writing web pages with PHP, I...

Summary of tips for making web pages

Preface This article mainly summarizes some of th...