当前位置: 移动技术网 > 科技>操作系统>Linux > 命令which, whereis, who, w的用法

命令which, whereis, who, w的用法

2018年12月04日  | 移动技术网科技  | 我要评论

开始

命令搜索的顺序

  1. 在shell function中查找,有则调用,无则下一步;
  2. 判断命令是否为bash内置命令,有则调用,无则下一步;
  3. 在$path中搜索该命令,有则调用,无则报错。

判断命令类型

bash中有一个内置命令叫做type,可用于判断命令的类型(内置命令或者外部命令)。内置命令就是bash内置的、自带的,外部命令一般是由程序包提供。
命令结果中显示shell builtin的话表示shell内置命令,显示命令为某个路径下的文件时,为外部命令。

[root@c7 ~]# type kill
kill is a shell builtin
[root@c7 ~]# type lvcreate
lvcreate is /usr/sbin/lvcreate

获取命令帮助

内置命令一般使用命令help获取帮助。

[root@c7 ~]# help kill
kill: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
    send a signal to a job.
    
    send the processes identified by pid or jobspec the signal named by
    sigspec or signum.  if neither sigspec nor signum is present, then
    sigterm is assumed.
    
    options:
      -s sig    sig is a signal name
      -n sig    sig is a signal number
      -l    list the signal names; if arguments follow `-l' they are
        assumed to be signal numbers for which names should be listed
    
    kill is a shell builtin for two reasons: it allows job ids to be used
    instead of process ids, and allows processes to be killed if the limit
    on processes that you can create is reached.
    
    exit status:
    returns success unless an invalid option is given or an error occurs.

外部命令一般使用命令man获取帮助。

kill(1)                                                                               user commands                                                                              kill(1)

name
       kill - terminate a process

synopsis
       kill [-s signal|-p] [-q sigval] [-a] [--] pid...
       kill -l [signal]
...

注意:命令kill,应该是bash有内置了,外部命令同时还提供了。

[root@c7 ~]# which kill
/usr/bin/kill
[root@c7 ~]# rpm -qf /usr/bin/kill
util-linux-2.23.2-52.el7.x86_64
[root@c7 ~]# type kill
kill is a shell builtin

外部命令还有一种获取帮助的方式是info命令,使用info命令就像在阅读一本书籍一样,有不同的章节,因此info命令提供的帮助信息的内容量较大、较详细。

命令 which

简介

显示命令(shell)的完整路径。

语法

which [options] [--] programname [...]

描述

命令可以带上多个参数。which的工作原理是在环境变量path路径下使用和bash相关的算法去搜索可执行文件或脚本。

选项

--all, -a:打印所有被匹配到的可执行程序,不仅仅是只打印第一个。

[root@c7 ~]# touch /usr/local/sbin/zwl.sh /usr/local/bin/zwl.sh
[root@c7 ~]# chmod a+x /usr/local/sbin/zwl.sh /usr/local/bin/zwl.sh
[root@c7 ~]# which zwl.sh
/usr/local/sbin/zwl.sh
[root@c7 ~]# which -a zwl.sh
/usr/local/sbin/zwl.sh
/usr/local/bin/zwl.sh

--read-alias, -i:从标准输入中读取别名,这样子使得which可以获取到命令的别名。

[root@c7 ~]# alias | /usr/bin/which --read-alias ls
alias ls='ls --color=auto'
    /usr/bin/ls
[root@c7 ~]# /usr/bin/which ls
/usr/bin/ls

基于此选项的特性,很适合将which自身也做成别名,系统自身已经帮我们做好了。

[root@c7 ~]# alias | grep 'which'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@c7 ~]# which grep
alias grep='grep --color=auto'
    /usr/bin/grep

--skip-dot:跳过path中以“.”开头的目录。

--skip-tilde:跳过path中以“~”开头的目录和家目录下的可执行程序。

--show-dot:与--skip-dot相反,不跳过,但是不会显示完整的路径,而是显示“./programname”。

--show-tilde:当目录匹配为家目录的时候输出一个波浪号。如果which是以root用户执行的话,则此选项会被忽略。

[zwl@c7 bin]$ pwd
/home/zwl/bin
[zwl@c7 bin]$ echo $path
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/zwl/.local/bin:/home/zwl/bin
[zwl@c7 bin]$ which zwl.sh
~/bin/zwl.sh

命令 whereis

简介

根据给出的命令名,查找对应的二进制程序,源代码和man手册。

语法

whereis [options] [-bms directory... -f] name...

描述

给出的命令名首先会被剥夺掉路径名和任何形式的扩展名。比如/path/to/command.exe,剥夺后剩下command。由源代码控制所产生的“s.”前缀也会被处理。然后whereis会尝试在linux的标准位置和path和manpath环境变量所指定的位置定位程序。

选项

-b:仅查找二进制文件。

-m:仅查找man手册文件。

-s:仅查找源代码文件。

-b:限制whereis查找二进制文件所使用的目录,多个目录使用空格分隔。

-m:限制whereis查找man手册文件所使用的目录,多个目录使用空格分隔。

-s:限制whereis查找源代码文件所使用的目录,多个目录使用空格分隔。

-f:终止目录列表并标志文件的开始。当-b、-m或者-s选项有被使用的时候,此选项必须被使用。

-u:只会显示那些具备不寻常(unusual)条目的命令的名字。当一个命令在每个明确的请求类型下,不是只有一个(即没有或者有多个)条目的时候,该命令会被认为是不寻常的。因此'whereis -m -u *'命令会查找当前目录中的那些没有文档的或者文档条目不止一个的文件。这种用法,一般我们都是会进入到某个bin目录底下,这样子就可以使得文件名即命令名了。

-l:输出whereis命令所使用的有效查找路径。当-b、-m或者-s选项均没有被使用的时候,该选项会输出命令可以在系统上找到的whereis程序代码所写死的(==写死,即硬编码,hard coded==)路径。

示例

不带选项,输出二进制程序文件、配置文件、man手册。

[root@c7 bin]# whereis passwd
passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man5/passwd.5.gz /usr/share/man/man1/passwd.1.gz

查找/usr/bin中的所有命令,找出那些在/usr/man/man1中没有帮助手册的,或者在/usr/src下没有源码文件的。

$ cd /usr/bin
$ whereis -u -ms -m /usr/man/man1 -s /usr/src -f *

文件搜索路径

默认情况下,whereis尝试从写死的路径中查找文件,写死的路径是通过glob模式被定义的。
默认情况下,whereis也会从环境变量path和manpath的值中查找文件。
如果想要知道whereis在本次查找中到底查找了哪些路径,可以使用选项-l。有意思的一点是/usr/src/底下的两个源码目录,都是空的。

[root@c7 ~]# whereis -l
bin: /usr/bin
bin: /usr/sbin
bin: /usr/lib
bin: /usr/lib64
...
man: /usr/share/man/man7
man: /usr/share/man/man5
man: /usr/share/man/man1
man: /usr/share/man/man8
...
src: /usr/src/debug
src: /usr/src/kernels

man手册中说“选项-b、-m和-s带来的影响也会被-l显示出来”,但是我测试不出来,不知道是否是自己敲的命令的问题,不深究,pass。

命令 who

简介

显示已登录的用户。

[root@c7 ~]# who
root     pts/0        2018-10-22 09:47 (192.168.17.1)
root     pts/1        2018-10-22 15:33 (192.168.17.1)

语法

who [option]... [ file | arg1 arg2 ]

选项

-b, --boot:显示系统上次引导(开机)的日期和时间。

[root@c7 ~]# who -b
         system boot  2018-10-22 09:47

-d, --dead:打印死亡进程。

-l, --login:打印系统登录进程。

-p, --process:打印由init所spawn出来的活跃进程。

-r, --runlevel:打印当前运行级别。

[root@c7 ~]# who -r
         run-level 5  2018-10-23 09:23

-t, --time:打印上次系统时钟变化。

-t, -w, --mesg:添加用户的消息状态(+, -, ?)。

  • +:表示允许写消息;
  • -:表示禁止写消息;
  • ?:表示无法找到终端设备。
[root@c7 ~]# who -t
root     + pts/0        2018-10-23 09:36 (192.168.17.1)
root     + pts/1        2018-10-23 09:38 (192.168.17.1)

-u, --users:列出已登录的用户,与不带选项的who有些许不同,可以看到登录shell的pid。

[root@c7 ~]# who
root     pts/0        2018-10-23 09:36 (192.168.17.1)
root     pts/1        2018-10-23 09:38 (192.168.17.1)
[root@c7 ~]# who -u
root     pts/0        2018-10-23 09:36 00:25        1999 (192.168.17.1)
root     pts/1        2018-10-23 09:38   .          2082 (192.168.17.1)

-a, --all:等同于-b -d --login -p -r -t -t -u。

[root@c7 ~]# who -a
           system boot  2018-10-23 09:23
           run-level 5  2018-10-23 09:23
root     + pts/0        2018-10-23 09:36 00:26        1999 (192.168.17.1)
root     + pts/1        2018-10-23 09:38   .          2082 (192.168.17.1)

如果省略file的话,则使用/var/run/utmp。常见的可以将/var/log/wtmp作为file来使用,这个文件主要是可以用于查看过去登录的用户信息。
arg1和arg2一般是“am i”或者“mom likes”,表示显示当前自己的登录信息。等同于-m选项。

命令 w

命令w是命令who的延展,除了有who的作用以外,还会显示额外的头部信息。

[root@c7 ~]# who
root     pts/0        2018-10-23 09:36 (192.168.17.1)
root     pts/1        2018-10-23 09:38 (192.168.17.1)
[root@c7 ~]# w
 11:36:06 up  2:12,  3 users,  load average: 0.00, 0.01, 0.05
user     tty      from             login@   idle   jcpu   pcpu what
root     pts/0    192.168.17.1     09:36   26:06   0.09s  0.02s less -s
root     pts/1    192.168.17.1     09:38    6.00s  0.09s  0.00s w

在头部信息中,从左往右分别表示:

  • 当前时间;
  • 系统运行时间;
  • 当前登录的用户数,不过这个值好像会+1;
  • 过去1、5和15分钟,cpu的负载情况。

下面的条目信息中,有几个需要说明:

  • jcpu:依附在tty上的所有进程所使用的时间。不包含过去后台作业(job),但包含当前正在运行的后台作业。
  • pcpu:当前进程所使用的时间,即what显示的进程。
  • what:用户当前正在运行的进程。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网