There are a number of ways to go about merging the directories. The difficulty lies in checking if an file with the same name already exists in the target directory and then incrementing the name of the source file to avoid overwriting the target.
Given your naming convention of #.TIF
, one way to accomplish this to scan the files in the target directory and determine the max #
. You also need to scan the source files as well to account for the case where a source file does not already exist in the target.
One you have the max #
for the set of files, you simply loop over each file, checking if it already exists, if so, move the file from its current #
to # + max
in the target dir. If the file doesn't already exist, just move it. The same applies for a source dir that does not already exist in the target directory structure -- just move it.
Putting all the pieces together, you could do something similar to the following where the script takes as arguments the old
directory name, new
directory name, and ext
(default TIF
):
#!/bin/bash
[ -z "$1" -o -z "$2" ] && { ## validate 2 arguments given
printf "error: insufficient input, usage: %s old new\n" "${0##*/}"
exit 1
}
old="$1" ## assign arguments to 'old' and 'new' (readability)
new="$2"
ext="${3:-TIF}"
[ -d "$old" -a -d "$new" ] || { ## validate both old/new are directories
printf "error: invalid input.\n"
[ -d "$old" ] || printf " '%s' is not a directory.\n" "$old"
[ -d "$new" ] || printf " '%s' is not a directory.\n" "$new"
exit 1
}
while read -r dname; do ## for each old directory
max=0 ## zero max
nname="${dname/$old/$new}" ## generate new dir name
if [ -d "$nname" ]; then ## check if new dir exists, if so
## get max file # in old & new
for i in "$dname"/*; do ## for each file in old
[ -f "$i" ] || continue ## skip if not file
fn="${i%.$ext}" ## get the number
fn=${fn#${dname}/}
((fn > max)) && max=$fn ## update max if greater
done
for i in "$nname"/*; do ## do the same for new dir
[ -f "$i" ] || continue
fn="${i%.$ext}"
fn=${fn#${nname}/}
((fn > max)) && max=$fn
done
## move files from old to new
for i in "$dname"/*; do ## for each file in old
[ -f "$i" ] || continue
newfn="${i/$old/$new}" ## form new file name
if [ -f "$newfn" ]; then ## check if it exists, if so
fn="${i%.$ext}"
fn=${fn#${dname}/} ## add max to number and mv
printf "mv %s %s\n" "$i" "${newfn%/*}/$((fn + max)).$ext"
mv "$i" "${newfn%/*}/$((fn + max)).$ext"
else ## otherwise, just mv
printf "mv %s %s\n" "$i" "$newfn"
mv "$i" "$newfn"
fi
done
else ## if no new dir exists, move old new
printf "mv %s %s\n" "$dname" "$nname"
mv "$dname" "$nname"
fi
done < <(find "$old" -type d)
For example, if you had the following old and new directory structures:
$ tree old
old
├── 22
│ ├── 1.TIF
│ ├── 2.TIF
│ ├── 3.TIF
│ └── 4.TIF
├── 23
│ ├── 1.TIF
│ ├── 2.TIF
│ ├── 3.TIF
│ ├── 4.TIF
│ └── 5.TIF
└── 24
├── 1.TIF
└── 2.TIF
$ tree new
new
├── 22
│ ├── 1.TIF
│ ├── 2.TIF
│ └── 3.TIF
└── 23
├── 1.TIF
├── 2.TIF
└── 3.TIF
The script would produce the following results (you can delete the printf
statements, or redirect the output to /dev/null
to suppress the text)
$ bash mvoldnew.sh old new
mv old/24 new/24
mv old/22/1.TIF new/22/5.TIF
mv old/22/2.TIF new/22/6.TIF
mv old/22/3.TIF new/22/7.TIF
mv old/22/4.TIF new/22/4.TIF
mv old/23/1.TIF new/23/6.TIF
mv old/23/2.TIF new/23/7.TIF
mv old/23/3.TIF new/23/8.TIF
mv old/23/4.TIF new/23/4.TIF
mv old/23/5.TIF new/23/5.TIF
now the new
directory tree contains the following:
$ tree new
new
├── 22
│ ├── 1.TIF
│ ├── 2.TIF
│ ├── 3.TIF
│ ├── 4.TIF
│ ├── 5.TIF
│ ├── 6.TIF
│ └── 7.TIF
├── 23
│ ├── 1.TIF
│ ├── 2.TIF
│ ├── 3.TIF
│ ├── 4.TIF
│ ├── 5.TIF
│ ├── 6.TIF
│ ├── 7.TIF
│ └── 8.TIF
└── 24
├── 1.TIF
└── 2.TIF
Look it over and let me know if you have any questions, or if I misunderstood your clarification.