shell 中如何用for语句同时搜索两个文件夹内的所有文件,并进行文件比较,急用,希望高手解答,谢谢

如题所述

不考虑子文件夹。

先用ls命令枚举其中一个文件夹内的所有文件(用数组记录文件名)。
cd 目录1
declare -a array=(`ls`)

然后在枚举另一个文件夹时用for循环与数组元素逐个比较。
cd 目录2
for file2 in *
do
isFound=0
for file1 in ${array[*]}
do
if [ "$file2" = "$file1" ]; then
diff -y --suppress-common-lines 目录1/$file1 目录2/$file2
isFound=1
fi
done
[ $isFound -eq 0 ] && echo "目录2下的$file2文件在目录1下不存在!"
done

当然,还有一种情况我没有处理,即目录1下的内容在目录2中不存在。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-12-20
for /f %%i (文件夹1) do (
for /f %%j (文件夹2) do (
findstr /ivg:%%i %%j >output.txt
)
)

不好意思上面是bat的。。
dir1=`ls -l 文件夹1|grep "^-"|awk '{print $1}'`
for file1 in $dir1
do
dir2=`ls -l 文件夹2|grep "^-"|awk '{print $1}'`
for file2 in dir2
do
diff $file1 $file2 >output.log
done
done
第2个回答  2012-12-20
代码中采用两个for循环就可以了。
相似回答
大家正在搜