• Linux之(15)shell(7)常见命令


    Linux之(15)shell常见命令

    Author:onceday Date: 2022年11月16日

    全系列文章请查看专栏: Linux Shell基础_Once_day的博客-CSDN博客

    漫漫长路,有人对你效果嘛…

    1. 内容处理命令
    1.1 cut 切选

    按行来操作,从每一行中切去所需要的信息。

    • -d 可以接分割字符,与-f一起使用
    • -f取出由-d分割的段中一部分。
    • -c,以字符的单位取出固定字符区间。

    如下示例:

    root@ubuntu:~# echo $PATH
    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
    root@ubuntu:~# echo $PATH | cut -d ':' -f 2
    /usr/local/bin
    
    • 1
    • 2
    • 3
    • 4
    root@ubuntu:~# export | tail -n 5 
    declare -x USER="root"
    declare -x XDG_RUNTIME_DIR="/run/user/0"
    declare -x XDG_SESSION_CLASS="user"
    declare -x XDG_SESSION_ID="68400"
    declare -x XDG_SESSION_TYPE="tty"
    root@ubuntu:~# export | tail -n 5 | cut -c 12-
    USER="root"
    XDG_RUNTIME_DIR="/run/user/0"
    XDG_SESSION_CLASS="user"
    XDG_SESSION_ID="68400"
    XDG_SESSION_TYPE="tty"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    1.2 grep 抓取目标信息

    grep通过子表达式,对每一行进行搜索,如果满足子表达式,则输出该行。

    子表达式可以是正则表达式,支持扩展和标准正则表达式。

    常见参数如下:

    • -a,将二进制文件以文本形式的方式查找数据
    • -c,计算找到"查找字符"的次数
    • -i,忽略大小写的不同
    • -n,顺便输出行号
    • -v,反向选择,显示没有目标信息的行
    root@ubuntu:~# export | grep USER
    declare -x USER="root"
    
    • 1
    • 2
    1.3 sort 排序文本

    sort可以根据不同的参数对数据进行排序,但排序的字符和语言编码有关。因此需要注意语系的选择。

    常用参数如下:

    • -f,忽略大小写的差异,例如A与a视为编码相同
    • -b,忽略最前面的空格字符部分
    • -M,以月份的名字来排序,例如JAN、DEC等的排序方法。
    • -n,使用纯数字进行排序
    • -r,反向排序
    • -u,就是uniq,相同的数据中,仅出现一行代表
    • -t,分割符号,默认是用Tab键来分割
    • -k,以哪个区间(field)来进行排序的意思
    root@ubuntu:~# export | tail -n 5 |sort -r
    declare -x XDG_SESSION_TYPE="tty"
    declare -x XDG_SESSION_ID="68400"
    declare -x XDG_SESSION_CLASS="user"
    declare -x XDG_RUNTIME_DIR="/run/user/0"
    declare -x USER="root"
    root@ubuntu:~# export | tail -n 5 |sort 
    declare -x USER="root"
    declare -x XDG_RUNTIME_DIR="/run/user/0"
    declare -x XDG_SESSION_CLASS="user"
    declare -x XDG_SESSION_ID="68400"
    declare -x XDG_SESSION_TYPE="tty"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    1.4 uniq 去除重复元素

    常用参数:

    • -i,忽略大小写字符的不同
    • -c, 进行计数
    root@ubuntu:~# last | cut -d ' ' -f 1 |sort | uniq
    
    lighthou
    onceday
    reboot
    root
    ubuntu
    wtmp
    root@ubuntu:~# last | cut -d ' ' -f 1 |sort | uniq -c
          1 
          3 lighthou
         45 onceday
          4 reboot
         35 root
          1 ubuntu
          1 wtmp
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    1.5 wc 计算文本信息

    常用参数:

    • -l,仅列出行
    • -w,仅列出多少字
    • -m,多少字符
    root@ubuntu:~# last | cut -d ' ' -f 1 |sort | uniq -c 
          1 
          3 lighthou
         45 onceday
          4 reboot
         35 root
          1 ubuntu
          1 wtmp
    root@ubuntu:~# last | cut -d ' ' -f 1 |sort | uniq -c |wc
          7      13      98
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    输出的数据包含从左到右,依次是行,字数,字符数。

    1.6 tee 双重定向

    tee会将数据同时送到文件和stdout中。

    tee [-a] file
    
    • 1

    -a表示以累加的方式,即append

    2. 字符装换命令
    2.1 tr 删除替换
    tr [-ds] SET1 ...
    
    • 1
    • -d,删除信息中的这个字符
    • -s,替换掉重复的字符
    root@ubuntu:~# export | tail -n 5 | tr '[A-Z]' '[a-z]'
    declare -x user="root"
    declare -x xdg_runtime_dir="/run/user/0"
    declare -x xdg_session_class="user"
    declare -x xdg_session_id="68400"
    declare -x xdg_session_type="tty"
    root@ubuntu:~# export | tail -n 5 | tr -d '[A-Z]'
    declare -x ="root"
    declare -x __="/run/user/0"
    declare -x __="user"
    declare -x __="68400"
    declare -x __="tty"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    2.2 col 转换空白字符

    col -xtab键转换成对等的空格键。

    2.3 join 比较和拼接行数据
    join [-ti12] file1 file2
    
    • 1
    • -t,指定分隔符,默认以空格字符分割数据,并比对第一个栏位的数据。
    • -i,忽略大小写的差异
    • -1,表示第一个文件使用的分割段位置
    • -2,表示第二个文件使用的分割段位置
    root@ubuntu:~# join -t ":" -1 4  /etc/passwd -2 3  /etc/group |head -n 3
    join: 0:root:x:0:root:/root:/bin/bash:root:x:
    1:daemon:x:1:daemon:/usr/sbin:/usr/sbin/nologin:daemon:x:
    2:bin:x:2:bin:/bin:/usr/sbin/nologin:bin:x:
    /etc/passwd:6: is not sorted: games:x:5:60:games:/usr/games:/usr/sbin/nologin
    
    • 1
    • 2
    • 3
    • 4
    • 5

    一般需要排序之后再使用,否则有些文件会弹出警告

    2.4 paste 直接拼接行数据
    paste [-d] file1 file2
    
    • 1
    • -d 后面可以接分割字符,默认是以[Tab]来分割
    root@ubuntu:test# cat te
    line1
    line2
    line3
    root@ubuntu:test# cat te2
    xxx
    ccc
    vvv
    root@ubuntu:test# paste -d : te te2
    line1:xxx
    line2:ccc
    line3:vvv
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    2.5 expand 将tab转换成空格
    expand [-t] file
    
    • 1

    -t后面可接数字,指定tab键替换的空格数。

    2.6 unexpand 转换空格到tab
    unexpand [-t] file
    
    • 1

    -t后面可接数字,指定tab键替换的空格数。

    2.7 split 划分文件
    split [-bl] file PREFIX
    
    • 1
    • -b,后面可以接欲划分成的文件大小,可加单位b,k,m等。
    • -l,以行数来进行划分。
    • PREFIX,前缀字符,表示划分文件的前缀文字。
    2.8 xargs 解析参数

    xargs可以读入stdin的内容,然后解析成参数。

    xargs [-0epn] command
    
    • 1
    • -0,输入的stdin含有特殊字符,如\等特殊字符,-0 的参数可以将其还原成一般字符。
    • -e,这是EOF(end of file)的意思,后面可以接一个字符,当xargs解析到这个字符时,就会停止工作。
    • -p,在执行每个命令时,都会询问使用者的意思。
    • -n,后面接次数,每次command命令执行时,使用的参数数目。
    root@ubuntu:~# find . -name "test"
    ./test
    root@ubuntu:~# find . -name "test" | xargs ls -l
    total 24
    -rw-r--r-- 1 root root     5 Nov 12 20:56 b
    -rw-r--r-- 1 root root    18 Nov 16 23:33 te
    -rw-r--r-- 1 root root    12 Nov 16 23:33 te2
    -rw-r--r-- 1 root root 12288 Oct 27 19:06 test.db
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    2.9 特殊的减号-

    减号可用于在管道命令中代替stdoutstdin:

    tar -cvf - /home | tar -xvf - -c /tmp/homepack
    
    • 1

    上述命令即把/home中内容打包,但输出到stdout中,然后管道命令对端使用-stdin获取输入,再解压到对应的文件中去

    3. 文件格式化
    3.1 格式化打印:printf

    这个打印命令的格式输出和C的printf函数很像。

    有以下特殊命令:

    特殊样式格式符
    \a警告声音输出
    \b退格键(backspace)
    \f清除屏幕(form feed)
    \n输出新的一行
    \r亦即回车按键
    \t水平的[tab]按键
    \v垂直的[tab]按键
    \xNNNN为两位数的数字,可以转换数字成为字符
    %ns那个n是数字,s代表string,亦即多少个字符
    %ni那个n是数字,i代表integer,亦即多少整数位数
    %N.nf那个n与N都是数字,f代表floating(浮点数),如果有小数位数,例如%10.2f

    使用格式如下:

    onceday@ubuntu:shell$ printf '%10s %4d %8.2f\n' "1452" 25 145.23
          1452   25   145.23
    
    • 1
    • 2
  • 相关阅读:
    职业成功指南:10条核心原则(上)丨三叠云
    SystemServer是如何启动AMS的
    【HCIE】13.VXLAN EVPN
    Java模板方法模式源码剖析及使用场景
    Linux基本指令2——时间相关
    【数字IC/FPGA】热独码检测
    深入理解 MyBatis
    基于SpringBoot的家电销售电商管理平台
    线程方法join/join(timeout)源码分析
    什么是迭代器,Python迭代器及其用法
  • 原文地址:https://blog.csdn.net/Once_day/article/details/127972910