You don’t show much effort to let us help you solving this. But since I’m learning bash scripting, I took your question as practicing.
So… here is a bash script to rename duplicated files.
/!\ The renamed files will be in the folder of first argument. So, in your case, you’ll have to launch it like this :
-
Rename duplicated files from folder1 and folder2 :
$ mv_duplicates_script.sh /path/to/FOLDER1 /path/to/FOLDER2
-
Rename duplicated files from folder1 and folder3 :
$ mv_duplicates_script.sh /path/to/FOLDER1 /path/to/FOLDER3
-
Rename duplicated files from folder3 and folder2 :
$ mv_duplicates_script.sh /path/to/FOLDER3 /path/to/FOLDER2
Note that you have to set FOLDER2 at last argument for the renamed files go to FOLDER3.
#!/bin/bash
# if missing args, print Usage
##############################
if [[ -z $1 ]] || [[ -z $2 ]]
then
echo "Usage : `basename $0` /absolute/path/to/folder1 /absolute/path/to/folder2"
# exit with failed status
exit 1
fi
# test if folders exist
#######################
if [[ ! -d $1 ]]
then
echo "ERROR : Folder $1 not found..."
exit 1
fi
if [[ ! -d $2 ]]
then
echo "ERROR : Folder $2 not found..."
exit 1
fi
# get filenames from folder 1
#############################
cd $1
i=0
for f in *.txt
do
folder1_files[$i]=$f
((i++))
done
# get filenames from folder 2
#############################
cd $2
i=0
for f in *.txt
do
folder2_files[$i]=$f
((i++))
done
# compare and move duplicates
#############################
for f in ${folder1_files[@]}
do
for g in ${folder2_files[@]}
do
if [[ $f == $g ]]
then
echo "Duplicate file : $g"
echo "Renaiming to DUPLICATE_$g in $1"
mv $1/$g $1/DUPLICATE_$g
fi
done
done
exit 0
Use it as :
$ mv_duplicates_script.sh /absolute/path/to/folder1 /absolute/path/to/folder2
Hope it helps.
PS : This can certainly be improved and your comments/tips are welcome.
solved Rename Duplicate file name to another name in multiple folders [closed]