How to move a folder to another path and to move back to same tree merging its contents on Unix? On my Android phone, I have:


A cron job to move files at 1:59am from /sdcard/WhatsApp/Media to /sdcard/wppmediabkp folder before WhatsApp backup at 2:00am:


mv -f /sdcard/WhatsApp/Media /sdcard/wppmediabkp


A cron job to move files at 2:30am back (from /sdcard/wppmediabkp to /sdcard/WhatsApp/Media):


mv -f /sdcard/wppmediabkp /sdcard/WhatsApp/Media


The "cron jobs" are tasks on "Tasks!" app (scheduling them with 00 59 1 ? * * and 00 30 2 ? * *). The commands are tested in Termux app also.


Aim:


To exclude Media folder from backup, but to also restore image "link" to chats (to do not download again).


On restoration of wppmediabkp folder to /sdcard/WhatsApp/Media:


Sometimes it works well (it creates the /sdcard/WhatsApp/Media with all its contents). It happens when /sdcard/WhatsApp/Media folder does not exist. For me, it is the expected behavior.
Sometimes it creates a subfolder (/sdcard/WhatsApp/Media/wppmediabkp). It happens when /sdcard/WhatsApp/Media folder exists. For me, it is the issue - I would like to restore original folder tree, merging its contents.


I think it was reduced to a Unix issue, because I can use all Unix basic commands.

How to solve it (to get Media folder back merging its contents) using mv command?
One approach is not to move directories. Let them exist all the time, move their content back and forth:

mv -f /sdcard/WhatsApp/Media/* /sdcard/wppmediabkp/
# and later
mv -f /sdcard/wppmediabkp/* /sdcard/WhatsApp/Media/


Notes:


* will not match hidden files/directories;
with too many objects you may hit "argument list too long" error.


In case any of this is an issue, the solution should use something like find … -exec mv …. Either way move what is inside the directory, not the directory itself.