shell中常用的基本命令(diff、patch、cut、sort、uniq、&&与||、test、tr)

diff 命令(比对文件)

名称方法
不检查空格字符的不同diff -b
不检查空白行diff -B
显示全部内文,并标出不同之处diff -c
不检查大小写的不同diff -i
若比较的文件为 C 语言的程序码文件时,显示差异所在的函数名称diff -p
仅显示有无差异,不显示详细的信息diff -q
比较子目录中的文件diff -r
以合并的方式来显示文件内容的不同diff -u
[root@shell_jichu mnt]# vim file
[root@shell_jichu mnt]# cat file
westos	hellow
text
[root@shell_jichu mnt]# vim file1
[root@shell_jichu mnt]# cat file1
westos	hellow
cai
[root@shell_jichu mnt]# diff file file1
2c2
< text
---
> cai

在这里插入图片描述

patch命令(打补丁)

名称方法
备份每一个原始文件patch -b
后面可以接“取消几层目录”的意思patch -p
代表还原,将新的文件还原成原来旧的版本patch -R
[root@shell_jichu mnt]# yum repolist 
已加载插件:langpacks
rhel_dvd                                                 | 4.1 kB     00:00     
(1/2): rhel_dvd/group_gz                                   | 136 kB   00:00     
(2/2): rhel_dvd/primary_db                                 | 3.9 MB   00:00     
源标识                     源名称                                          状态
rhel_dvd                   Remote classroom copy of dvd                    4,751
repolist: 4,751
[root@shell_jichu mnt]# 
[root@shell_jichu mnt]# yum install patch -y
已加载插件:langpacks
正在解决依赖关系
--> 正在检查事务
---> 软件包 patch.x86_64.0.2.7.1-8.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

================================================================================
 Package        架构            版本                    源                 大小
================================================================================
正在安装:
 patch          x86_64          2.7.1-8.el7             rhel_dvd          110 k

事务概要
================================================================================
安装  1 软件包

总下载量:110 k
安装大小:210 k
Downloading packages:
patch-2.7.1-8.el7.x86_64.rpm                               | 110 kB   00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : patch-2.7.1-8.el7.x86_64                                    1/1 
  验证中      : patch-2.7.1-8.el7.x86_64                                    1/1 

已安装:
  patch.x86_64 0:2.7.1-8.el7                                                    

完毕!
[root@shell_jichu mnt]# diff -u file file1 > file.path
[root@shell_jichu mnt]# ls
file  file1  file.path
[root@shell_jichu mnt]# patch -b file  file.path 
patching file file
[root@shell_jichu mnt]# cat file
westos	hellow
cai

在这里插入图片描述

cut命令(截取字符)

名称方法
指定分隔符cut  -d
截取1和4列cut  -f 1,4
指定截取的字符位置cut  -c 1-4
[root@shell_jichu mnt]# cp -p /etc/passwd /mnt/passwd
[root@shell_jichu mnt]# ls
file  file1  file1.rej  file.orig  file.path  passwd
[root@shell_jichu mnt]# vim passwd 
[root@shell_jichu mnt]# cut -d : -f 3 passwd 
0
1
2
3
4
5
6
7
8
42
993
72
[root@shell_jichu mnt]# cut -d : -f 1-3 passwd 
root:x:0
bin:x:1
daemon:x:2
adm:x:3
lp:x:4
sync:x:5
shutdown:x:6
halt:x:7
mail:x:8
gdm:x:42
gnome-initial-setup:x:993
tcpdump:x:72


在这里插入图片描述

sort(字符排序)

名称方法
纯数字排序sort  -n
倒序sort  -r
去掉重复数字sort  -u
输出到指定文件中sort  -o
指定要排序的列sort  -k
指定分隔符sort  -t
[root@shell_jichu mnt]# vim num
[root@shell_jichu mnt]# cat num 
1
5
6
22
22
62
15
51
65
1
4
4
565

165
[root@shell_jichu mnt]# sort num 

1
1
15
165
22
22
4
4
5
51
565
6
62
65
[root@shell_jichu mnt]# sort -n num 

1
1
4
4
5
6
15
22
22
51
62
65
165
565
[root@shell_jichu mnt]# sort -nr num 
565
165
65
62
51
22
22
15
6
5
4
4
1
1

[root@shell_jichu mnt]# sort -nru num 
565
165
65
62
51
22
15
6
5
4
1

在这里插入图片描述

uniq(处理重复的字符)

名称方法
显示唯一的行uniq  -u
显示重复的行uniq  -d
每行显示一次并统计重复次数uniq  -c
 每行仅显示一次,并且显示出现的次数
[root@shell_jichu mnt]# sort -n num | uniq -c
      1 
      2 1
      2 4
      1 5
      1 6
      1 15
      2 22
      1 51
      1 62
      1 65
      1 165
      1 565
显示重复的行并且进行排序
[root@shell_jichu mnt]# sort -n num | uniq -d 
1
4
22
显示唯一的行并且排序
[root@shell_jichu mnt]# sort -n num | uniq -u

5
6
15
51
62
65
165
565

&&命令和||命令

&&————(表示true条件为真):用来执行条件成立后执行的命令
||—————(表示false条件为假):用来执行条件不成立后执行的命令

test命令——用来检测某个条件是否成立

test "A&quot;==&quot;A"=="B” 等同 [ "A&quot;==&quot;A"=="B" ]
名称方法
相等[ ‘A’ = ‘B’ ]
不相等[ ‘A’ != ‘B’ ]
等于[ ‘A’ -eq ‘B’ ]
不等于[ ‘A’ -ne ‘B’ ]
不大于[ ‘A’ le ‘B’ ]
小于[ ‘A’ lt ‘B’ ]
不小于[ ‘A’ ge ‘B’ ]
大于[ ‘A’ gt ‘B’ ]
都满足才成立A -a B
满足一个成立A -o B
判断是否为空,空为真,非空为假-z “$A”
和条件相反就是真,非空为真,空为假-n “$A”
判断前后两个文件是否互相为硬链接[“file1” -ef “file2” ]
判断前面的硬链接是否与新[“file1” -nt “file2” ]
判断前面的硬链接是否旧[“file1” -ot “file2” ]
判断文件是否存在[-e “file” ]
判断文件是否为普通文件[-f “file” ]
是否为软链接[-L “file” ]
判断是否为套接字[-S “file” ]
判断是否为块设备[-b “file” ]
判断是否为目录[-d “file” ]
判断是否为字符设备[-c “file” ]

[root@shell_jichu mnt]# a=1;b=1
A等于B
[root@shell_jichu mnt]# [ “a&quot;=&quot;a&quot; = &quot;a"="b” ] && echo yes || echo no
yes
A等于B
[root@shell_jichu mnt]# [ “a&quot;−eq&quot;a&quot; -eq &quot;a"eq"b” ] && echo yes || echo no
yes
[root@shell_jichu mnt]# a=2;b=1
A不等于B
[root@shell_jichu mnt]# [ “a&quot;!=&quot;a&quot; != &quot;a"!="b” ] && echo yes || echo no
yes
A不等于B
[root@shell_jichu mnt]# [ “a&quot;−ne&quot;a&quot; -ne &quot;a"ne"b” ] && echo yes || echo no
yes
A大于等于B
[root@shell_jichu mnt]# [ “a&quot;−ge&quot;a&quot; -ge &quot;a"ge"b” ] && echo yes || echo no
yes
A大于B
[root@shell_jichu mnt]# [ “a&quot;−gt&quot;a&quot; -gt &quot;a"gt"b” ] && echo yes || echo no
yes
A小于等于B
[root@shell_jichu mnt]# [ “a&quot;−le&quot;a&quot; -le &quot;a"le"b” ] && echo yes || echo no
no
A小于B A小于B
[root@shell_jichu mnt]# [ “a&quot;−lt&quot;a&quot; -lt &quot;a"lt"b” ] && echo yes || echo no
no
在这里插入图片描述

tr——转换大小写

[root@shell_jichu mnt]# echo qwertyuiop | tr ‘a-z’ ‘A-Z’
QWERTYUIOP
[root@shell_jichu mnt]# echoASDFGHJKL | tr ‘a-z’ ‘A-Z’
bash: echoASDFGHJKL: 未找到命令…
[root@shell_jichu mnt]# echoASDFGHJKL | tr ‘A-Z’ ‘a-z’
bash: echoASDFGHJKL: 未找到命令…
[root@shell_jichu mnt]# echo ASDFGHJKL | tr ‘A-Z’ ‘a-z’
asdfghjkl
[root@shell_jichu mnt]# echo ASDFGHJKL | tr ‘a-z’ ‘A-Z’
ASDFGHJKL
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值