Take the 2-minute tour ×
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.

If I move a folder with mv called "folder" to a directory that already contains "folder" will they merge or will it be replaced?

share|improve this question

3 Answers 3

up vote 11 down vote accepted

mv cannot merge or overwrite directories, it will fail with the message "mv: cannot move 'a' to 'b': Directory not empty", even when you're using the --force option.

You can however use rsync with the --remove-source-files option (and possibly others) to merge one directory into another.
rsync won't delete any directories, so you will have to do something like find -type d -empty -delete afterwards to get rid of the empty source directory tree.

share|improve this answer
    
It sounds like you've tried just one implementation of mv. This answer would be better with a broader truth. Linux, BSD and "real" Unix, or a reference from POSIX or SUS. –  Warren Young May 3 at 19:29
    
@WarrenYoung You're right, I only tried the mv implementation used by Debian - the emphasis being on tried, since the manpage doesn't mention this behavior... –  n.st May 3 at 23:47
rsync -av /source/ /destination/
(after checking)
rm -rf /source/
share|improve this answer
    
Will this remove the source files like in the comment by n.st? –  Dominique May 3 at 19:01
2  
No, I would prefer to make it in two steps for safety reasons. Merged and removed source is unreversible. Additon step in n.st anwer is also needed (to remove directories). –  fazie May 3 at 19:10
    
--remove-source-files has the advantage of only removing files that were transferred successfully, so you can use find to remove empty directories and will be left with everything that wasn't transferred without having to check rsyncs output. –  n.st May 3 at 23:51

One way to accomplish this would be to use:

mv folder/* directory/folder/
rmdir folder

As long as no two files have the same name in folder and directory/folder, you will achieve the same result i.e. merging.

share|improve this answer
2  
How exactly does rm folder work? –  JakeGould May 4 at 4:28
1  
@JakeGould Not at all. :) –  n.st May 4 at 6:58

Your Answer

 
discard

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.