您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

有一个文件夹包含相同名称但文件不同的文件

有一个文件夹包含相同名称但文件不同的文件

if [ "$file_hash" == "$a" ];将哈希与文件名进行比较。你需要类似的东西

if [ "$file_hash" == $(md5sum "$a" | cut -d ' ' -f 1) ];

计算目标文件夹中每个文件的哈希值。

此外,在当前版本中,您的for循环仅运行一次;你需要像

for a in $destination_folder/*

获取文件夹中的所有文件,而不仅仅是文件名称

根据您的修改解决方案看起来像

#!/bin/bash -xv

file=$1
destination_folder=$2
file_hash=`md5sum "$file" | cut -d ' ' -f 1`

# test that the filename exists in the destination dir
if [[ -f $destination_folder/$file ]] ; then
    dest_hash=$(md5sum "$destination_folder/$file" | cut -d ' ' -f 1)
    # test that the hash is the same
    if [[ "$file_hash" == $curr_hash ]] ; then
        cp "$file.JPG" "$destination_folder/$file.JPG"
    else 
        # do nothing
    fi
else
    # destination does not exit, copy file
    cp "$file.JPG" "$destination_folder/$file"
fi

这不能确保没有重复项。它只是确保具有相同名称的不同文件不会相互覆盖。

#!/bin/bash -xv

file=$1
destination_folder=$2
file_hash=`md5sum "$file" | cut -d ' ' -f 1`

# test each file in destination
for a in $destination_folder/*
do
   curr_hash=$(md5sum "$a" | cut -d ' ' -f 1)
   if [ "$file_hash" == $curr_hash ]; 
   then
       # an identical file exists. (maybe under another name)
       # do nothing
       exists=1
       break
   fi
done

if [[ $exists != 1 ]] ; then
   if [[ -f $destination_folder/$file ]] ; then
       cp "$file.JPG" "$destination_folder/$file.JPG"
   else 
       cp "$file.JPG" "$destination_folder"
   fi
fi

未经测试。

其他 2022/1/1 18:18:39 有538人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶