Linux基础知识总结2 | vim与bash

2022-03-02 13:52:06 浏览数 (1)

《鸟哥的Linux私房菜》学习笔记

1.Vim的使用

  • 一般指令模式:vim进去模式的模式
  • 编辑模式:在一般模式下按 “i, I, o, O, a, A, r, R” 可以进入编辑模式
  • 命令行命令模式:在一般模式下输入 “: / ?” 任意一个,可以将光标移动到最下面那一列

vim fileName可以新建一个文件,编辑完后按ESC输入:wq即可完成编辑且退出,若文件权限不对无法写入,可以加一个!表示强制写入,即 :wq!,但这是在“你的权限可以改变”的情况下才能成立的

在一般指令模式下:按u可以撤销编辑模式下的修改、30 j向下移动30行光标、30 k向上移动30行光标

菜鸟教程 vim

2.BASH

2.1 认识bash与shell

通过“ Shell ”将输入的指 令与 Kernel 沟通,好让 Kernel 可以控制硬件来正确无误的工作

history:历史查询功能

Tab按键:自动补全

alias:命令别名设置功能,如:alias lm='ls -al'lmls -al功能是一样的,要取消别名的设置unalias lm

代码语言:javascript复制
[root@VM-20-9-centos /]# 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; declare -f) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot'
alias xzegrep='xzegrep --color=auto'
alias xzfgrep='xzfgrep --color=auto'
alias xzgrep='xzgrep --color=auto'
alias zegrep='zegrep --color=auto'
alias zfgrep='zfgrep --color=auto'
alias zgrep='zgrep --color=auto'

Wildcard:万用字符:比如查看 /usr/bin文件夹下面以x开头的文件或文件夹

代码语言:javascript复制
[root@VM-20-9-centos /]# ls -l /usr/bin/x*
lrwxrwxrwx  1 root root       7 4月  24 2020 /usr/bin/x86_64 -> setarch
-rwxr-xr-x  4 root root 1257912 10月 12 2020 /usr/bin/x86_64-redhat-linux-c  
-rwxr-xr-x  4 root root 1257912 10月 12 2020 /usr/bin/x86_64-redhat-linux-g  
-rwxr-xr-x  3 root root 1253912 10月 12 2020 /usr/bin/x86_64-redhat-linux-gcc
-rwxr-xr-x  3 root root 1253912 10月 12 2020 /usr/bin/x86_64-redhat-linux-gcc-8
-rwxr-xr-x. 1 root root     424 5月  11 2019 /usr/bin/x86_64-redhat-linux-gnu-pkg-config
-rwxr-xr-x  1 root root   32832 10月 22 2020 /usr/bin/x86_energy_perf_policy
-rwxr-xr-x. 1 root root   90312 5月  11 2019 /usr/bin/xargs
-rwxr-xr-x  1 root root  340216 11月  9 2019 /usr/bin/xgettext
-rwxr-xr-x  1 root root   20656 4月   7 2020 /usr/bin/xmlcatalog
-rwxr-xr-x  1 root root   75096 4月   7 2020 /usr/bin/xmllint
-rwxr-xr-x. 1 root root   35680 5月  11 2019 /usr/bin/xmlwf
-rwxr-xr-x  1 root root   29248 10月 15 2020 /usr/bin/xsltproc
-rwxr-xr-x  1 root root   23440 11月 12 2019 /usr/bin/xxd
-rwxr-xr-x. 1 root root   91432 5月  11 2019 /usr/bin/xz
lrwxrwxrwx. 1 root root       2 5月  11 2019 /usr/bin/xzcat -> xz
lrwxrwxrwx. 1 root root       6 5月  11 2019 /usr/bin/xzcmp -> xzdiff
-rwxr-xr-x. 1 root root   18376 5月  11 2019 /usr/bin/xzdec
-rwxr-xr-x. 1 root root    6632 5月  11 2019 /usr/bin/xzdiff
lrwxrwxrwx. 1 root root       6 5月  11 2019 /usr/bin/xzegrep -> xzgrep
lrwxrwxrwx. 1 root root       6 5月  11 2019 /usr/bin/xzfgrep -> xzgrep
-rwxr-xr-x. 1 root root    5628 5月  11 2019 /usr/bin/xzgrep
-rwxr-xr-x. 1 root root    1802 5月  11 2019 /usr/bin/xzless
-rwxr-xr-x. 1 root root    2161 5月  11 2019 /usr/bin/xzmore

type:查看指令是否为内置指令, 因此 type 也可以用来作为类似 which 指令的用途

代码语言:javascript复制
[root@VM-20-9-centos /]# type man
man 是 /usr/bin/man
[root@VM-20-9-centos /]# type ls
ls 是 `ls --color=auto` 的别名
[root@VM-20-9-centos /]# type history
history 是 shell 内建
[root@VM-20-9-centos /]# type cd
cd 是 shell 内

[Enter]键:换行输入

代码语言:javascript复制
[root@VM-20-9-centos /]# ls 
> -al
总用量 80
dr-xr-xr-x.  19 root root  4096 3月   1 10:46 .
dr-xr-xr-x.  19 root root  4096 3月   1 10:46 ..
-rw-r--r--    1 root root     0 11月 26 2019 .autorelabel
lrwxrwxrwx.   1 root root     7 5月  11 2019 bin -> usr/bin
dr-xr-xr-x.   5 root root  4096 4月  13 2021 boot
drwxr-xr-x    2 root root  4096 12月 10 2019 data
drwxr-xr-x   19 root root  2960 11月  7 01:31 dev
drwxr-xr-x. 100 root root 12288 2月  18 14:25 etc
drwxr-xr-x.   6 root root  4096 2月  18 14:25 home
lrwxrwxrwx.   1 root root     7 5月  11 2019 lib -> usr/lib
lrwxrwxrwx.   1 root root     9 5月  11 2019 lib64 -> usr/lib64
drwx------.   2 root root 16384 11月 26 2019 lost found
drwxr-xr-x.   2 root root  4096 5月  11 2019 media
drwxr-xr-x.   2 root root  4096 5月  11 2019 mnt
drwxr-xr-x.   3 root root  4096 11月  6 10:59 opt
dr-xr-xr-x  100 root root     0 11月  7 01:31 proc
dr-xr-x---.   6 root root  4096 2月  28 14:22 root
drwxr-xr-x   31 root root  1020 2月  23 11:34 run
lrwxrwxrwx.   1 root root     8 5月  11 2019 sbin -> usr/sbin
drwxr-xr-x.   2 root root  4096 5月  11 2019 srv
dr-xr-xr-x   13 root root     0 11月  7 01:31 sys
drwxrwxrwt.   4 root root  4096 3月   1 10:27 tmp
drwxr-xr-x.  12 root root  4096 11月 26 2019 usr
drwxr-xr-x.  20 root root  4096 11月 26 2019 var

2.2 变量

定义:变量就是让某一个特定字串代表不固定的内容

如系统变量 一般是大写 PATH、HOME、SHELL、MAIL

变量的取用:echo,一般会在变量前面加上$

代码语言:javascript复制
[root@VM-20-9-centos /]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@VM-20-9-centos /]# echo $HOME
/root
[root@VM-20-9-centos /]# echo $MAIL
/var/spool/mail/root
[root@VM-20-9-centos /]# echo $SHELL
/bin/bash

变量的设置:=,变量开头不能是数字,等号两边不能接空格

代码语言:javascript复制
[root@VM-20-9-centos /]# echo ${myname}

[root@VM-20-9-centos /]# myname=试一下设置变量
[root@VM-20-9-centos /]# echo ${myname}
试一下设置变

取消变量的设置:unset

代码语言:javascript复制
[root@VM-20-9-centos /]# echo ${myname}
试一下设置变量
[root@VM-20-9-centos /]# unset myname
[root@VM-20-9-centos /]# echo ${myname}

环境变量

env:列出来所有的环境变量

代码语言:javascript复制
[root@VM-20-9-centos /]# env
LS_COLORS=rs=0:di=38;5;33:ln=38;5;51:mh=00:pi=40;38;5;11:so=38;5;13:do=38;5;5:bd=48;5;232;38;5;11:cd=48;5;232;38;5;3:or=48;5;232;38;5;9:mi=01;05;37;41:su=48;5;196;38;5;15:sg=48;5;11;38;5;16:ca=48;5;196;38;5;226:tw=48;5;10;38;5;16:ow=48;5;10;38;5;21:st=48;5;21;38;5;15:ex=38;5;40:*.tar=38;5;9:*.tgz=38;5;9:*.arc=38;5;9:*.arj=38;5;9:*.taz=38;5;9:*.lha=38;5;9:*.lz4=38;5;9:*.lzh=38;5;9:*.lzma=38;5;9:*.tlz=38;5;9:*.txz=38;5;9:*.tzo=38;5;9:*.t7z=38;5;9:*.zip=38;5;9:*.z=38;5;9:*.dz=38;5;9:*.gz=38;5;9:*.lrz=38;5;9:*.lz=38;5;9:*.lzo=38;5;9:*.xz=38;5;9:*.zst=38;5;9:*.tzst=38;5;9:*.bz2=38;5;9:*.bz=38;5;9:*.tbz=38;5;9:*.tbz2=38;5;9:*.tz=38;5;9:*.deb=38;5;9:*.rpm=38;5;9:*.jar=38;5;9:*.war=38;5;9:*.ear=38;5;9:*.sar=38;5;9:*.rar=38;5;9:*.alz=38;5;9:*.ace=38;5;9:*.zoo=38;5;9:*.cpio=38;5;9:*.7z=38;5;9:*.rz=38;5;9:*.cab=38;5;9:*.wim=38;5;9:*.swm=38;5;9:*.dwm=38;5;9:*.esd=38;5;9:*.jpg=38;5;13:*.jpeg=38;5;13:*.mjpg=38;5;13:*.mjpeg=38;5;13:*.gif=38;5;13:*.bmp=38;5;13:*.pbm=38;5;13:*.pgm=38;5;13:*.ppm=38;5;13:*.tga=38;5;13:*.xbm=38;5;13:*.xpm=38;5;13:*.tif=38;5;13:*.tiff=38;5;13:*.png=38;5;13:*.svg=38;5;13:*.svgz=38;5;13:*.mng=38;5;13:*.pcx=38;5;13:*.mov=38;5;13:*.mpg=38;5;13:*.mpeg=38;5;13:*.m2v=38;5;13:*.mkv=38;5;13:*.webm=38;5;13:*.ogm=38;5;13:*.mp4=38;5;13:*.m4v=38;5;13:*.mp4v=38;5;13:*.vob=38;5;13:*.qt=38;5;13:*.nuv=38;5;13:*.wmv=38;5;13:*.asf=38;5;13:*.rm=38;5;13:*.rmvb=38;5;13:*.flc=38;5;13:*.avi=38;5;13:*.fli=38;5;13:*.flv=38;5;13:*.gl=38;5;13:*.dl=38;5;13:*.xcf=38;5;13:*.xwd=38;5;13:*.yuv=38;5;13:*.cgm=38;5;13:*.emf=38;5;13:*.ogv=38;5;13:*.ogx=38;5;13:*.aac=38;5;45:*.au=38;5;45:*.flac=38;5;45:*.m4a=38;5;45:*.mid=38;5;45:*.midi=38;5;45:*.mka=38;5;45:*.mp3=38;5;45:*.mpc=38;5;45:*.ogg=38;5;45:*.ra=38;5;45:*.wav=38;5;45:*.oga=38;5;45:*.opus=38;5;45:*.spx=38;5;45:*.xspf=38;5;45:
SSH_CONNECTION=58.251.76.253 52032 10.0.20.9 22
LANG=zh_CN.UTF-8
HISTTIMEFORMAT=%F %T 
HOSTNAME=VM-20-9-centos
OLDPWD=/root
XDG_SESSION_ID=66213
USER=root
PWD=/
HOME=/root
SSH_CLIENT=58.251.76.253 52032 22
SSH_TTY=/dev/pts/0
MAIL=/var/spool/mail/root
TERM=xterm-256color
SHELL=/bin/bash
SHLVL=1
PROMPT_COMMAND=history -a; history -a; printf "33]0;%s@%s:%s07" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"
LOGNAME=root
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/0/bus
XDG_RUNTIME_DIR=/run/user/0
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
HISTSIZE=3000
LESSOPEN=||/usr/bin/lesspipe.sh %s
_=/usr/bin/env

set:列出所有变量(包括 环境变量、自定义变量、与 bash 操作接口有关的变量)

代码语言:javascript复制
[root@VM-20-9-centos /]# set
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:login_shell:progcomp:promptvars:sourcepath
BASHRCSOURCED=Y
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_COMPLETION_VERSINFO=([0]="2" [1]="7")
BASH_LINENO=()
BASH_REMATCH=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="4" [1]="4" [2]="19" [3]="1" [4]="release" [5]="x86_64-redhat-linux-gnu")
BASH_VERSION='4.4.19(1)-release'
COLUMNS=179
COMP_WORDBREAKS=$' tn"'><=;|&(:'
...

export:后面不接变量则和env的作用一样,显示所有环境变量,若接了变量 export 变量,则可以把当前变量添加到环境变量中,用unset 变量可以把该变量从环境变量中取消

代码语言:javascript复制
[root@VM-20-9-centos /]# myname=测试一下环境变量
[root@VM-20-9-centos /]# export myname
[root@VM-20-9-centos /]# export
declare -x DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/0/bus"
declare -x HISTSIZE="3000"
declare -x HISTTIMEFORMAT="%F %T "
declare -x HOME="/root"
declare -x HOSTNAME="VM-20-9-centos"
declare -x LANG="zh_CN.UTF-8"
declare -x LESSOPEN="||/usr/bin/lesspipe.sh %s"
declare -x LOGNAME="root"
declare -x LS_COLORS="rs=0:di=38;5;33:ln=38;5;51:mh=00:pi=40;38;5;11:so=38;5;13:do=38;5;5:bd=48;5;232;38;5;11:cd=48;5;232;38;5;3:or=48;5;232;38;5;9:mi=01;05;37;41:su=48;5;196;38;5;15:sg=48;5;11;38;5;16:ca=48;5;196;38;5;226:tw=48;5;10;38;5;16:ow=48;5;10;38;5;21:st=48;5;21;38;5;15:ex=38;5;40:*.tar=38;5;9:*.tgz=38;5;9:*.arc=38;5;9:*.arj=38;5;9:*.taz=38;5;9:*.lha=38;5;9:*.lz4=38;5;9:*.lzh=38;5;9:*.lzma=38;5;9:*.tlz=38;5;9:*.txz=38;5;9:*.tzo=38;5;9:*.t7z=38;5;9:*.zip=38;5;9:*.z=38;5;9:*.dz=38;5;9:*.gz=38;5;9:*.lrz=38;5;9:*.lz=38;5;9:*.lzo=38;5;9:*.xz=38;5;9:*.zst=38;5;9:*.tzst=38;5;9:*.bz2=38;5;9:*.bz=38;5;9:*.tbz=38;5;9:*.tbz2=38;5;9:*.tz=38;5;9:*.deb=38;5;9:*.rpm=38;5;9:*.jar=38;5;9:*.war=38;5;9:*.ear=38;5;9:*.sar=38;5;9:*.rar=38;5;9:*.alz=38;5;9:*.ace=38;5;9:*.zoo=38;5;9:*.cpio=38;5;9:*.7z=38;5;9:*.rz=38;5;9:*.cab=38;5;9:*.wim=38;5;9:*.swm=38;5;9:*.dwm=38;5;9:*.esd=38;5;9:*.jpg=38;5;13:*.jpeg=38;5;13:*.mjpg=38;5;13:*.mjpeg=38;5;13:*.gif=38;5;13:*.bmp=38;5;13:*.pbm=38;5;13:*.pgm=38;5;13:*.ppm=38;5;13:*.tga=38;5;13:*.xbm=38;5;13:*.xpm=38;5;13:*.tif=38;5;13:*.tiff=38;5;13:*.png=38;5;13:*.svg=38;5;13:*.svgz=38;5;13:*.mng=38;5;13:*.pcx=38;5;13:*.mov=38;5;13:*.mpg=38;5;13:*.mpeg=38;5;13:*.m2v=38;5;13:*.mkv=38;5;13:*.webm=38;5;13:*.ogm=38;5;13:*.mp4=38;5;13:*.m4v=38;5;13:*.mp4v=38;5;13:*.vob=38;5;13:*.qt=38;5;13:*.nuv=38;5;13:*.wmv=38;5;13:*.asf=38;5;13:*.rm=38;5;13:*.rmvb=38;5;13:*.flc=38;5;13:*.avi=38;5;13:*.fli=38;5;13:*.flv=38;5;13:*.gl=38;5;13:*.dl=38;5;13:*.xcf=38;5;13:*.xwd=38;5;13:*.yuv=38;5;13:*.cgm=38;5;13:*.emf=38;5;13:*.ogv=38;5;13:*.ogx=38;5;13:*.aac=38;5;45:*.au=38;5;45:*.flac=38;5;45:*.m4a=38;5;45:*.mid=38;5;45:*.midi=38;5;45:*.mka=38;5;45:*.mp3=38;5;45:*.mpc=38;5;45:*.ogg=38;5;45:*.ra=38;5;45:*.wav=38;5;45:*.oga=38;5;45:*.opus=38;5;45:*.spx=38;5;45:*.xspf=38;5;45:"
declare -x MAIL="/var/spool/mail/root"
declare -x OLDPWD="/root"
declare -x PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"
declare -x PROMPT_COMMAND="history -a; history -a; printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/\~}""
declare -x PWD="/"
declare -x SHELL="/bin/bash"
declare -x SHLVL="1"
declare -x SSH_CLIENT="58.251.76.253 52032 22"
declare -x SSH_CONNECTION="58.251.76.253 52032 10.0.20.9 22"
declare -x SSH_TTY="/dev/pts/0"
declare -x TERM="xterm-256color"
declare -x USER="root"
declare -x XDG_RUNTIME_DIR="/run/user/0"
declare -x XDG_SESSION_ID="66213"
declare -x myname="测试一下环境变量"

locale:查询语系

代码语言:javascript复制
[root@VM-20-9-centos /]# locale -a
C
C.utf8
en_AG
en_AU
en_AU.utf8
en_BW
en_BW.utf8
en_CA
en_CA.utf8
en_DK
en_DK.utf8
en_GB
en_GB.iso885915
en_GB.utf8
en_HK
en_HK.utf8
en_IE
en_IE@euro
en_IE.utf8
...
[root@VM-20-9-centos /]# locale
LANG=zh_CN.UTF-8
LC_CTYPE="zh_CN.UTF-8"
LC_NUMERIC="zh_CN.UTF-8"
LC_TIME="zh_CN.UTF-8"
LC_COLLATE="zh_CN.UTF-8"
LC_MONETARY="zh_CN.UTF-8"
LC_MESSAGES="zh_CN.UTF-8"
LC_PAPER="zh_CN.UTF-8"
LC_NAME="zh_CN.UTF-8"
LC_ADDRESS="zh_CN.UTF-8"
LC_TELEPHONE="zh_CN.UTF-8"
LC_MEASUREMENT="zh_CN.UTF-8"
LC_IDENTIFICATION="zh_CN.UTF-8"
LC_ALL=
[root@VM-20-9-centos /]# cat etc/locale.conf 
LANG=en_US.UTF-8

变量键盘读取、阵列与宣告

read:读取来自键盘变量的输入

代码语言:javascript复制
[root@VM-20-9-centos /]# read test 		//按下回车后请输入内容
你好
[root@VM-20-9-centos /]# echo $test
你好

declare:宣告变量的类型

array:类似于数组

代码语言:javascript复制
[root@VM-20-9-centos /]# var[1]=1
[root@VM-20-9-centos /]# var[2]=2
[root@VM-20-9-centos /]# var[3]=3
[root@VM-20-9-centos /]# echo ${var[1]}, ${var[2]}, ${var[3]}
1, 2, 3

命令执行的判断依据

cmd1 ; cmd2:可以一次执行多个指令,用 ; 隔开

代码语言:javascript复制
[root@VM-20-9-centos /]# echo aa;echo bb
aa
bb

cmd1 && cmd2cmd1 || cmd2

代码语言:javascript复制
[root@VM-20-9-centos /]# echo a && echo b
a
b
[root@VM-20-9-centos /]# echo a || echo b
a

2.3 管线命令(pipe)

如果一群数据必需要经过几道手续之后才能得到我们所想要的格式,应该如何来设置?这就牵涉到管线命令的问题了 (pipe),管线命令使用的是“ | ”这个界定符号! 另外,管线命令与“连续下达命令”是不一样的

在每个管线后面接的第一个数据必定是“指令”!而且这个指令必须要能够接受standard input 的数据才行,这样的指令才可以是为“管线命令”,例如 less, more, head, tail等都是可以接受 standard input 的管线命令啦。至于例如 ls, cp, mv 等就不是管线命令了!因为 ls, cp, mv 并不会接受来自 stdin 的数据

2.3.1 撷取命令 cut、grep

cut:可以把一段信息的某一段切出来,以行为单位,cut 在处理多空格相连的数据时,可能会比较吃力

代码语言:javascript复制
[root@VM-20-9-centos /]# last
root     pts/1        58.251.76.253    Tue Mar  1 13:44   still logged in
root     pts/0        58.251.76.253    Tue Mar  1 10:24 - 14:31  (04:07)
root     pts/0        58.251.76.253    Tue Mar  1 10:17 - 10:24  (00:06)
root     pts/0        58.251.76.253    Mon Feb 28 09:51 - 14:22  (04:31)
root     pts/0        58.251.76.253    Wed Feb 23 16:40 - 17:49  (01:08)
root     pts/1        58.251.76.253    Fri Feb 18 14:00 - 20:10  (06:09)
root     pts/0        58.251.76.253    Fri Feb 18 11:39 - 14:33  (02:54)
lighthou pts/0        81.69.102.16     Wed Feb 16 00:10 - 00:11  (00:01)
lighthou pts/0        81.69.102.25     Wed Feb 16 00:05 - 00:06  (00:01)
lighthou pts/0        81.69.102.18     Wed Feb 16 00:00 - 00:02  (00:02)
lighthou pts/0        81.69.102.22     Tue Feb 15 23:57 - 23:58  (00:01)
...
[root@VM-20-9-centos /]# last | cut -d ' ' -f 1
root
root
root
root
root
root
root
lighthou
lighthou
lighthou
lighthou
...

grep:分析一行讯息,若当中有我们需要的信息,则把该行拿出来

代码语言:javascript复制
[root@VM-20-9-centos /]# last | grep 'root' -n
1:root     pts/1        58.251.76.253    Tue Mar  1 13:44   still logged in
2:root     pts/0        58.251.76.253    Tue Mar  1 10:24 - 14:31  (04:07)
3:root     pts/0        58.251.76.253    Tue Mar  1 10:17 - 10:24  (00:06)
4:root     pts/0        58.251.76.253    Mon Feb 28 09:51 - 14:22  (04:31)
5:root     pts/0        58.251.76.253    Wed Feb 23 16:40 - 17:49  (01:08)
6:root     pts/1        58.251.76.253    Fri Feb 18 14:00 - 20:10  (06:09)
7:root     pts/0        58.251.76.253    Fri Feb 18 11:39 - 14:33  (02:54)
2.3.2 排序命令 sort、wc、uniq

sort:排序

如下默认以开头的第一个字母排序

代码语言:javascript复制
[root@VM-20-9-centos /]# cat etc/group
root: x:0:
bin: x:1:
daemon: x:2:
sys: x:3:
adm: x:4:
tty: x:5:
disk: x:6:
[root@VM-20-9-centos /]# cat etc/group | sort
adm: x:4:
audio: x:63:
bin: x:1:
cdrom: x:11:
cgred: x:983:
chrony: x:987:

uniq:重复的数据仅显示一行

代码语言:javascript复制
[root@VM-20-9-centos /]# last
root     pts/1        58.251.76.253    Tue Mar  1 13:44   still logged in
root     pts/0        58.251.76.253    Tue Mar  1 10:24 - 14:31  (04:07)
root     pts/0        58.251.76.253    Tue Mar  1 10:17 - 10:24  (00:06)
root     pts/0        58.251.76.253    Mon Feb 28 09:51 - 14:22  (04:31)
root     pts/0        58.251.76.253    Wed Feb 23 16:40 - 17:49  (01:08)
root     pts/1        58.251.76.253    Fri Feb 18 14:00 - 20:10  (06:09)
root     pts/0        58.251.76.253    Fri Feb 18 11:39 - 14:33  (02:54)
lighthou pts/0        81.69.102.16     Wed Feb 16 00:10 - 00:11  (00:01)
lighthou pts/0        81.69.102.25     Wed Feb 16 00:05 - 00:06  (00:01)
lighthou pts/0        81.69.102.18     Wed Feb 16 00:00 - 00:02  (00:02)
lighthou pts/0        81.69.102.22     Tue Feb 15 23:57 - 23:58  (00:01)
...
[root@VM-20-9-centos /]# last | cut -d ' ' -f 1 | sort | uniq -c
      1 
     67 lighthou
      3 reboot
     28 root
      1 wtmp

wc:显示某个文件里面有多少字、多少行、多少字符

代码语言:javascript复制
[root@VM-20-9-centos /]# cat etc/passwd | wc
     33      70    1719

输出的三个数字中,分别代表: “行、字数、字符数”

2.3.3 双向重导向 tee

可以将数据流的处理过程中某段讯息存下来,同时输出到屏幕供下个指令继续处理

代码语言:javascript复制
[root@VM-20-9-centos ~]# ls
[root@VM-20-9-centos ~]# last | tee last.list
root     pts/1        58.251.76.253    Tue Mar  1 13:44   still logged in
root     pts/0        58.251.76.253    Tue Mar  1 10:24 - 14:31  (04:07)
root     pts/0        58.251.76.253    Tue Mar  1 10:17 - 10:24  (00:06)
root     pts/0        58.251.76.253    Mon Feb 28 09:51 - 14:22  (04:31)
root     pts/0        58.251.76.253    Wed Feb 23 16:40 - 17:49  (01:08)
root     pts/1        58.251.76.253    Fri Feb 18 14:00 - 20:10  (06:09)
root     pts/0        58.251.76.253    Fri Feb 18 11:39 - 14:33  (02:54)
lighthou pts/0        81.69.102.16     Wed Feb 16 00:10 - 00:11  (00:01)
lighthou pts/0        81.69.102.25     Wed Feb 16 00:05 - 00:06  (00:01)
lighthou pts/0        81.69.102.18     Wed Feb 16 00:00 - 00:02  (00:02)
lighthou pts/0        81.69.102.22     Tue Feb 15 23:57 - 23:58  (00:01)
...
[root@VM-20-9-centos ~]# ls
last.list
[root@VM-20-9-centos ~]# cat last.list 
root     pts/1        58.251.76.253    Tue Mar  1 13:44   still logged in
root     pts/0        58.251.76.253    Tue Mar  1 10:24 - 14:31  (04:07)
root     pts/0        58.251.76.253    Tue Mar  1 10:17 - 10:24  (00:06)
root     pts/0        58.251.76.253    Mon Feb 28 09:51 - 14:22  (04:31)
root     pts/0        58.251.76.253    Wed Feb 23 16:40 - 17:49  (01:08)
root     pts/1        58.251.76.253    Fri Feb 18 14:00 - 20:10  (06:09)
root     pts/0        58.251.76.253    Fri Feb 18 11:39 - 14:33  (02:54)
lighthou pts/0        81.69.102.16     Wed Feb 16 00:10 - 00:11  (00:01)
lighthou pts/0        81.69.102.25     Wed Feb 16 00:05 - 00:06  (00:01)
lighthou pts/0        81.69.102.18     Wed Feb 16 00:00 - 00:02  (00:02)
lighthou pts/0        81.69.102.22     Tue Feb 15 23:57 - 23:58  (00:01)
...
2.3.4 字符转换命令 tr、join、paste

tr:可以用来删除一段信息中的文字,或者是进行文字信息的替换,不改变原文件里的数据,只是改变展示出来的数据

代码语言:javascript复制
[root@VM-20-9-centos ~]# cat fileTest 
  Line 105:  Expected '!==' and instead saw '!='          eqeqeq
  Line 115:  Expected '!==' and instead saw '!='          eqeqeq
  Line 149:  Expected '===' and instead saw '=='          eqeqeq
  Line 154:  Expected '===' and instead saw '=='          eqeqeq
  Line 155:  Expected '!==' and instead saw '!='          eqeqeq
  Line 166:  Expected '===' and instead saw '=='          eqeqeq
  Line 194:  Expected '===' and instead saw '=='          eqeqeq
  Line 216:  Expected '===' and instead saw '=='          eqeqeq
  Line 262:  Expected '!==' and instead saw '!='
[root@VM-20-9-centos ~]# cat fileTest | tr -d '!'
  Line 105:  Expected '==' and instead saw '='          eqeqeq
  Line 115:  Expected '==' and instead saw '='          eqeqeq
  Line 149:  Expected '===' and instead saw '=='          eqeqeq
  Line 154:  Expected '===' and instead saw '=='          eqeqeq
  Line 155:  Expected '==' and instead saw '='          eqeqeq
  Line 166:  Expected '===' and instead saw '=='          eqeqeq
  Line 194:  Expected '===' and instead saw '=='          eqeqeq
  Line 216:  Expected '===' and instead saw '=='          eqeqeq
  Line 262:  Expected '==' and instead saw '='
[root@VM-20-9-centos ~]# cat fileTest 
  Line 105:  Expected '!==' and instead saw '!='          eqeqeq
  Line 115:  Expected '!==' and instead saw '!='          eqeqeq
  Line 149:  Expected '===' and instead saw '=='          eqeqeq
  Line 154:  Expected '===' and instead saw '=='          eqeqeq
  Line 155:  Expected '!==' and instead saw '!='          eqeqeq
  Line 166:  Expected '===' and instead saw '=='          eqeqeq
  Line 194:  Expected '===' and instead saw '=='          eqeqeq
  Line 216:  Expected '===' and instead saw '=='          eqeqeq
  Line 262:  Expected '!==' and instead saw '!='

join:两个文件当中,有 "相同数据" 的那一行加在一起,如下两个文件分别是4行和9行,且都有相同的数据,所用join后总共有36行

代码语言:javascript复制
[root@VM-20-9-centos ~]# cat fileTemp | nl
     1	  Line 9:   Expected a default case                     default-case
     2	  Line 20:  Expected a 'break' statement before 'case'  no-fallthrough
     3	  Line 30:  Expected a 'break' statement before 'case'  no-fallthrough
     4	  Line 48:  Expected a default case                     default-case
[root@VM-20-9-centos ~]# cat fileTest | nl
     1	  Line 105:  Expected '!==' and instead saw '!='          eqeqeq
     2	  Line 115:  Expected '!==' and instead saw '!='          eqeqeq
     3	  Line 149:  Expected '===' and instead saw '=='          eqeqeq
     4	  Line 154:  Expected '===' and instead saw '=='          eqeqeq
     5	  Line 155:  Expected '!==' and instead saw '!='          eqeqeq
     6	  Line 166:  Expected '===' and instead saw '=='          eqeqeq
     7	  Line 194:  Expected '===' and instead saw '=='          eqeqeq
     8	  Line 216:  Expected '===' and instead saw '=='          eqeqeq
     9	  Line 262:  Expected '!==' and instead saw '!='
[root@VM-20-9-centos ~]# join fileTemp fileTest | nl
     1	Line 9: Expected a default case default-case 105: Expected '!==' and instead saw '!=' eqeqeq
     2	Line 9: Expected a default case default-case 115: Expected '!==' and instead saw '!=' eqeqeq
     3	Line 9: Expected a default case default-case 149: Expected '===' and instead saw '==' eqeqeq
     4	Line 9: Expected a default case default-case 154: Expected '===' and instead saw '==' eqeqeq
     5	Line 9: Expected a default case default-case 155: Expected '!==' and instead saw '!=' eqeqeq
     6	Line 9: Expected a default case default-case 166: Expected '===' and instead saw '==' eqeqeq
     7	Line 9: Expected a default case default-case 194: Expected '===' and instead saw '==' eqeqeq
     8	Line 9: Expected a default case default-case 216: Expected '===' and instead saw '==' eqeqeq
     9	Line 9: Expected a default case default-case 262: Expected '!==' and instead saw '!='
    10	Line 20: Expected a 'break' statement before 'case' no-fallthrough 105: Expected '!==' and instead saw '!=' eqeqeq
    11	Line 20: Expected a 'break' statement before 'case' no-fallthrough 115: Expected '!==' and instead saw '!=' eqeqeq
    12	Line 20: Expected a 'break' statement before 'case' no-fallthrough 149: Expected '===' and instead saw '==' eqeqeq
    13	Line 20: Expected a 'break' statement before 'case' no-fallthrough 154: Expected '===' and instead saw '==' eqeqeq
    14	Line 20: Expected a 'break' statement before 'case' no-fallthrough 155: Expected '!==' and instead saw '!=' eqeqeq
    15	Line 20: Expected a 'break' statement before 'case' no-fallthrough 166: Expected '===' and instead saw '==' eqeqeq
    16	Line 20: Expected a 'break' statement before 'case' no-fallthrough 194: Expected '===' and instead saw '==' eqeqeq
    17	Line 20: Expected a 'break' statement before 'case' no-fallthrough 216: Expected '===' and instead saw '==' eqeqeq
    18	Line 20: Expected a 'break' statement before 'case' no-fallthrough 262: Expected '!==' and instead saw '!='
    19	Line 30: Expected a 'break' statement before 'case' no-fallthrough 105: Expected '!==' and instead saw '!=' eqeqeq
    20	Line 30: Expected a 'break' statement before 'case' no-fallthrough 115: Expected '!==' and instead saw '!=' eqeqeq
    21	Line 30: Expected a 'break' statement before 'case' no-fallthrough 149: Expected '===' and instead saw '==' eqeqeq
    22	Line 30: Expected a 'break' statement before 'case' no-fallthrough 154: Expected '===' and instead saw '==' eqeqeq
    23	Line 30: Expected a 'break' statement before 'case' no-fallthrough 155: Expected '!==' and instead saw '!=' eqeqeq
    24	Line 30: Expected a 'break' statement before 'case' no-fallthrough 166: Expected '===' and instead saw '==' eqeqeq
    25	Line 30: Expected a 'break' statement before 'case' no-fallthrough 194: Expected '===' and instead saw '==' eqeqeq
    26	Line 30: Expected a 'break' statement before 'case' no-fallthrough 216: Expected '===' and instead saw '==' eqeqeq
    27	Line 30: Expected a 'break' statement before 'case' no-fallthrough 262: Expected '!==' and instead saw '!='
    28	Line 48: Expected a default case default-case 105: Expected '!==' and instead saw '!=' eqeqeq
    29	Line 48: Expected a default case default-case 115: Expected '!==' and instead saw '!=' eqeqeq
    30	Line 48: Expected a default case default-case 149: Expected '===' and instead saw '==' eqeqeq
    31	Line 48: Expected a default case default-case 154: Expected '===' and instead saw '==' eqeqeq
    32	Line 48: Expected a default case default-case 155: Expected '!==' and instead saw '!=' eqeqeq
    33	Line 48: Expected a default case default-case 166: Expected '===' and instead saw '==' eqeqeq
    34	Line 48: Expected a default case default-case 194: Expected '===' and instead saw '==' eqeqeq
    35	Line 48: Expected a default case default-case 216: Expected '===' and instead saw '==' eqeqeq
    36	Line 48: Expected a default case default-case 262: Expected '!==' and instead saw '!='

paste:paste 就要比 join 简单多了,相对于 join 必须要比对两个文件的数据相关 性, paste 就直接“将两行贴在一起,且中间以 [tab] 键隔开”

代码语言:javascript复制
[root@VM-20-9-centos ~]# paste fileTemp fileTest | nl
     1	  Line 9:   Expected a default case                     default-case	  Line 105:  Expected '!==' and instead saw '!='          eqeqeq
     2	  Line 20:  Expected a 'break' statement before 'case'  no-fallthrough	  Line 115:  Expected '!==' and instead saw '!='          eqeqeq
     3	  Line 30:  Expected a 'break' statement before 'case'  no-fallthrough	  Line 149:  Expected '===' and instead saw '=='          eqeqeq
     4	  Line 48:  Expected a default case                     default-case	  Line 154:  Expected '===' and instead saw '=='          eqeqeq
     5		  Line 155:  Expected '!==' and instead saw '!='          eqeqeq
     6		  Line 166:  Expected '===' and instead saw '=='          eqeqeq
     7		  Line 194:  Expected '===' and instead saw '=='          eqeqeq
     8		  Line 216:  Expected '===' and instead saw '=='          eqeqeq
     9		  Line 262:  Expected '!==' and instead saw '!='
2.3.5 分区命令 split

split:可以将一个大文件按文件大小或行数来分区为小文件

如下将fileTest按每2行分成几个小文件,总共有9行,可以分为5个文件,fileSplitaa、fileSplitab、fileSplitac、fileSplitad、fileSplitae

代码语言:javascript复制
[root@VM-20-9-centos ~]# cat fileTest 
  Line 105:  Expected '!==' and instead saw '!='          eqeqeq
  Line 115:  Expected '!==' and instead saw '!='          eqeqeq
  Line 149:  Expected '===' and instead saw '=='          eqeqeq
  Line 154:  Expected '===' and instead saw '=='          eqeqeq
  Line 155:  Expected '!==' and instead saw '!='          eqeqeq
  Line 166:  Expected '===' and instead saw '=='          eqeqeq
  Line 194:  Expected '===' and instead saw '=='          eqeqeq
  Line 216:  Expected '===' and instead saw '=='          eqeqeq
  Line 262:  Expected '!==' and instead saw '!='
[root@VM-20-9-centos ~]# split -l 2 fileTest fileSplit
[root@VM-20-9-centos ~]# ls
fileSplitaa  fileSplitab  fileSplitac  fileSplitad  fileSplitae  fileTemp  fileTest  last.list
[root@VM-20-9-centos ~]# cat fileSplita*
  Line 105:  Expected '!==' and instead saw '!='          eqeqeq
  Line 115:  Expected '!==' and instead saw '!='          eqeqeq
  Line 149:  Expected '===' and instead saw '=='          eqeqeq
  Line 154:  Expected '===' and instead saw '=='          eqeqeq
  Line 155:  Expected '!==' and instead saw '!='          eqeqeq
  Line 166:  Expected '===' and instead saw '=='          eqeqeq
  Line 194:  Expected '===' and instead saw '=='          eqeqeq
  Line 216:  Expected '===' and instead saw '=='          eqeqeq
  Line 262:  Expected '!==' and instead saw '!='
2.3.6 减号 - 的用途

0 人点赞