命令千千万,遇到不熟悉的命令怎么办?Google search?其实可以本地help的!当然还有很多的操作……

1. Command Lists

Command Meaning
type Indicate how a command name is interpreted
which Display which executable program will be executed
help Get help for shell builtins
man Display a command’s manual page
apropos Display a list of appropriate commands(相近命令)
info Display a command’s info entry
whatis Display one-line manual page descriptions(一句话描述)
alias Create an alias for a command

2. What exactly are commands?

  1. An executable program(可执行程序)
  2. A command built into the shell itself(内置命令)
  3. A shell function(函数)
  4. An alias(别名,自己组合的命令)

3. Commands

3.1 Indentifying commands

$\bullet$ typeDisplay the kind of command the shell will execute

[me@linuxbox ~]$ type type
type is a shell builtin
[me@linuxbox ~]$ type ls
ls is aliased to `ls --color=tty`
[me@linuxbox ~]$ type cp
cp is /bin/cp

$\bullet$ whichDisplay an executable’s location

3.2 Getting a Command’s Documentation

$\bullet$ helpGet Help for Shell Builtins

[me@linuxbox ~]$ 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.
 
 If the directory is not found, and the shell option 'cdable_vars' is set,
 the word is assumed to be a variable name. If that variable has a value,
 its value is used for DIR.
 
 Options:
 -L force symbolic links to be followed: resolve symbolic links in
 DIR after processing instances of '..'
 -P use the physical directory structure without following symbolic
 links: resolve symbolic links in DIR before processing instances
 of '..'
 -e if the -P option is supplied, and the current working directory
 cannot be determined successfully, exit with a non-zero status
 -@ on systems that support it, present a file with extended attributes
 as a directory containing the file attributes

  注:[]表示该条目是可选项,|表示二者选其一;help命令也可以这样使用

[me@linuxbox ~]$ mkdir --help
Usage: mkdir [OPTION] DIRECTORY...
Create the DIRECTORY(ies), if they do not already exist.
 -Z, --context=CONTEXT (SELinux) set security context to CONTEXT
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
 --help display this help and exit
 --version output version information and exit
Report bugs to <bug-coreutils@gnu.org>.

$\bullet$ manDisplay a Program’s Manual Page

3.3 Creating Our Own Commands

命令组合(中间分号隔开)可以有如下效果

[me@linuxbox ~]$ cd /usr; ls; cd -
bin games include lib local sbin share src
/home/me
[me@linuxbox ~]$

现在可以用alias命令将其组合起来,示例如下

[me@linuxbox ~]$ alias foo='cd /usr; ls; cd -'

此时foo就代表了这个组合,注意取名字不要和内置的重复,可以在取名前用type命令测试下。

[me@linuxbox ~]$ foo
bin games include lib local sbin share src
/home/me
[me@linuxbox ~]$

取消这个“别名”,则如下

[me@linuxbox ~]$ unalias foo
[me@linuxbox ~]$ type foo
bash: type: foo: not found

当然,这个shell近程结束时,这些别名自然就无效了,当你再次打开shell时这些都无效。当然除非你将它写进“环境文件”。