Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
939 views
in Technique[技术] by (71.8m points)

linux - 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. 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...