如何将目录中所有文件的内容与另一个目录中的内容进行比较?
How do I compare the contents of all the files in a directory against another directory?
我有一个包含一堆 C 文件的目录,.c
和 .h
,以及另一个非常相似的目录,其中包含同事发送的文件,由于更新,它们之间的差异非常小,我想看看这些差异是什么。
我可以 运行 手动区分每个文件,但由于它们的名称相同,所以我可以更快、更轻松地查看哪些文件不同以及它们的不同之处?
diff
命令可以做到。试试看:
diff directory1 directory2
您可能会发现计算机上有一个命令 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 [=10=] .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 "" ]
then error " is not a directory"
elif [ ! -d "" ]
then error " is not a directory"
fi
tmp="${TMPDIR:-/tmp}/dc.$$"
trap "rm -f \"$tmp\".?; exit 1" 0 1 2 3 13 15
(cd "" 1>&2 && find . -print | sort) > "$tmp".1
(cd "" 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([=10=]) > len) { len = length([=10=]); }}
END { print 2 * len + 6; }' "$tmp".3 "$tmp".4)
echo "Files in only and in 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 and ";;
(01) echo "Files which differ in and ";;
(10) echo "Files which are the same in and ";;
esac
echo
cat "$tmp".5 |
while read file
do
if [ -f "/$file" ] && [ -f "/$file" ]
then
if cmp -s "/$file" "/$file"
then [ $sflag = 1 ] && echo "same $file"
else [ $dflag = 1 ] && echo "different $file"
fi
elif [ $sflag = 0 ]
then continue
elif [ -d "/$file" ] && [ -d "/$file" ]
then echo "directory $file"
elif [ -b "/$file" ] && [ -b "/$file" ]
then echo "block special $file"
elif [ -c "/$file" ] && [ -c "/$file" ]
then echo "character special $file"
elif [ -p "/$file" ] && [ -p "/$file" ]
then echo "named pipe $file"
else echo "***dubious*** $file"
fi
done
echo
fi
fi
} |
uniq
rm -f $tmp.?
trap 0
我有一个包含一堆 C 文件的目录,.c
和 .h
,以及另一个非常相似的目录,其中包含同事发送的文件,由于更新,它们之间的差异非常小,我想看看这些差异是什么。
我可以 运行 手动区分每个文件,但由于它们的名称相同,所以我可以更快、更轻松地查看哪些文件不同以及它们的不同之处?
diff
命令可以做到。试试看:
diff directory1 directory2
您可能会发现计算机上有一个命令 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 [=10=] .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 "" ]
then error " is not a directory"
elif [ ! -d "" ]
then error " is not a directory"
fi
tmp="${TMPDIR:-/tmp}/dc.$$"
trap "rm -f \"$tmp\".?; exit 1" 0 1 2 3 13 15
(cd "" 1>&2 && find . -print | sort) > "$tmp".1
(cd "" 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([=10=]) > len) { len = length([=10=]); }}
END { print 2 * len + 6; }' "$tmp".3 "$tmp".4)
echo "Files in only and in 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 and ";;
(01) echo "Files which differ in and ";;
(10) echo "Files which are the same in and ";;
esac
echo
cat "$tmp".5 |
while read file
do
if [ -f "/$file" ] && [ -f "/$file" ]
then
if cmp -s "/$file" "/$file"
then [ $sflag = 1 ] && echo "same $file"
else [ $dflag = 1 ] && echo "different $file"
fi
elif [ $sflag = 0 ]
then continue
elif [ -d "/$file" ] && [ -d "/$file" ]
then echo "directory $file"
elif [ -b "/$file" ] && [ -b "/$file" ]
then echo "block special $file"
elif [ -c "/$file" ] && [ -c "/$file" ]
then echo "character special $file"
elif [ -p "/$file" ] && [ -p "/$file" ]
then echo "named pipe $file"
else echo "***dubious*** $file"
fi
done
echo
fi
fi
} |
uniq
rm -f $tmp.?
trap 0