Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I want to move content of a directory within another directory with the same folders.

Here's an example:

I have ./backup wich have the directories "base" and "test". Now I want to move these directories in ./backupArchives.

I use this: mv ./backup/* ./backupArchives

but I got the error mv: cannot move './backup/base' to './backupsArchive/base': Directory not empty

I tried using --force, but no luck. Is there a way to move it when folders already exists?

Note: I just want to merge contents, there's no overwriting.

share|improve this question
    
Are you just trying to merge the folders' contents into the destination, or are you trying to overwrite what's in the destination. (it also helps if you have the ENTIRE error, without using ...... to truncate data) –  Thomas W. Mar 19 '13 at 4:13
    
I just want to merge the contents, there's no overwriting. –  UdK Mar 19 '13 at 4:20
1  
@UdK Even if this would work, note that this would not merge the contents of the two directories. Moving a directory will replace its target. Have a look at rsync instead, it will do the merging. –  zwets Mar 19 '13 at 7:38

3 Answers 3

up vote 35 down vote accepted

Though its man page doesn't document it, mv will refuse to rename a directory to another directory if the target directory contains files. This is a good thing in your case because you turn out to want to merge the content of the source into the target, which mv will not do.

Use rsync -a backup/ backupArchives/ instead. After that rm -rf backup/*.

Instead of using rsync, you also can do the classical

(cd backup && tar c .) | (cd backupArchives && tar xf -)

which earns you more geek points.

share|improve this answer
2  
nice things to know! Both are working great. Thank's! –  UdK Mar 20 '13 at 17:54
    
Geek points are the best points :D –  MadMike Oct 11 '13 at 12:54
1  
should be noted this requires you have the size of the folder in free space. And will cause horrible problems if not. –  Pogrindis Oct 2 at 21:07
    
@Pogrindis Please explain what horrible problems will occur, and how they are different from any other attempt to write to a full disk? Indeed, since both operations keep the original directory intact, if the operation fails you simply rm -rf backupArchives without consequences, let alone 'horrible' ones. –  zwets Oct 5 at 9:15
    
Also, must be noted, that rsync solution uses copying, which can be much slower with large files than mv. –  Ivan Balashov 14 hours ago

After the directory you moving you need * (represents any text or number). For example:

mv /var/www/* /recovery/wwwrecovery/

thats all, if you moving files, than you move as:

mv /var/www/index.php /recovery/index.php

Another way is to pack that folder content by using tar:

tar -cvzpf backup.tar.gz /var/www 

Then move it lie any other file. Also I recommend this step because tar compresses it and make it smaller in size.

To extract the files to another folder use

tar -xvzpf /var/www/

If you need to copy to a location you don't own, make sure to prepend your command with the sudo command after whichever option you decide to use.

sudo tar -cvzpf backup.tar.gz /var/www/
share|improve this answer
    
I'm using Ubuntu 12.10 and I got: mv: invalid option -- 'R' Try `mv --help' for more information. Indeed .tar is nice, but I need to know how to move my files for this situation. Thank's for your help –  UdK Mar 19 '13 at 4:34
    
i using 12.04 desktop and server and in both works well. be carefull, if you didnt make space betwen folfer and -R. i also tryed without -R works in same way for me, try without -R, but be carefull with spelling: linux matters lower and uppercase letters!!! than if path is incorect movingin or movingfrom location. if you dont have that folder that you wana move to just put -C after it and it will create it. also try first: find -iname name of your folder and run this command from root, just to see if it exists in other place if you miss spelled something. give me exact command that you used –  Dimitris Theodoridis Mar 19 '13 at 4:47
    
what thaaa..... put that filename in betwen ** –  Dimitris Theodoridis Mar 19 '13 at 4:54
1  
That -R surely shouldn't come after the first argument. Options (nearly) always precede the command arguments. Also, mv has no -R option, and finally, recursion is irrelevant to a mv. @DimitrisTheodoridis please test your answer first. –  zwets Mar 19 '13 at 7:34
    
yes my bro you are right, i always have problems with my spelling, and allways i have problem with terminal for that or command not found or create new files and directories that i dont know, just for bas spelling... by the way i used cp command, than here wrote mv sory for that bro :D –  Dimitris Theodoridis Mar 19 '13 at 17:23

Quick and dirty, if you know what you are doing:

cp -r ./backup/* ./backupArchives && rm -R ./backup/*
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.