---
title: "llnux递归比较两个目录并把差异文件放到一个目录"
author: "starlight_520"
date: "2016-11-22T15:52:03Z"
tags: ["CentOS"]
categories: ["操作系统"]
slug: "llnux"
draft: false
lastmod: "2016-11-22T15:52:03Z"
id: 2339802
url: "https://starlight-520.iteye.com/blog/2339802"
---

shell脚本cdir.sh如下：

```java
#!/bin/bash     
# $1 第1个参数：原目录  $2 第2个参数：新目录 $3 第3个参数：差异文件目录  
if [[ ! -d $3 ]]; then
  mkdir -p $3
fi
outdir=$(cd $3;pwd)
#A是原目录 B是新目录  out差异输出目录
rsync --dry-run -rcnC --out-format="%n" $2/ $1/ |grep -v "/$"|xargs -I{} rsync -R $2/./{} $outdir/
```