命令的用法
命令是什么?
命令有以下四种:
-
一个可执行程序,就像我们在
/ usr / bin
中看到的所有那些文件一样。因此,程序可以编译为二进制文件,例如用C和C ++或用脚本语言(例如shell,perl,python等 -
内置在
shell
中的命令。bash
支持许多命令,其中包括内部称为Shell Builting
。例如,cd命令是内置的shell。 -
shell
函数。这些是合并到环境中的微型shell
脚本。 -
别名。我们可以定义自己的命令,这些命令是从其他命令构建的。
判别属性
识别正在使用四种命令中的哪一种。
type – 类型
type
命令是一个内置的Shell
命令,它显示shell
将要执行的命令的种类,格式:
type command
[root@iZerb5rob3dcf6Z ~]# type type
type 是 shell 内嵌
[root@iZerb5rob3dcf6Z ~]# type ls
ls 是 `ls --color=auto' 的别名
[root@iZerb5rob3dcf6Z ~]# type cp
cp 是 `cp -i' 的别名
which – 定位可执行程序
有时,系统上安装了一个以上版本的可执行程序。要确定给定可执行文件的确切位置,可以使用which
命令:
[root@iZerb5rob3dcf6Z ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@iZerb5rob3dcf6Z ~]# which cd
/usr/bin/cd
获取帮助
内嵌命令
help command
[root@iZerb5rob3dcf6Z ~]# help cd
cd: cd [-L|[-P [-e]]] [dir]
Change the shell working directory.
Change the current directory to DIR. The default DIR is the value of the
HOME shell variable.
The variable CDPATH defines the search path for the directory containing
DIR. Alternative directory names in CDPATH are separated by a colon (:).
A null directory name is the same as the current directory. If DIR begins
with a slash (/), then CDPATH is not used.
.....
–help – 使用信息
[root@iZerb5rob3dcf6Z ~]# mkdir --help
用法:mkdir [选项]... 目录...
Create the DIRECTORY(ies), if they do not already exist.
Mandatory arguments to long options are mandatory for short options too.
-m, --mode=MODE set file mode (as in chmod), not a=rwx - umask
-p, --parents no error if existing, make parent directories as needed
-v, --verbose print a message for each created directory
-Z set SELinux security context of each created directory
to the default type
--context[=CTX] like -Z, or if CTX is specified then set the SELinux
......
某些程序不支持“ --help”选项。
man – 程序手册
大多数可执行程序都提供了手册或手册页的说明。一个叫做man
的特殊分页程序用于查看它们。
man program
man
显示的“手册”分为几部分,不仅涵盖了用户的内容,指令,还有系统管理命令,编程接口,文件格式等。下表描述了手册的布局:
有时我们需要查看手册的特定部分以查找我们所要查找的内容。不指定节号,我们将始终获得的第一个实例可能在第1节中。要指定节号,我们使用如下所示的man
:
#man section search_tem
[root@iZerb5rob3dcf6Z ~]# man 8 useradd
USERADD(8) 系统管理命令 USERADD(8)
名称
useradd - 创建一个新用户或更新默认新用户信息
大纲
useradd [选项] 登录
useradd -D
useradd -D [选项]
......
whatis –简短描述
whatis
程序显示手册页匹配的名称和一行简单描述:
[root@iZerb5rob3dcf6Z ~]# whatis ls
ls (1) - list directory contents
info --信息条目
信息页面将显示一个名为“ info”
的阅读器程序。信息页面像网页一样超链接。
README 以及其他程序文档文件
系统上安装的许多软件包的文档文件都位于/ usr / share / doc
目录。其中大多数以纯文本格式存储,并且可以用less命令查看。
alias–创建别名
将多个命令通过将' ;'分开可以在一行上放置多个命令。它是这样的:
command1 ; command2 ; command3 ..
[root@iZerb5rob3dcf6Z ~]# ls ; cd / ; ls
dir0 dir2 dir3 dir999 file{1-4} filel2 file-sym link-test My_Hexo node_modules noexistdir package-lock.json sum test wildtest
bin boot dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@iZerb5rob3dcf6Z /]#
现在,让我们使用alias
命令将此序列转换为新命令。在此之前,最好先弄清楚新命令的名称是否为已经被使用,例如这里我们xiongze
作为新命令,可以使用type命令检查是否被占用:
[root@iZerb5rob3dcf6Z ~]# type xiongze
-bash: type: xiongze: 未找到
[root@iZerb5rob3dcf6Z ~]# alias xiongze='ls; cd / ;ls '
[root@iZerb5rob3dcf6Z ~]# xiongze
dir0 dir2 dir3 dir999 file{1-4} filel2 file-sym link-test My_Hexo node_modules noexistdir package-lock.json sum test wildtest
bin boot dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@iZerb5rob3dcf6Z /]#
在命令“alias
之后,我们给别名起一个名字,紧随其后(没有空格,低等号)后跟等号,然后紧跟带引号的字符串。定义别名后,仅可以在当前shell
下使用。
要删除别名,可以用unalias
命令:
[root@iZerb5rob3dcf6Z /]# unalias xiongze
[root@iZerb5rob3dcf6Z /]# xiongze
-bash: xiongze: 未找到命令
查看当前shell
中所有的别名:
[root@iZerb5rob3dcf6Z ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'