Tuesday, November 8, 2011

mv: cannot move `./directory1' to `../directory2': Directory not empty

I wanted to move all files from one directory structure to another, for me I had an updated version of a lot of website files and I wanted to update them all in one go so I tried:

mv ./directory1/* ./directory2/

Because I have lots of files within sub-directories on both folders the mv command wouldn't play nice and kept throwing this "Directory not empty" error, even doing an "mv -f" to force overrides didn't work..

After search around people have come up with elaborate bash scripts to do the task, and I like to throw a bash script together every now and again but in this situation it's really not necessary, we just need to use rsync:

rsync -avh ./directory2/ ./directory1/

That rsync command will override all the files in directory1 with the files in directory2 and recurse through all sub-directories, it will override them regardless if they're older/newer/different sized etc so do a backup of your original directory before running the command

Rsync is a great tool and you can do so much more than just synchronising local folders, for more advanced options (like only overwriting newer files etc) check out the man page for rsync here: http://linux.die.net/man/1/rsync

Hope this helps somebody out.