Ubuntu: mv: Directory not empty

Опубликовано: 19 Июнь 2019
на канале: Roel Van de Paar
607
6

Ubuntu: mv: Directory not empty


Question: 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.

Solutions Sample (Please watch the whole video to see all solutions, in order of how many people found them helpful):

== This solution helped 172 people ==
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.

== This solution helped 1 person ==
Before asking the question, let's consider a simplified case.
Suppose in /home/admin we have two folders foo and bar which have the same
subdirectory structure, but each contains different files in it. Now we want to
merge them into one. When we do things like mv foo/* bar, the error mv:
directory not empty occurs.
My solution is to give up command line tools and switch to a GUI file manager,
for example, dolphin. When you rename foo to bar in dolphin, it gives you the
option to write into the destination folder, and asks you whether to overwrite
existing files, interactively. This avoids copy and delete, which in effect
saves your time without a single line of code.
PS: I didn't test every file manager out there. But most of them should have
this feature.

== This solution helped 29 people ==
Quick and dirty, if you know what you are doing:
cp -r ./backup/* ./backupArchives && rm -R ./backup/*

== This solution helped 3 people ==
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/

With thanks & praise to God! With thanks to the many people who have made this project possible! | Content (except music & images) licensed under cc by-sa 3.0 | Music: https://www.bensound.com/royalty-free... | Images: https://stocksnap.io/license & others | With thanks to user zwets (https://askubuntu.com/users/134479), user UdK (https://askubuntu.com/users/141547), user patrickvacek (https://askubuntu.com/users/173694), user Pabi (https://askubuntu.com/users/173631), user navigaid (https://askubuntu.com/users/431111), user Mark Paskal (https://askubuntu.com/users/15939), user Dimitris Theodoridis (https://askubuntu.com/users/141402), user Atillo Regner Rex (https://askubuntu.com/users/659648), and the Stack Exchange Network (http://askubuntu.com/questions/269775). Trademarks are property of their respective owners. Disclaimer: All information is provided "AS IS" without warranty of any kind. You are responsible for your own actions. Please contact me if anything should be amiss at Roel D.OT VandePaar A.T gmail.com.