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

7 Answers 7

up vote 20 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
3  
The disadvantage of rsync is that it actually copies the data, rather than just changing the hard link, which is potentially resource intensive if you're dealing with a lot of data. –  Jonathan Mayer Sep 13 at 18:53
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

Here is a way that will merge the directories. It is much faster than rsync since it just renames the files instead of copying them and then deleting them.

cd source; find -type f | xargs -n 1 -I {} mv {} dest/{}
share|improve this answer
    
That's interesting but only vaguely relevant to the topic and not even remotely what the user asked about. –  Shadur Sep 5 at 5:14
2  
Actually, Jewel's code does precisely what the user asked for, with the exception of creating missing directories. Perhaps you should look again? –  Jonathan Mayer Sep 13 at 18:44

I'd recommend these four steps:

cd ${SOURCE}; 
find . -type d -exec mkdir -p ${DEST}/\{} \; 
find . -type f -exec mv \{} ${DEST}/\{} \; 
find . -type d -empty -delete

or better yet, here's a script that implements semantics similar to mv:

#!/bin/bash

DEST=${@:${#@}}; for SRC in ${@:1:$(({#@} -1))}; do   (
    cd $SRC;
    find . -type d -exec mkdir -p ${DEST}/\{} \; 
    find . -type f -exec mv \{} ${DEST}/\{} \; 
    find . -type d -empty -delete
) done
share|improve this answer

You can use the -l option of the cp command, which creates hard links of files on the same filesystem instead of full-data copies. The following command copies the folder source/folder to a parent folder (destination) which already contains a directory with the name folder.

cp -rl source/folder destination
rm -r source/folder

You may also want to use the -P (--no-dereference - do not de-reference symbolic links) or -a (--archive - preserve all metadata, also includes -P option), depending on your needs.

share|improve this answer

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
2  
@JakeGould Not at all. :) –  n.st May 4 at 6:58

Here is a script that worked for me. I prefer mv over rsync, so I use Jewel and Jonathan Mayer's solutions.

#!/bin/bash

# usage source1 .. sourceN dest

length=$(($#-1))
sources=${@:1:$length}
DEST=$(readlink -f ${!#})
for SRC in $sources; do
    pushd $SRC;
    find . -type d -exec mkdir -p ${DEST}/{} \;
    find . -type f -exec mv {} ${DEST}/{} \;
    find . -type d -empty -delete
    popd
done
share|improve this answer

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.