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