21

I am wanting to move (not just copy) a group of files/directories to a different directory some of which have name clashes with files/directories in the target directory. My main objective is to move the files, so I can tolerate the non-empty directory being overwritten.

I am currently using mv ... destination however, occasionally I get

mv: cannot move `target' to /destination/target': Directory not empty

I tried mv -f ... destination with no success and since I want the files to be gone from their original location, rsync doesn't seem to be appropriate.

As a bonus, is there a good solution for preserving the files intended to be overwritten somehow maybe by renaming?

3 Answers3

22

If you use mv --backup=numbered
(or one of the other options for the --backup switch),
then mv will complete the merge and preserve the files intended to be overwritten.

bsd
  • 11,036
0
#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
SRC=/path/to/source
DST=/path/to/destination

for item in $SRC/*; do
    if [[ -f "$DST/$(basename "$item")" ]]; then
        newitem=$(basename $item)_old
        while [[ -f "$DST/$newitem" ]]; do
            newitem=${newitem}_old
        done
        mv "$DST/$(basename $item)" "$DST/$newitem"
    fi
    mv "$item" "$DST/$(basename $item)"
done
DopeGhoti
  • 76,081
0

You can use mc (Midnight Commander) inside the screen and the folders with with Ins and F6

Toolkit
  • 101