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.

I need to essentially merge changes on two trees, applying the changes from branch 2 to branch 1. I have trees like this:

media
├── cd
│   ├── 0
│   │   ├── file1
│   │   ├── file2
│   │   └── file3
│   ├── 1
│   │   ├── file1
│   │   ├── file2
│   │   └── file3
│   └── 2
│       ├── file1
│       ├── file2
│       └── file3
├── dvd
│   ├── 0
│   │   ├── file1
│   │   ├── file2
│   │   └── file3
│   ├── 1
│   │   ├── file1
│   │   ├── file2
│   │   └── file3
│   └── 2
│       ├── file1
│       ├── file2
│       └── file3
└── stuff
    ├── 0
    │   ├── file1
    │   ├── file2
    │   └── file3
    ├── 1
    │   ├── file1
    │   ├── file2
    │   └── file3
    └── 2
        ├── file1
        ├── file2
        └── file3

I've changed the files in my branch 2, and I need to apply them to branch 1. If I just try moving them over branch 1, I get the following errors:

mv: cannot move `/branch2/media/cd' to `/branch1/media/cd': Directory not empty.

Is there another command I should be using for this? mv -f doesn't seem to do the trick.

share|improve this question
    
Can you post the command that you are using? –  jasonwryan Oct 19 '11 at 22:48
    
simple mv /branch2/media/* /branch1/media/ –  Naftuli Tzvi Kay Oct 19 '11 at 22:49
1  
I'm probably missing something, but doesn't mv branch2/media/ branch1/media work? –  jasonwryan Oct 19 '11 at 22:55
1  
No, as noted below, mv won't overwrite files in existing subdirectories. –  Naftuli Tzvi Kay Oct 19 '11 at 23:23
add comment

2 Answers

up vote 5 down vote accepted

Why not use rsync instead?

rsync -a /branch2/media/ /branch1/media/

The reason why mv can't move /branch2/media/cd/ to /branch1/media is because /branch1/media already has a cd/ in it. mv refuses to clobber non-empty directories.

share|improve this answer
    
That looks like it'll work, I ended up using something like this: yes | cp -af /branch2/media/* /branch1/media. –  Naftuli Tzvi Kay Oct 19 '11 at 23:24
add comment

You would generally prefer using rsync or any other versioning tool.

If you still want to hack, you could start by removing the existing files in /branch1/media/ with find

find /branch1/media -type d -exec rm -rf {} \+

You then proceed to your move

for i in $(find /branch2/media -type d); do
    mv /branch2/media/$dir /branch1/media
done

You should probably tweak those commands a bit according to your environment, but no reason it wouldn't work.

PS be careful with the find -exec rm -rf call. I usually start by checking my find command with ls before using rm

share|improve this answer
add comment

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.