Linux 有一个文件夹,其中包含同名但不同文件的文件

Linux 有一个文件夹,其中包含同名但不同文件的文件,linux,bash,Linux,Bash,我试图只将原始文件从一个目录复制到另一个目录,但有些文件具有相同的名称。。。我正在尝试使用散列来比较文件,如果它不在目录中,将其发送到那里,如果名称相同,则将其更改为file_name.something。在这个时候,我得到一些文件和文件有相同的名称正在被覆盖。。。有人能提出一些建议吗 #!/bin/bash -xv source_folder=$1 destination_folder=$2 if [ $# -eq 0 ] then echo "usage:$0 directo

我试图只将原始文件从一个目录复制到另一个目录,但有些文件具有相同的名称。。。我正在尝试使用散列来比较文件,如果它不在目录中,将其发送到那里,如果名称相同,则将其更改为file_name.something。在这个时候,我得到一些文件和文件有相同的名称正在被覆盖。。。有人能提出一些建议吗

#!/bin/bash -xv

source_folder=$1
destination_folder=$2

if [ $# -eq 0 ] 
then 
    echo "usage:$0 directory_name";exit 999; 
fi

if [ -d $source_folder ]
then
     echo "source source_folder exists."
else
     echo "Source folder doesn't exist"
     exit 1;
fi

if [ -d $destination_folder ]
then
    echo "Destination folder exists"
else
    mkdir $destination_folder
fi



find "$source_folder" -name "IMG_[0-9][0-9][0-9][0-9].JPG" -exec ./check {} $destination_folder/ \;





 #!/bin/bash -xv

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

for a in $destination_folder/*
do
   curr_hash=$(md5sum "$a" | cut -d ' ' -f 1)
   curr_file=$a

   if [ ! "$file_hash" == "$curr_hash" ]; 
   then
   if [[ -f $destination_folder/$file ]] ;
   then # CAN ANYBODY TELL ME WHY IT IGNORES THIS LINE
      cp "$file" "$file.JPG"
      mv "$file.JPG" "$destintion_folder" 
     else # IT GOES STRAIGHT FOR THIS ONE
       cp "$file" "$destination_folder"
   fi
   fi

done
你的
如果[“$file\u 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

未测试。

感谢您的快速回复,现在它只复制了所有文件两次,副本有额外的。JPG:s顺便说一句,我不知道这很重要,但目标文件夹是空的+1到damienfrancois@user2988983:您可能还需要分几个步骤来完成:1)将当前的$file及其$file_散列与目录中的每个文件进行比较[甚至只是复制一个文件?或者不复制,这取决于您需要什么],并查看它是否“已经存在”。如果它还不存在:2)复制它(如果碰巧有一个文件已经存在(可能使用不同的散列!)并且具有相同的名称,则更改其名称)。这样,将只复制带有“新哈希”的$file。我不确定你是什么意思,但这是你想要的^^@OlivierDulac谢谢你的
+1
,但我宁愿你点击我答案旁边的向上箭头:)@damienfrancois:的确,我忘了^^^对不起。。。我今天很累:p@damienfrancois:根据他的评论和需要(将文件从一个目录复制到另一个目录,而“另一个目录”开始为空),我猜它需要改进。。。类似于
move.sh original_dir destination_dir
script witch对$中的原始文件执行类似于
的操作(查找“$1”-类型f);如果需要“$origfile”$2/”和&mv\u,则不要将“$origfile”$2/”和“$origfile”$2/”中的“尚未”散列出来;完成
注意返回码是字节,因此999超出范围。它将作为231返回。您在第二个脚本中的
if
语句中缺少一个结束
]
为什么要重新发明轮子?您尝试过Rsync吗?没有,它更容易实现吗?