逐个比较文件



我需要比较文件夹中的文件,现在我手动浏览它们并运行:

diff -w file1 file2 > file_with_difference

我如何一次比较两个?让我的生活更轻松的是这样的东西(伪代码(:

for eachfile in folder:
diff -w filei filei+1 > file_with_differencei #the position of the file, because the name can vary randomly

i+=1                                          #so it goes to 3vs4 next time through the loop, 
#and not 2vs3

因此,它将第一名与第二名、第三名与第四名进行比较,依此类推。文件夹中总是有偶数个文件。

假设globing按您想要的顺序列出文件:

declare -a list=( folder/* )
for (( i = 0; i < ${#list[@]}; i += 2 )); do
if [[ -f "${list[i]}" ]] && [[ -f "${list[i + 1]}" ]]; then
diff "${list[i]}" "${list[i + 1]}" > "file_with_difference_$i"
fi
done

@Renaud有一个很好的答案。

假设您的文件名不包含空白,则另一种选择是:

printf '%s %sn' * |
while read -r f1 f2; do
diff "$f1" "$f2" > "diffs_$((++i))"
done

最新更新