【发布时间】:2015-06-03 04:42:48
【问题描述】:
我想知道有没有办法或Unix命令可以知道两个文件的内容是否相同而不考虑顺序。
也就是说以下两个文件内容必须被认为是相同的:
AAAA
BBBB
BBBB
AAAA
提前致谢!
备注:我知道我可以使用 diff 或 md5sum 但据我了解,他们不考虑我感兴趣的情况。
编辑:因为我需要它来查找在数千个文件中是否至少有两个文件具有相同的内容,所以我发布了我使用@anishsane 给出的答案编写的 bash 脚本:
#!/bin/bash
for entry in file-*.smt2
do
for entry1 in file-*.smt2
do
if [ -f "$entry" ] && [ -f "$entry1" ] && [ "$entry" != "$entry1" ]; then
file1=`sort $entry | md5sum`
file2=`sort $entry1 | md5sum`
if [ "$file1" == "$file2" ]
then
echo "Files have the same content"
echo "$entry $entry1"
echo "$file1"
echo "$file2"
exit -2
else
echo "Files $entry and $entry1 have NOT the same content"
fi
fi
done
done
【问题讨论】:
-
diff <(sort file1) <(sort file2) -
@anishsane - 我认为这应该是答案。
-
我太懒了..会做.. :P
-
当3行
AAAA和2行BBBB的文件必须与示例相同时使用sort -n。 -
在您的脚本中,每个 sort/md5sum 都会执行数千次。考虑先制作 tmp 文件
${entry}.md5。
标签: bash unix awk command diff