2012-02-27 60 views
0

我试图得到差异输出只有修改后的文件的名称。 我试图使用-q选项,但它仍然给了我太多的输出:Linux的差异只输出文件名和其他什么

我现在得到的是这样的:

files path/to/file1/file1 and path/to/file2/file2 are different 

,我想这一点:

path/to/file1/file1 

任何如何做到这一点的想法?

回答

3

使用diff的退出状态:

if ! diff -q file1 file2 >/dev/null; then echo file1; fi 
+2

+1,虽然不以'差异-r'工作;你必须将它与'find'结合来检查一个巨大列表中的每个文件。 – 2012-02-27 23:41:02

+0

thx我没有注意到复数(文件) – 2012-02-28 09:11:01

1

你可以随时就管别的事情,比如:

# diff ... | sed 's/^files //;s/ and .*//;' 

但请注意,如果你有文字“和”文件,然后上面会引起问题。通常我鼓励人们不要在文件名中使用空格。是的,你可以做到,但是,它仍然会导致问题。

1

于韦斯的答案类似:

echo "files path/to/file1/file1 and path/to/file2/file2 are different" | sed -e "s/^files //" -e "s/ and .*$//" 

//output 
path/to/file1/file1 
1

另一种选择是使用cmp --silent而不是diff -q。这不会向stdout发送任何消息,因此您只需处理退出状态。

例如:

file1=path/to/file1/file1; cmp --silent $file1 path/to/file2/file2 || echo $file1 

CMP的退出状态是一样的diff(0,如果输入是相同的,并且如果1不同)。

3

你可以这样做: diff -q file1 file2 | cut -f2 -d' '