将给定名称的所有文件与另一个文件进行比较

时间:2013-06-22 22:21:52

标签: linux

我想找到给定名称# find / -name myfile的所有文件,将它们与另一个给定文件/home/me/myCompareFile进行比较,并输出文件是否相同。很高兴忽略空白,但没有必要。这可能是从shell做的吗?感谢

2 个答案:

答案 0 :(得分:2)

您可以执行以下操作:

find . -name "t.c" | xargs -I % diff -q ../t.h % && echo "matches"

将查找myFile,并为每个结果调用diffmyCompareFile进行比较,并告诉您它是否有所不同(感谢-q

例如,我在当前目录中以t.ct.h为例:

% find . -name "t.c" | xargs -I % diff -q ./t.h % && echo "matches"
Files ./t.h and ./foo/t.c differ
Files ./t.h and ./t.c differ
% find . -name "t.c" | xargs -I % diff -q ./t.c % && echo "matches"
matches

甚至比&& echo "%matches"更好:

% find . -name "t.c" | xargs -I % diff -qs ./t.c %
Files ./t.c and ./foo/t.c differ
Files ./t.c and ./t.c are identical

-s的{​​{1}}参数是:

diff

-s --report-identical-files Report when two files are the same.

-q

cf -q --brief Output only whether files differ.

答案 1 :(得分:1)

for file in $(find / -name myfile)
do
   diff $file /home/me/myCompareFile
done