使用shell脚本检查两个目录下的所有文件是否有相同的,输出相同文件以及所有各自有的的文件

#!/bin/bash

read -p "请输入第一个文件夹的路径:" DIR1
read -p "请输入第二个文件夹的路径:" DIR2
#echo $DIR1
#echo $DIR2

#用于读取 DIR1 中文件的数目
if [ ! -d $DIR1 ];then # 判断用户输入的路径是否正确,错误则直接退出程序
    echo "没有 $DIR1 这个目录,程序退出"
    exit
fi
count_1=`find $DIR1 |wc -l`
let count_1=${count_1}-1 #find命令第一行实际是查找的目录,减去第一行才是是实际的文件数
#echo $count_1
#echo $DIR1

#用于读取 DIR1 中文件的数目
if [ ! -d $DIR2 ];then # 判断用户输入的路径是否正确,错误则直接退出程序
    echo "没有 $DIR2 这个目录,程序退出"
    exit
fi
count_2=`find $DIR2 |wc -l`
let count_2=${count_2}-1 #find命令第一行实际是查找的目录,减去第一行才是是实际的文件数
#echo $count_2
#echo $DIR2

#将DIR1中的文件以“文件名 md5值”形式放入temp_1
time=0
temp=2
while [ $time -lt $count_1 ];do #利用DIR1中的文件数目控制循环
    file_locate_1=`find $DIR1 | sed -n "${temp}p"` #取出文件名
    #echo $temp
    #echo $file_locate_1
    file_first=`md5sum $file_locate_1 | awk -F[' ']+ '{print $1}'` #取出文件对应的md5值
    #echo $file_first
    echo "$file_locate_1 $file_first" >> /root/temp_1 #格式化输入文件
    let time=${time}+1
    let temp=${temp}+1
done

#将DIR2中的文件以“文件名 md5值”形式放入temp_2
time=0
temp=2
while [ $time -lt $count_2 ];do #利用DIR2中的文件数目控制循环
    file_locate_2=`find $DIR2 | sed -n "${temp}p"`  #取出文件名
    #echo $temp
    #echo $file_locate_2
    file_second=`md5sum $file_locate_2 | awk -F[' ']+ '{print $1}'`  #取出文件对应的md5值
    #echo $file_second
    echo "$file_locate_2 $file_second" >> /root/temp_2 #格式化输入文件
    let time=${time}+1
    let temp=${temp}+1
done

out=0
in=0
hang=1
hang_2=1
while [ $out -lt $count_1 ];do #外层循环由DIR1中的文件数目控制
    file_name=`cat /root/temp_1 | sed -n "${hang}p" | awk -F" " '{print $1}'`
    file_md5=`cat /root/temp_1 | sed -n "${hang}p" | awk -F" " '{print $2}'`
    while [ $in -lt $count_2 ];do #内层循环由DIR2中的文件数目控制
        file_name_2=`cat /root/temp_2 | sed -n "${hang_2}p" | awk -F" " '{print $1}'`
        file_md5_2=`cat /root/temp_2 | sed -n "${hang_2}p" | awk -F" " '{print $2}'`
        if [ "${file_md5}" = "${file_md5_2}" ];then #将文件的md5值进行对比
            echo "$DIR1 中的 $file_name 与 $DIR2 中的 $file_name_2 为相同文件" #相同的则输出
            flag=1
            echo "$file_name 1" >> /root/temp_3 #第一个文件夹的放入temp_3
            echo "$file_name_2 1" >> /root/temp_4 #第二个文件夹的放入temp_4
        else #[ $flag -eq 0 ];then #该组对比表示没有对比上
            echo "$file_name 0" >> /root/temp_3 #第一个文件夹的放入temp_3
            echo "$file_name_2 0" >> /root/temp_4 #第二个文件夹的放入temp_4
        fi
        let in=${in}+1
        let hang_2=${hang_2}+1
        flag=0
    done
    hang_2=1
    in=0
    #echo $out
    let out=${out}+1
    let hang=${hang}+1
    # echo $file_name
    #echo $out
    #echo $count_1
done

if [ -f /root/temp_3 ];then
    echo "这是 $DIR1 中特有的文件"
    sort -n /root/temp_3 | uniq | awk -F" " '{print $1}' | uniq -u #去除重复后取第一个元素后再次去掉重复行
    # echo "这是 $DIR2 中特有的文件"
    # sort -n /root/temp_4 | uniq
else
    echo "$DIR1 中的每个文件都与 $DIR2 中相同"
fi
if [ -f /root/temp_4 ];then
    echo "这是 $DIR2 中特有的文件"
    sort -n /root/temp_4 | uniq | awk -F" " '{print $1}' | uniq -u  #去除重复后取第一个元素后再次去掉重复行
else
    echo "$DIR2 中的每个文件都与 $DIR1 中相同"
fi
rm -f /root/temp_{1..4}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值