用于比较两个文件夹的Shell脚本

时间:2014-02-17 15:20:49

标签: shell

我想编写一个shell脚本来比较两个文件夹,并将差异保存在另一个文件夹中。

假设FolderA有五个子文件夹,如A1,A2,A3,A4,A5,FolderB有五个子文件夹,如A1,A2,A3,A4,A5。文件夹A& B在结构上是相同的,但文件夹A1,A2等文件中的文件有一些差异。

我想比较FolderA->中文件的差异。 A1与FolderB->; A1并将结果保存在差值/ A1文件夹中。我试着写剧本但是没有用 - 请指导我?

    #!/bin/bash
    # setup folders for our different stages
    DIST=/app/webmcore1/Demo/FolderA
    DIST_OLD=/app/webmcore1/Demo/FolderB
    mkdir -p Difference/newAddition
    mkdir -p Difference/DifferenceIs
    DIST_UPGRADE=/app/webmcore1/Demo/Difference
    cd $DIST
    find . -type f | while read filename
    do
        if [ ! -f "$DIST_OLD$filename" ]; then
        cp --parents "$filename" $DIST_UPGRADE/newAddition
        continue
        fi
        diff "$filename" "$DIST_OLD$filename" > /app/webmcore1/Demo/DIST_UPGRADE/DifferenceIs
    done

1 个答案:

答案 0 :(得分:0)

尝试使用ls命令,然后获得差异。

cd FolderA
ls -R >/tmp/list1
cd FolderB
ls -R >/tmp/list2
diff /tmp/list1 /tmp/list2 >/tmp/difference.list

您可以根据自己的要求自定义此逻辑。