详解 Linux 中的 diff 命令及示例
先决条件
运行 Linux 的服务器 在您的服务器上配置的 root 密码
创建云服务器
基本语法
diff [OPTION] FILES
-s 两个文件相同时报告 -c 在上下文模式下显示输出 -q 仅在文件不同时报告 -y 在两列中显示差异输出 -r 递归比较任何子目录 -i 忽略文件内容的大小写差异 -w 忽略所有空格 –ignore-file-name-case 比较文件名时忽略大小写 --no-ignore-file-name-case 比较文件名时考虑一种情况
在 Linux 中创建文件
11 12 13 14 15 16 17 18 19
eveven twelve thirteen fourteen fifteen sixteen seventeen eighteen ninteen
In this file, we added some numbers in text and numeric form
www.LinuxMI.com
10 12 3 14 8 16 7 18 19
five twelve eight fourteen fifteen nine seventeen one ninteen
In this file, we added some numbers in text and numeric form
www.LinuxMI.com
11 12 13 14 15 16 17 18 19
eveven twelve thirteen fourteen fifteen sixteen seventeen eighteen ninteen
In this file, we added some numbers in text and numeric form
www.LinuxMI.com
打印两个文件之间的差异
linuxmi@linuxmi /home/linuxmi/www.linuxmi.com
⚡ diff file1.txt file2.txt
1,2c1,2
< 11 12 13 14 15 16 17 18 19
< eveven twelve thirteen fourteen fifteen sixteen seventeen eighteen ninteen
---
10 12 3 14 8 16 7 18 19
five twelve eight fourteen fifteen nine seventeen one ninteen
linuxmi@linuxmi /home/linuxmi/www.linuxmi.com
⚡ diff -c file1.txt file2.txt
*** file1.txt 2022-05-07 22:37:23.371594485 +0800
--- file2.txt 2022-05-07 22:37:15.699594796 +0800
***************
! 11 12 13 14 15 16 17 18 19
! eveven twelve thirteen fourteen fifteen sixteen seventeen eighteen ninteen
In this file, we added some numbers in text and numeric form
www.LinuxMI.com
! 10 12 3 14 8 16 7 18 19
! five twelve eight fourteen fifteen nine seventeen one ninteen
In this file, we added some numbers in text and numeric form
www.LinuxMI.com
使用 diff 命令查找相似文件
linuxmi@linuxmi /home/linuxmi/www.linuxmi.com
⚡ diff -s file1.txt file3.txt
文件 file1.txt 和 file3.txt 相同
使用 Diff 命令查找不同的文件
linuxmi@linuxmi /home/linuxmi/www.linuxmi.com
⚡ diff -q file1.txt file2.txt
文件 file1.txt 和 file2.txt 不同
在并排视图中显示输出
linuxmi@linuxmi /home/linuxmi/www.linuxmi.com
⚡ diff -y file1.txt file2.txt
linuxmi@linuxmi /home/linuxmi/www.linuxmi.com
⚡ diff -y --suppress-common-lines file1.txt file2.txt
结论
来自:Linux迷
相关文章