简体   繁体   English

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

[英]Have one folder with files that have the same name but different file

i am trying to copy only original files from one directory to another, but some files have the same name... i am trying to use hash to compare files, if its not in directory send it there and if the name is the same change it to file_name.something. 我试图只将原始文件从一个目录复制到另一个目录,但有些文件具有相同的名称...我正在尝试使用哈希来比较文件,如果它不在目录中发送它并且如果名称是相同的更改它到file_name.something。 at this time im getting some files and the files that have the same name are being overwritten... can anyone suggest 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

Your if [ "$file_hash" == "$a" ]; 你的if [ "$file_hash" == "$a" ]; compares a hash with a filename. 将哈希与文件名进行比较。 You need something like 你需要类似的东西

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

to compute the hash for each of the file in destination folder. 计算目标文件夹中每个文件的哈希值。

Furthermore, your for loop, in its current version, runs only once ; 此外,您的for循环在其当前版本中仅运行一次; you need something like 你需要类似的东西

for a in $destination_folder/*

to get all files in that folder rather than just the folder name. 获取该文件夹中的所有文件而不仅仅是文件夹名称。

Based on your edits, a solution would look like 根据您的编辑,解决方案看起来像

#!/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

This does not ensure that there are no duplicates. 这并不能确保没有重复。 It simply ensures that distinct files with identical names do not overwrite each other. 它只是确保具有相同名称的不同文件不会相互覆盖。

#!/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

Not tested. 没有测试过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 移动与其包含的文件名称相同的文件 - move files that have the same name of the file they contain 如何查找所有不具有名称相同但扩展名不同的匹配文件的文件 - how to find all files that dont have a matching file with the same name but different extension 如何从多个目录中提取多个文件,而不同目录中的不同文件可能具有相同的名称 - how to scp multiple files from multiple directories, while different files in different directories may have the same name 用与文件相同的名称但在不同的文件夹中创建tar - Create tar with same name as file but in different folder 使用bash以递归方式查找具有相同名称但实际上不同的文件的最佳方法? - best way to find files recursively that have the same name but are actually different using bash? Bash命令查找名称相同但扩展名不同的文件 - Bash command to find files who have the same name but not the same extension 复制/移动不同文件夹中同名的多个文件 - copy/move same name multiple files in different folder 合并两个文件夹并保持文件具有相同的名称 - merge two folders and keeping the files have same name 查找文件名大于的文件 - find files that have number in file name greater than 我对文件夹名称中的空格有疑问 - I have a problem with spaces in folder name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM