1. diff
- 用法:
diff [options] files | directorys - 输出信息:
[num1,num2] [a|c|d] [num3,num4] -
字符 含义 num1,num2 第一个文件中的行 a 添加 c 更改 d 删除 < 第一个文件中的内容 > 第二个文件中的内容 num3,num4 第二个文件中的行
实验前提:
[root@westos_student50 mnt]# cat westos
hello westos
[root@westos_student50 mnt]# cat westos.new
hello westos
123
[root@westos_student50 mnt]# diff westos westos.new
1a2 第一个文件添加第二个文件的第二行123就与第二个文件一致
> 123
[root@westos_student50 mnt]# diff westos.new westos
2d1 第一个文件中如果删掉第二行123就与第二个文件一致
< 123
参数 | 含义 |
---|---|
-b | 忽略空格 |
-B | 忽略空行 |
-i | 忽略大小写 |
-c | 显示文件所有内容并标示不同 |
-r | 对比目录 |
-u | 合并输出 |
[root@westos_student50 mnt]# diff -b westos westos.new 加-b忽略文件中的空格
[root@westos_student50 mnt]# diff -B westos westos.new 加-B忽略空行
[root@westos_student50 mnt]# diff -i westos westos.new 加-i忽略大小写
[root@westos_student50 mnt]# diff -c westos westos.new显示整个内容并用!标识不同的行
[root@westos_student50 mnt]# diff -r test test1加-r对比两个目录的不同
[root@westos_student50 mnt]# diff -u westos westos.new 加-u表示合并输出(多用于生成补丁
2.patch(用法: patch + 原文件 + 补丁文件)
[root@westos_student50 mnt]# diff -u westos westos.new > westos.path 生成补丁
[root@westos_student50 mnt]# dnf install patch -y
[root@westos_student50 mnt]# patch westos westos.path给westos文件打补丁生成补丁文件westos.path
[root@westos_student50 mnt]# patch -b westos westos.path在打补丁的同时备份源文件 3.cut
参数 | 含义 |
---|---|
-d : | 指定 : 为分割符 |
-f | 指定显示的列 ,5表示第五列;3,5 表示3和5列;3-5 表示 3到5列 |
-c | 指定截取的字符(数字用法同-f) |
5-表示第五列以后 -5 表示到第五列
[root@westos_student50 mnt]# cp /etc/passwd .
[root@westos_student50 mnt]# vim passwd删掉多余行
[root@westos_student50 mnt]# cut -d : -f 1 passwd -d : 表示指定分割符为:-f表示产看第几列
[root@westos_student50 mnt]# cut -d : -f 1,7 passwd查看以:为分隔符的第一列和第七列 [root@westos_student50 mnt]# cut -d : -f 1-3 passwd查看以:为分隔符的第一列到第三列
[root@westos_student50 mnt]# cut -d : -f 3- passwd查看以:为分隔符的第三列到最后一列
[root@westos_student50 mnt]# cut -c 1-4 passwd查看第一个到第四个字符
[root@westos_student50 mnt]# cut -c 1,4 passwd 查看第一个和第四个字符
4. sort
参数 | 含义 |
---|---|
-n | 纯数字排序 |
-r | 倒叙 |
-u | 去掉重复 |
-o | 输出到指定文件 |
-t | 指定分割符 |
-k | 指定排序的列 |
[root@westos_student50 mnt]# vim westos
[root@westos_student50 mnt]# sort westos把每一行第一个数字进行排序
[root@westos_student50 mnt]# sort -n westos 按照数字大小排序
[root@westos_student50 mnt]# sort -nr westos 按数字大小倒叙 [root@westos_student50 mnt]# sort -nru westos 去掉重复的进行倒叙 [root@westos_student50 mnt]# sort -nru westos -o file 去掉重复的进行倒叙然后输出到file [root@westos_student50 mnt]# vim westos在文件中添加分割符:
[root@westos_student50 mnt]# sort westos默认以第一列排序
[root@westos_student50 mnt]# sort -t : -k 2 -n westos 加-t : 表示指定分割符: -k 2表示排序第二列 -n表示数字大小排序
5. uniq
参数 | 含义 |
---|---|
-c | 合并重复并统计重复个数 |
-d | 显示重复的行 |
-u | 显示唯一的行 |
[root@westos_student50 mnt]# vim westos
[root@westos_student50 mnt]# sort -n westos | uniq -c按数字大小排序,合并重复数字并统计重复个数
[root@westos_student50 mnt]# sort -n westos | uniq -u 按数字大小排序并只显示唯一的数字
[root@westos_student50 mnt]# sort -n westos | uniq -d 按数字大小排序并只显示重复的数字
[root@westos_student50 mnt]# rev file倒叙文件内容
4 3 2 1
[root@westos_student50 mnt]# tac file正序文件内容
1 2 3 4
6.tr(只能替换单个字符)
命令 | 含义 |
---|---|
tr ‘a-z’ ‘A-Z’ | 小写转大写 |
tr ‘A-Z’ ‘a-z’ | 大写转小写 |
[root@westos_student50 mnt]# tr 'l' 'w' < westos将westos中的l全部替换为w
[root@westos_student50 mnt]# tr 'a-z' 'A-Z' < westos将小写字母替换为大写字母
7.test
test = [] [] 就相当于test命令 "test $a = $b" = [ "$a" = "$b" ]
test 数字对比
字符 | 含义 |
---|---|
! | 取反 |
!= | 不等于 |
-eq | 等于 |
-ne | 不等于 |
-le | 小于等于 |
-lt | 小于 |
-ge | 大于等于 |
-gt | 大于 |
[root@westos_student50 mnt]# a=1
[root@westos_student50 mnt]# b=1
[root@westos_student50 mnt]# c=2设定a=1 b=1 c=2
[root@westos_student50 mnt]# test "$a" = "$b" && echo yes || echo no判断a是否等于b ,等于输出yes 不等于输出no
[root@westos_student50 mnt]# [ "$a" = "$b" ] && echo yes || echo no判断a是否等于b ,等于输出yes 不等于输出no
[root@westos_student50 mnt]# [ "$a" != "$b" ] && echo yes || echo no判断a是否不等于b ,正确输出yes 不正确输出no
[root@westos_student50 mnt]# [ "$a" -eq "$b" ] && echo yes || echo no判断a是否等于b ,等于输出yes 不等于输出no (-eq表示等于)
[root@westos_student50 mnt]# [ "$a" -ne "$b" ] && echo yes || echo no判断a是否不等于b ,正确输出yes 不正确输出no (-ne表示不等于)
[root@westos_student50 mnt]# [ "$a" -ge "$b" ] && echo yes || echo no判断a是否大于等于b ,正确输出yes 不正确输出no (-ge表示大于等于)
[root@westos_student50 mnt]# [ "$a" -gt "$b" ] && echo yes || echo no判断a是否大于b ,正确输出yes 不正确输出no (-gt表示大于)
[root@westos_student50 mnt]# [ "$a" -lt "$b" ] && echo yes || echo no判断a是否小于b ,正确输出yes 不正确输出no (-lt表示小于)
[root@westos_student50 mnt]# [ "$a" -le "$b" ] && echo yes || echo no判断a是小于等于b ,正确输出yes 不正确输出no (-le表示小于等于)
test 的条件关系
参数 | 含义 |
---|---|
-a | 并且 |
-o | 或者 |
[root@westos_student50 mnt]# a=20
[root@westos_student50 mnt]# [ "$a" -gt "0" -a "$a" -lt "10" ] && echo yes || echo no 判断a是否大于0并且小于10 正确输出yes 不正确输出no
[root@westos_student50 mnt]# [ "$a" -lt "0" -o "$a" -gt "10" ] && echo yes || echo no判断是否a小于0或者大于10 正确输出yes 不正确输出no
[root@westos_student50 mnt]# [ ! "$a" -gt "0" -a "$a" -lt "10" ] && echo yes || echo no 反选判断a是否大于0并且小于10 正确输出yes 不正确输出no
test对空的判定
参数 | 含义 |
---|---|
-n | nozero 判定内容不为空 |
-z | zero 判定内容为空 |
[root@westos_student50 mnt]# [ -z "$d" ] && echo yes || echo no判断d是否为空,正确输出yes 不正确输出no
[root@westos_student50 mnt]# [ -n "$d" ] && echo yes || echo no判断d是否不为空,正确输出yes 不正确输出no
test 对于文件的判定
参数 | 含义 |
---|---|
-ef | 文件节点号是否一致(硬链) |
-nt | 文件1是不是比文件2新 |
-ot | 文件1是不是比文件2老 |
-d | 目录 |
-S | 套结字 |
-L | 软链接 |
-e | 存在 |
-f | 普通文件 |
-b | 块设备 |
-c | 字符设备 |
[root@westos_student50 mnt]# [ "/mnt/test.sh" -nt "westos" ] && echo yes || echo no判断test.sh是否比westos新,正确输出yes 不正确输出no
[root@westos_student50 mnt]# [ "/mnt/test.sh" -ot "westos" ] && echo yes || echo no判断test.sh是否比westos老,正确输出yes 不正确输出no
[root@westos_student50 mnt]# ln test.sh test1.sh
[root@westos_student50 mnt]# [ "/mnt/test.sh" -ef "test1.sh" ] && echo yes || echo no判断test.sh和test1.sh节点号是否一致,正确输出yes 不正确输出no
[root@westos_student50 mnt]# [ -e "/mnt/haha" ] && echo yes || echo no判断/mnt/haha是否存在,正确输出yes 不正确输出no
[root@westos_student50 mnt]# [ -f "/mnt/test.sh" ] && echo yes || echo no判断/mnt/test.sh是不是普通文件,正确输出yes 不正确输出no
8.&& ||
&& 符合条件作动作
|| 不符合条件作动作
[root@westos_student50 mnt]# ls /mnt/haha &> /dev/null && echo yes ||echo no 查看/mnt将结果扔到回收站,命令在执行的时候没有任何报错输出yes,命令在执行的时候有报错输出no
[root@westos_student50 mnt]# ls /mnt && echo yes ||echo no 浏览/mnt,符合输出结果和yes,不符合输出no
[root@westos_student50 mnt]# vim test.sh(一次使用多条命令)
[root@westos_student50 mnt]# sh test.sh