有一个文件夹与具有相同名称但不同的文件的文件 [英] Have one folder with files that have the same name but different file

查看:200
本文介绍了有一个文件夹与具有相同名称但不同的文件的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图只有原始文件从一个目录复制到另一个,但有些文件有相同的名字......我想使用哈希比较文件,如果它不是在目录中有发送,如果名称是同样的变化它file_name.something。在这个时候即时得到一些文件,并且具有相同的名称被覆盖的文件...任何人都可以提出好的建议?

 #!/斌/ bash的-xvSOURCE_FOLDER = $ 1
destination_folder = $ 16如果[$#-eq 0]
然后
    回声用法:$ 0目录名退出999;
科幻如果[-d $ SOURCE_FOLDER]
然后
     回声源SOURCE_FOLDER存在。
其他
     回声源文件夹不存在
     1号出口;
科幻如果[-d $ destination_folder]
然后
    回声目标文件夹是否存在
其他
    MKDIR $ destination_folder
科幻找到$ SOURCE_FOLDER-nameIMG_ [0-9] [0-9] [0-9] [0-9] .JPG-exec ./check {} $ destination_folder / \\; #!/斌/ bash的-xv文件= $ 1
destination_folder = $ 16
file_hash =`的md5sum$文件|切-d''-f 1`对于在$ destination_folder / *

   curr_hash = $(的md5sum$ A|切-d''-f 1)
   curr_file = $一个   如果[! $ file_hash==$ curr_hash];
   然后
   如果[[-f $ destination_folder / $文件]];
   然后#有谁能够告诉我为什么会忽略这个LINE
      CP$文件,$ file.JPG
      MV$ file.JPG$ destintion_folder
     否则#它直接GOES这一个
       CP$文件,$ destination_folder
   科幻
   科幻DONE


解决方案

如果[$ file_hash==$ A]; 的哈希与比较文件名。你需要像

 如果[$ file_hash== $(的md5sum$ A|切-d''-f 1)];

来计算哈希为每一个目标文件夹中的文件。

此外,你的循环,在目前的版本中,只运行一次;你需要像

 为在$ destination_folder / *

让所有的文件夹中,而不仅仅是文件夹名称。

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

 #!/斌/ bash的-xv文件= $ 1
destination_folder = $ 16
file_hash =`的md5sum$文件|切-d''-f 1`该文件名在目标目录存在#测试
如果[[-f $ destination_folder / $文件]];然后
    dest_hash = $(的md5sum$ destination_folder / $文件|切-d''-f 1)
    #测试哈希是相同的
    如果[$ file_hash== $ curr_hash]];然后
        CP$ file.JPG$ destination_folder / $ file.JPG
    其他
        # 没做什么
    科幻
其他
    #目的地不退出,复制文件
    CP$ file.JPG$ destination_folder / $文件
科幻

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

 #!/斌/ bash的-xv文件= $ 1
destination_folder = $ 16
file_hash =`的md5sum$文件|切-d''-f 1`#测试目的地每个文件
对于在$ destination_folder / *

   curr_hash = $(的md5sum$ A|切-d''-f 1)
   如果[$ file_hash== $ curr_hash]。
   然后
       #相同的文件存在。 (也许在另一个名字)
       # 没做什么
       存在= 1
       打破
   科幻
DONE如果[[$存在= 1]!]然后
   如果[[-f $ destination_folder / $文件]];然后
       CP$ file.JPG$ destination_folder / $ file.JPG
   其他
       CP$ file.JPG$ destination_folder
   科幻
科幻

未经测试。

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. 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" ]; 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 ; 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.

这篇关于有一个文件夹与具有相同名称但不同的文件的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆