Linux 命令扩展、历史记录管理与帮助系统详解

在工作中,学会查看命令的帮助与命令行的扩展对提高工作效率有很大的帮助,本文将以CentOS版本示例如何去使用查看与理解这两个功能。(本文将以CentOS 7.9版本示例)

命令行扩展

  • $(COMMAND)

[root@bi-test-centos ~]# echo $(ls)
2024-07-30.log release_worweb.sh

  • `COMMAND` 

[root@bi-test-centos ~]# echo `ls`
2024-07-30.log release_worweb.sh

双引号不能识别命令,但可以识别变量: 

[root@bi-test-centos ~]# echo "echo $(ls)"
echo 2024-07-30.log
release_worweb.sh 

单引号既不能识别命令也不能识别变量: 

[root@bi-test-centos ~]# echo 'echo $(ls)'
echo $(ls) 

反引号的运行规则是,反引号中的内容直接输出结果再交给反引号外的命令继续执行: 

[root@bi-test-centos ~]# echo `echo $(ls)`
2024-07-30.log release_worweb.sh 

{}扩展

  • {元素1,元素2,元素3}

[root@bi-test-centos ~]# echo {1,2,3}
1 2 3
[root@bi-test-centos ~]# echo file{1,2,3}
file1 file2 file3
[root@bi-test-centos ~]# echo {file,text,example}{1,2,3}
file1 file2 file3 text1 text2 text3 example1 example2 example3

[root@bi-test-centos ~]# echo {file,text,example} {1,2,3}       
file text example 1 2 3                                #两个花括号中间带空格就不会出现排列组合

  •  {元素1..元素n} 

[root@bi-test-centos ~]# echo {0..10}
0 1 2 3 4 5 6 7 8 9 10
[root@bi-test-centos ~]# echo {0..10..2}
0 2 4 6 8 10
[root@bi-test-centos ~]# echo {00..10..2}
00 02 04 06 08 10 

[root@bi-test-centos ~]# echo {A..z}
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [  ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z                                                      #根据ASCII码顺序排列
[root@bi-test-centos ~]# echo {a..z} {A..Z}
a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

 history命令

一般工作中常用一下选项:

查看所有历史命令

[root@bi-test-centos ~]# history
    1  history
    2  echo {1,2,3}
    3  echo {a..z}
    4  echo {1,2,3}{a..z}
    5  echo {1,2,3} {a..z}
    6  history

执行某一个编号的命令

 [root@bi-test-centos ~]# !5
echo {1,2,3} {a..z}
1 2 3 a b c d e f g h i j k l m n o p q r s t u v w x y z

执行最后一条命令

[root@bi-test-centos ~]# !! 

 执行最后一条以某个字符串为开头的命令

 [root@bi-test-centos ~]# !e
echo {1,2,3} {a..z}
1 2 3 a b c d e f g h i j k l m n o p q r s t u v w x y z

清空历史命令

(只在当前会话中清空,不会删除历史文件中的数据)

[root@bi-test-centos ~]# history -c
[root@bi-test-centos ~]# history
    1  history 

查看历史文件

[root@bi-test-centos ~]# cat .bash_history 

如果找不到历史文件,可以用 find / -type f -name '*history' 进行查找路径。

查看命令帮助信息

查看命令的简要描述(whatis)

[root@bj-test-centos ~]# whatis ls
ls (1)               - list directory contents

查找命令的相关文件 (whereis)

 [root@bj-test-centos ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz

查看内部命令帮助

help命令可以得到命令功能的描述、命令的用法、可用的选项、参数的说明、退出的状态。

[root@bj-test-centos ~]# help read
read: read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]                                                                     #命令的描述
    Read a line from the standard input and split it into fields.

    Reads a single line from the standard input, or from file descriptor FD
    if the -u option is supplied.  The line is split into fields as with word
    splitting, and the first word is assigned to the first NAME, the second
    word to the second NAME, and so on, with any leftover words assigned to
    the last NAME.  Only the characters found in $IFS are recognized as word
    delimiters.

    If no NAMEs are supplied, the line read is stored in the REPLY variable.

    Options:                                                                         #命令支持的选项和作用
      -a array    assign the words read to sequential indices of the array
            variable ARRAY, starting at zero
      -d delim    continue until the first character of DELIM is read, rather
            than newline
      -e        use Readline to obtain the line in an interactive shell
      -i text    Use TEXT as the initial text for Readline
      -n nchars    return after reading NCHARS characters rather than waiting
            for a newline, but honor a delimiter if fewer than NCHARS
            characters are read before the delimiter
      -N nchars    return only after reading exactly NCHARS characters, unless
            EOF is encountered or read times out, ignoring any delimiter
      -p prompt    output the string PROMPT without a trailing newline before
            attempting to read
      -r        do not allow backslashes to escape any characters
      -s        do not echo input coming from a terminal
      -t timeout    time out and return failure if a complete line of input is
            not read withint TIMEOUT seconds.  The value of the TMOUT
            variable is the default timeout.  TIMEOUT may be a
            fractional number.  If TIMEOUT is 0, read returns success only
            if input is available on the specified file descriptor.  The
            exit status is greater than 128 if the timeout is exceeded
      -u fd        read from file descriptor FD instead of the standard input

    Exit Status:
    The return code is zero, unless end-of-file is encountered, read times out,
    or an invalid file descriptor is supplied as the argument to -u.
readarray: readarray [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]                                                                               #解释命令的参数和使用场景
    Read lines from a file into an array variable.

    A synonym for `mapfile'.
readonly: readonly [-aAf] [name[=value] ...] or readonly -p
    Mark shell variables as unchangeable.

    Mark each NAME as read-only; the values of these NAMEs may not be
    changed by subsequent assignment.  If VALUE is supplied, assign VALUE
    before marking as read-only.

    Options:
      -a    refer to indexed array variables
      -A    refer to associative array variables
      -f    refer to shell functions
      -p    display a list of all readonly variables and functions

    An argument of `--' disables further option processing.

    Exit Status:                                                                      #退出的状态(返回值)
    Returns success unless an invalid option is given or NAME is invalid.

查看外部命令帮助

  • COMMAND --help
  • man COMMAND

通用查看命令帮助

  • info COMMAND
  • tldr COMMAND

没有这个插件的,可以输入以下命令进行下载。

[root@bj-test-centos doc]# yum install tldr

 tldr这个命令相较于前面的几个帮助命令,更为简洁和易用。

[root@bj-test-centos doc]# tldr ls

  ls

  List directory contents.
  More information: https://www.gnu.org/software/coreutils/ls.

  - List files one per line:
    ls -1

  - List all files, including hidden files:
    ls -a

  - List all files, with trailing `/` added to directory names:
    ls -F

  - Long format list (permissions, ownership, size, and modification date) of all files:
    ls -la

  - Long format list with size displayed using human-readable units (KiB, MiB, GiB):
    ls -lh

  - Long format list sorted by size (descending) recursively:
    ls -lSR

  - Long format list of all files, sorted by modification date (oldest first):
    ls -ltr

  - Only list directories:
    ls -d */

 总结

学会查看命令的帮助是一项重要的技能,当遇到新的命令时,能够轻松的利用帮助命令了解使用的语法,从而提高工作效率。

  • 18
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值