如何将目录中所有文件的内容与另一个目录进行比较?

时间:2015-02-17 06:16:17

标签: bash command-line terminal compare diff

我有一个包含大量C文件的目录,.c.h,以及另一个非常相似的文件目录,由于更新,同事发送的文件存在很小的差异,我想看看这些差异是什么。

我可以手动在每个文件上运行差异,但是因为它们的命名是相同的,无论如何我可以更快地轻松查看哪些文件不同以及它们的不同之处?

2 个答案:

答案 0 :(得分:2)

diff命令能够做到。试试吧:

diff directory1 directory2

答案 1 :(得分:0)

您可能会发现计算机上有一个命令dircmp,它将执行比较两个目录的工作。它标识仅在第一个目录中找到的文件和仅在第二个目录中找到的文件。对于在两者中找到的文件,它会告诉您它们是相同还是不同。

如果您在机器上没有标准配置,则可以使用此配置:

#!/bin/sh
#
#   @(#)$Id: dircmp.sh,v 1.6 2003/03/12 08:29:13 jleffler Exp jleffler $
#
#   Simulation of the much-loved dircmp(1) script, with extensions.

arg0=$(basename $0 .sh)

error(){
    echo "$arg0: $*" 1>&2
    exit 1
}

dflag=0 # Files that are different
mflag=0 # Files that are missing in one or the other
sflag=0 # Files that are the same in both (or directories, or otherwise special)
while getopts dms flag
do
    case "$flag" in
    (d) dflag=1;;
    (m) mflag=1;;
    (s) sflag=1;;
    (*) echo "Usage: $arg0 [-dms] dir1 dir2" 1>&2; exit 1;;
    esac
done
shift $(expr $OPTIND - 1)

# If user set no flags, set them all (traditional behaviour of dircmp).
if [ $sflag = 0 ] && [ $dflag = 0 ] && [ $mflag = 0 ]
then dflag=1; mflag=1; sflag=1
fi

if [ $# != 2 ]
then echo "Usage: $arg0 [-dms] dir1 dir2" 1>&2; exit 1
elif [ ! -d "$1" ]
then error "$1 is not a directory"
elif [ ! -d "$2" ]
then error "$2 is not a directory"
fi

tmp="${TMPDIR:-/tmp}/dc.$$"
trap "rm -f \"$tmp\".?; exit 1" 0 1 2 3 13 15

(cd "$1" 1>&2 && find . -print | sort) > "$tmp".1
(cd "$2" 1>&2 && find . -print | sort) > "$tmp".2

{
if [ $mflag = 1 ]
then
    comm -23 "$tmp".1 "$tmp".2 > "$tmp".3
    comm -13 "$tmp".1 "$tmp".2 > "$tmp".4
    if [ -s "$tmp".3 ] || [ -s "$tmp".4 ]
    then
        long=$(awk '{if(length($0) > len) { len = length($0); }}
                END { print 2 * len + 6; }' "$tmp".3 "$tmp".4)
        echo "Files in $1 only and in $2 only"
        echo
        pr -w$long -l1 -t -m "$tmp".3 "$tmp".4
        echo
    fi
    rm -f "$tmp".3 "$tmp".4
fi

if [ $sflag = 1 ] || [ $dflag = 1 ]
then
    comm -12 "$tmp".1 "$tmp".2 > "$tmp".5
    if [ -s "$tmp".5 ]
    then
        case $sflag$dflag in
        (11) echo "Comparison of files in $1 and $2";;
        (01) echo "Files which differ in $1 and $2";;
        (10) echo "Files which are the same in $1 and $2";;
        esac
        echo
        cat "$tmp".5 |
        while read file
        do
            if [ -f "$1/$file" ] && [ -f "$2/$file" ]
            then
                if cmp -s "$1/$file" "$2/$file"
                then [ $sflag = 1 ] && echo "same                $file"
                else [ $dflag = 1 ] && echo "different           $file"
                fi
            elif [ $sflag = 0 ]
            then continue
            elif [ -d "$1/$file" ] && [ -d "$2/$file" ]
            then echo "directory           $file"
            elif [ -b "$1/$file" ] && [ -b "$2/$file" ]
            then echo "block special       $file"
            elif [ -c "$1/$file" ] && [ -c "$2/$file" ]
            then echo "character special   $file"
            elif [ -p "$1/$file" ] && [ -p "$2/$file" ]
            then echo "named pipe          $file"
            else echo "***dubious***       $file"
            fi
        done
        echo
    fi
fi
} |
uniq

rm -f $tmp.?
trap 0