Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I have a ton of files and dirs in a subdirectory I want to move to the parent directory. There are already some files and dirs in the target directory which need to be overwritten. Files that are only present in the target should be left untouched. Can I force mvto do that? It (mv * ..) complains

mv: cannot move `xyz' to `../xyz': Directory not empty

What am I missing?

share|improve this question
Have you tried mv -f? –  ierax Mar 22 '11 at 17:16
Ofcourse....... –  EricSchaefer Mar 22 '11 at 17:44

7 Answers 7

up vote 37 down vote accepted

You will have to copy them to the destination and then delete the source, using the commands cp -r * .. followed by rm -rf *.

I don't think you can "merge" directories using mv.

share|improve this answer
Well, that's what I didn't want to do, because it will take a long time... Thanks anyway. –  EricSchaefer Mar 22 '11 at 17:45
Presumably mv is faster because you're on the same filesystem? What if you use cp -l to create hardlinks rather than actually moving the files? –  mattdm Mar 22 '11 at 20:32
You should use cp -a instead of cp -r in order to preserve the file attributes (timestamp, permissions, etc.). –  dotancohen Feb 2 '14 at 7:14

rsync would probably be a better option here. It's as simple as rsync -a subdir/ ./.

My test tree in filename:contents format:


Running rsync:

$ rsync -a -v subdir/ ./
sending incremental file list



And then, to emulate mv, you probably want to remove the source directory:

$ rm -r subdir/



If this is wrong, can you please provide a similar example (e.g. using my test tree from near the top of this answer) with the desired result?

share|improve this answer
rsync copies. This question is about moving. –  Gilles Mar 22 '11 at 22:51
@Gilles: Thanks. I added rm -r at the end to make it basically the same as mv. –  Mikel Mar 22 '11 at 22:53
copy-then-delete isn't equivalent to mv when the source and destination are on the same filesystem. mv is atomic, preserves inode numbers (so the file can remain open), and doesn't take time and space making a copy. –  Gilles Mar 22 '11 at 23:08
@Gilles: I realize that, but currently the leading answer is cp -r; rm -r. I think in that sense, rsync is worth mentioning too. –  Mikel Mar 22 '11 at 23:21
I already did it with cp/rm (it was urgent). It really took a long time. Gilles script would probably have been a lot quicker, but he was too late too. –  EricSchaefer Mar 23 '11 at 10:09

rsync can delete the source after copies with the --remove-source-files parameter. This should be a convenient way to do what you'd like.

From the rsync man page:

        --remove-source-files   sender removes synchronized files (non-dir)
share|improve this answer
This is really the best answer. I couldn't use cp -r; rm because of lack of free space. Instead rsync --remove-source-files both minimized used disk space avoided copying over the exact same files. –  Kasper Souren Aug 5 '14 at 12:10

Here's a script that moves files from under /path/to/source/root to the corresponding path under /path/to/destination/root.

  • If a directory exists in both the source and the destination, the contents are moved-and-merged recursively.
  • If a file or directory exists in the source but not in the destination, it is moved.
  • Any file or directory that already exists in the destination is left behind. (In particular merged directories are left behind in the source. This is not easy to fix.)

Beware, untested code.

export dest='/path/to/destination/root'
cd /path/to/source/root
find . -type d \( -exec sh -c '[ -d "$dest/$0" ]' \; -o \
                  -exec sh -c 'mv "$0" "$dest/$0"' {} \; -prune \) \
    -o -exec sh -c '
        if ! [ -e "$dest/$0" ]; then
          mv -f "$0" "$dest/$0"
' {} \;
share|improve this answer
Two corrections: you need a \; before the -o on the first line of the find command, and you shouldn't escape ! in the if -- it's just !, not \! –  llhuii Apr 20 '11 at 5:30

You can do this with cp and rm, but without copying the massive amount of data you are (presumably) trying to avoid transferring. @mattdm alluded to this in his comment, and an answer for another question has a more complete discussion about various options.

cp -rl source destination
rm -r source

Essentially, the -l option for the cp command creates hard links to files rather than copying their data to new files.

share|improve this answer
I know the OP already completed his task which prompted the question, but hopefully this answer can help anyone with this problem in the future. –  palswim Jan 31 at 7:36
This really the answer they needed. I just did this with 60GB of thousands of small cyrus email files and it took only 21 seconds. –  labradort May 12 at 18:37

I find the easiest way is to just pipe yes to the cp command like this:

yes | cp -R /source/* /destination/
share|improve this answer
What on earth does yes add here? –  Bernhard Mar 24 '14 at 6:21

I just used kind of the same as last one. Without any yes |.. I had a problem which needed overwriting some files over ssh after decompression. What I did was

cp -R * ..

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.