diff 命令
diff 命令是用来比较两个文件或者目录的不同的。
diff [option] file1 file2
diff [option] dir1 dir2
diff 在比较文件过程中结果的读取方式:
【num1】【a|c|d】【num2】
num1 表示在第一个文件中的行数
num2 表示在第二个文件中的行数
a 表示增加
c 表示改变
d 表示删除
< 表示第一个文件中的内容
> 表示第二个文件中的内容
diff 在比较目录过程中结果的读取
Only in directory:filename
表示值在directory目录中存在filename文件
1.diff -b ##比较时不检查空格字符
[root@localhost mnt]# cat top1
hello welcome
[root@localhost mnt]# cat top2
hello welcome #文件top1 和文件top2 不同仅在于 空格数不一致
[root@localhost mnt]# diff top1 top2
1c1 # 改变第一个文件的第一行才能够匹配到第二个文件 的第一行
< hello welcome #第一个文件的内容
--- # 分割线
> hello welcome #第二个文件的内容
[root@localhost mnt]# diff -b top1 top2 # 使用-b 参数,忽略空格 两文件没有不同。
[root@localhost mnt]#
2.diff -B ##不检查空白行
[root@localhost mnt]# cat top1
hello welcome
[root@localhost mnt]# cat top2 # 第一个文件比第二个文件多一个空行
hello welcome
[root@localhost mnt]# diff top1 top2
2d1 # 删除第一个文件的第2行 才能匹配到第二个文件的第一行
< # 第一个文件的内容,空行
[root@localhost mnt]# diff -B top1 top2 # 使用参数-B 忽略空行 两文件一致
[root@localhost mnt]#
3.diff -c ##显示全部内容并标明不同之处
[root@localhost mnt]# diff -c top1 top2
*** top1 2019-05-20 00:06:33.881059761 -0400 #***表示第一个文件
--- top2 2019-05-19 23:53:39.686335173 -0400 #---表示第二个文件
***************
*** 1,2 ****
! hello welcome haha #!表示存在不同地方
!
--- 1 ----
! hello welcome
[root@localhost mnt]#
4.diff -i ##不检查大小写的不同
[root@localhost mnt]# cat top1
Hello Welcome
[root@localhost mnt]# cat top2 # 文件1 和文件2 只是存在字母大小写的不同
hello welcome
[root@localhost mnt]# diff top1 top2
1c1 # 改变第一个文件的第一行 才能匹配到第二个文件的第一行
< Hello Welcome # 第一个文件的内容
---
> hello welcome #第二个文件的内容
[root@localhost mnt]# diff -i top1 top2 # 使用参数-i 忽略大小写 两文件内容一致
[root@localhost mnt]#
5.diff -q ##仅显示有无差异不显示详细信息
[root@localhost mnt]# diff top1 top2
1c1
< Hello Welcome
---
> hello welcome
[root@localhost mnt]# diff -q top1 top2 # 使用参数-q
Files top1 and top2 differ # 只说明两个文件存在不同之处
6.diff -u ##以合并的方式比较两文件的不同
[root@localhost mnt]# diff top1 top2
1c1
< Hello Welcome
---
> hello welcome # 正常的方式比较两个文件的不同
[root@localhost mnt]# diff -u top1 top2 #合并的方式比较两个文件的不同
--- top1 2019-05-20 00:10:21.470273503 -0400 #---表示一个文件
+++ top2 2019-05-19 23:53:39.686335173 -0400 #++ 表示第二个文件
@@ -1 +1 @@ # 第一个文件的第一行 第二个文件的第一行
-Hello Welcome # 第一个文件的内容
+hello welcome # 第二个文件的内容
7.diff -p ##当检查的为c语言程序码文件时,显示差异所在的函数名称
[root@localhost mnt]# diff hello.c hello1.c # 两个c程序代码文件 比较不同
4c4 # 第一个文件的第四行 改变后 可以匹配第二个文件的第四行
< printf ("hello welcome\n")
---
> printf ("hello welcome\n")
[root@localhost mnt]# diff -p hello.c hello1.c # 使用-p参数之后 显示存在不同的函数名称
*** hello.c 2019-05-20 00:30:39.774413382 -0400
--- hello1.c 2019-05-20 00:31:10.598442464 -0400
***************
*** 1,5 ****
#include<stdio.h>
main() # 不同所在函数名称
{
! printf ("hello welcome\n") # ! 表示不同所在的行
}
--- 1,5 ----
#include<stdio.h>
main() # 不同所在函数名称
{
! printf ("hello welcome\n") # ! 表示不同所在的行
}
8.diff -r ##比较子目录中的文件
path 命令
patch常用来打补丁,修文件中的不同之处,使得两个文件一致。
diff -u top1 top2 > top.path
# 生成补丁,这是top1文件和top2 文件相比较产生的补丁,对top1文件生效。
patch top1 top.path
# 使用生成的补丁对top1 文件进行修补,这种方式为无备份的修改,修改完成后,原来top1 文件的内容消失。
[root@localhost mnt]# cat top1 # top1 文件原内容
Hello Welcome
[root@localhost mnt]# patch top1 top.path # 使用补丁对top1 文件进行修改
patching file top1
[root@localhost mnt]# cat top1 # 完成后 top1 文件改变 ,和top2 文件一致
hello welcome
patch -b top1 top.path
# 在使用补丁对top1 文件内容进行修改时,保留源内容到.orig文件中
[root@localhost mnt]# diff -u top1 top2 > top.path # 生成补丁
[root@localhost mnt]# cat top1 # top1 原内容
Hello Welcome
[root@localhost mnt]# cat top2 # top2 原内容
hello welcome
[root@localhost mnt]# patch -b top1 top.path # 使用补丁修改top1 内容并保留源文件内容
patching file top1
[root@localhost mnt]# ls
hello1.c hello.c top1 top1.orig top2 top.path toto1 toto2 # 生成top.orig文件 记录源内容
[root@localhost mnt]# cat top1
hello welcome
[root@localhost mnt]# cat top1.orig
Hello Welcome
cut 命令
cut 命令多用于字符的截取:
cut -d 指定分隔符
cut -f 指定列
cut -c 指定截取的字符
cut -d : -f 1 passwd # 以: 为分隔符,截取文件的第一列
cut -d : -f 1,6 passwd #以: 为分隔符,截取文件的第一列,和第六列
cut -d : -f 1-6 passwd #以: 为分隔符,截取文件的第一列到第六列
cut -c 1 passwd # 截取文件的第一个字符
cut -c 1,4 passwd # 截取文件的第一个和第四个字符
cut -c 1-4 passwd # 截取文件的第一个到第四个字符
测试:对本虚拟机的ip eth0进行截取仅截取ip
[root@localhost mnt]# ifconfig eth0|head -n 2 |tail -n 1|cut -d ' ' -f 10
172.25.47.104
[root@localhost mnt]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.47.104 netmask 255.255.255.0 broadcast 172.25.47.255
inet6 fe80::5054:ff:fe09:86e1 prefixlen 64 scopeid 0x20<link>
ether 52:54:00:09:86:e1 txqueuelen 1000 (Ethernet)
RX packets 8814 bytes 691942 (675.7 KiB)
RX errors 0 dropped 2890 overruns 0 frame 0
TX packets 2457 bytes 305924 (298.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
sort 命令
sort 多用于字符的排序
sort -n # 纯数字排序
sort -r # 倒序
sort -u # 去掉重复数字
sort -o # 输出到指定的文件中
sort -t # 指定分隔符
sort -k # 指定要排序的列
1 sort toto # 对文件toto内容进行排序
[root@localhost mnt]# sort toto
1
12
12
2
213
3
3
3
4
43
44
5
5
5
5
7
2 sort -n toto # 将文件内容按照纯数字进行排序
[root@localhost mnt]# sort -n toto
1
2
3
3
3
4
5
5
5
5
7
12
12
43
44
213
3 sort -rn toto # -r 倒序排列
[root@localhost mnt]# sort -rn toto
213
44
43
12
12
7
5
5
5
5
4
3
3
3
2
1
4 sort -rnu toto # -u 去掉重复数字
[root@localhost mnt]# sort -rnu toto
213
44
43
12
7
5
4
3
2
1
5 sort -rnu toto -o file # -o 将排序输出结果指定到某文件中去
[root@localhost mnt]# sort -rnu toto -o file
[root@localhost mnt]# cat file
213
44
43
12
7
5
4
3
2
1
6 sort -t ##指定分隔符;sort -k ##指定要排列的序
[root@localhost mnt]# sort -n -t : -k 1 toto
2:12
2:12
2:13
2:2
2:43
2:5
4:3
4:3
4:44
4:5
4:5
7:1
7:3
7:4
7:5
7:7
测试:对/mnt 中文件按照容量大小进行从达到小排序
[root@localhost mnt]# ls
file hello1.c hello.c passwd toto
[root@localhost mnt]# ll
total 20
-rw-r--r--. 1 root root 25 May 20 03:48 file
-rw-r--r--. 1 root root 56 May 20 00:31 hello1.c
-rw-r--r--. 1 root root 57 May 20 00:30 hello.c
-rw-r--r--. 1 root root 546 May 20 03:09 passwd
-rw-r--r--. 1 root root 69 May 20 03:51 toto
[root@localhost mnt]# ll|grep total -v
-rw-r--r--. 1 root root 25 May 20 03:48 file
-rw-r--r--. 1 root root 56 May 20 00:31 hello1.c
-rw-r--r--. 1 root root 57 May 20 00:30 hello.c
-rw-r--r--. 1 root root 546 May 20 03:09 passwd
-rw-r--r--. 1 root root 69 May 20 03:51 toto
[root@localhost mnt]# ll|grep total -v|sort -t ' ' -k 5 -rn
-rw-r--r--. 1 root root 546 May 20 03:09 passwd
-rw-r--r--. 1 root root 69 May 20 03:51 toto
-rw-r--r--. 1 root root 57 May 20 00:30 hello.c
-rw-r--r--. 1 root root 56 May 20 00:31 hello1.c
-rw-r--r--. 1 root root 25 May 20 03:48 file
[root@localhost mnt]#
uniq 命令
uniq 对重复字符坐相应的处理
uniq -u # 显示唯一的行
uniq -d # 显示重复的行
uniq -c # 每行显示一次,并统计重复次数
1 uniq -u ##显示唯一的行
[root@localhost mnt]# sort -n -t : -k 1 toto | uniq -u
2:13
2:2
2:43
2:5
4:44
7:1
7:3
7:4
7:5
7:7
2 uniq -d ##显示重复的行
[root@localhost mnt]# sort -n -t : -k 1 toto | uniq -d
2:12
4:3
4:5
3 uniq -c ##每行显示一次并统计重复次数
[root@localhost mnt]# sort -n -t : -k 1 toto | uniq -c
2 2:12
1 2:13
1 2:2
1 2:43
1 2:5
2 4:3
1 4:44
2 4:5
1 7:1
1 7:3
1 7:4
1 7:5
1 7:7
** 注意: uniq 一般配合sort 命令同时使用**
tr 命令
进行字符大小写的转换
tr ‘a-z’ ‘A-Z’ # 将小写转化成大写
tr ‘A-Z’ ‘a-z’ # 将大写转化成小写
[root@localhost mnt]# cat lala
hello welcome # 内容全部为小写字母
[root@localhost mnt]# cat lala|tr 'a-z' 'A-Z' # 经过转化之后输出大写字母
HELLO WELCOME
[root@localhost mnt]# vim lele
[root@localhost mnt]# cat lele
HAPPY TODAY # 内容全部为大写字母
[root@localhost mnt]# cat lele| tr 'A-Z' 'a-z' # 经过转化之后全部输出小写字母
happy today
测试:对于磁盘加密时输入yes不区分大小写进行加密