Merge script
I needed to combine all the files in two different directories. The challenge was that in two different directories, two different files had the same name. So I wrote a merge script to rename the file from the source directory if the name already existed in the target directory. It appears below:
#!/bin/bash
# Merge the contents of two directories, renaming duplicate files so that no files are overwritten
# usage merge.sh destdir
SRC1="$1"
DEST="$2"
for FILE in $( ls "$SRC1" ); do
let "i = 0"
FN="$DEST/${FILE/%.jpg/-$i.jpg}"
while [[ -e "$FN" ]]; do
let "i += 1"
FN="$DEST/${FILE/%.jpg/-$i.jpg}"
done
ln -v "$SRC1/$FILE" "$FN"
done
It worked for my purposes.
Comments