---
title: "在Linux下比较两个目录需要忽略某些文件时怎么做？"
author: "codingstandards"
date: "2013-01-10T22:18:56Z"
tags: []
categories: ["操作系统"]
slug: "linux"
draft: false
lastmod: "2013-01-11T10:34:07Z"
id: 1767812
url: "https://codingstandards.iteye.com/blog/1767812"
---

在Linux下如果要比较两个目录，可以使用diff命令，并且需要加上选项-r（递归）。

       -r     When comparing directories, recursively compare any subdirectories found.

 

另外还有两个常用的选项，-b （忽略空白）和 -B （忽略空行）

       -b     Ignore changes in amount of white space.  
       -B     Ignore changes that just insert or delete blank lines.

如下例所示：

```
[root@s170 cjtj]# diff -r -b -B sch_admin 4sch_admin
              
Only in 4sch_admin: 1.txt
diff -r -b -B sch_admin/build/CVS/Root 4sch_admin/build/CVS/Root
1c1
< :pserver:coding@cvsbank:/CVSAPPS
---
> :pserver:zhy@218.23.142.44:12401/home/cvsroot
diff -r -b -B sch_admin/conf/CVS/Entries 4sch_admin/conf/CVS/Entries
1c1
< /struts-sch.xml/1.3/Fri May 18 07:31:05 2012//
---
> /struts-sch.xml/1.3/Fri May 18 14:39:35 2012//
。。。
。。。

```

因为我的目录中带有版本信息，CVS目录存在很大的区别，但是对实际的文件内容是没有意义的，因此我想在比较时忽略掉CVS目录。

       --exclude=pattern  
              When comparing directories, ignore files and subdirectories whose basenames match pattern.

```
下面是加了 --exclude选项时的比较结果，看上去很清爽。

[root@s170 cjtj]# diff -r -b -B --exclude=CVS sch_admin 4sch_admin

Only in 4sch_admin: 1.txt
Binary files sch_admin/dist/sch-admin.jar and 4sch_admin/dist/sch-admin.jar differ
Only in 4sch_admin/dist: .#sch-admin.jar.1.10
Only in 4sch_admin/dist: .#sch-admin.jar.1.11
Only in 4sch_admin/dist: .#sch-admin.jar.1.12
Only in 4sch_admin/extra/zhaosheng: baoming.html.1
[root@s170 cjtj]# 


```

 

欲访问本文最新版，请移步：[http://www.vktone.com/articles/diff-dirs-ignore-some-files-in-linux.html](http://www.vktone.com/articles/diff-dirs-ignore-some-files-in-linux.html)

 

更多关于Linux使用的文章请访问：[http://www.vktone.com/linux.html](http://www.vktone.com/linux.html)