Linux命令(144)之diff
1.diff命令介绍
用于比较文件或目录的内容,特别是比较两个版本不同的文件以找到改动的地方。常用于文件比较和差异分析。
2.diff用法
diff [参数] file1 file2
diff参数说明
参数 | 说明 |
-u | 统一格式(Unified Format),输出更加简洁易懂 |
-c | 上下文格式(Context Format),输出更多的上下文行 |
-i | 忽略大小写差异 |
-w | 忽略所有空格差异,包括空格、制表符等 |
-b | 忽略空格的变化,但不会忽略不同数量的空格 |
-q | 只显示文件是否不同,而不显示具体的差异 |
-r | 递归比较目录中的文件 |
-N | 如果文件不存在,则假设它是空文件 |
输出格式:
diff的输出通常是通过以下几种方式来表示差异:
<表示file1中有的内容,file2中没有的。
>表示file2中有的内容,file1中没有的。
3.实例
1.目录递归比较
命令:
diff -r /root/ /root/cron/
[root@patrolagent ~]# diff -r /root/ /root/cron/
Only in /root/: 1.txt
Only in /root/: 2.txt
Only in /root/: anaconda-ks.cfg
Only in /root/: .bash_history
Only in /root/: .bash_logout
Only in /root/: .bash_profile
Only in /root/: .bashrc
Only in /root/: bc_here_document.sh
Only in /root/: copydir.sh
Only in /root/: .cpan
Only in /root/: cron
Only in /root/: .cshrc
Only in /root/: dda_transfer.sh
Only in /root/: function.sh
Only in