BASH - merge directories when using mv

I just started using BASH, so I can move all contents of a directory to another. But the issue is, I was expecting it to merge the directories. I am using cron job to run this script.

#!/bin/bash
shopt -s dotglob nullglob
mv mv_schedule/* public_html/

Source: /mv_schedule/ (containing folders/files)

/files/
4.html
5.html
/assets/
sitemap.xml

Destination: /public_html/ (previous folders already exist)

/files/
1.html
2.html
3.html
/assets/
sitemap.xml

So what I want, is for all of the new files from those directories inside /mv_schedule/ to merge with the ones in /public_html/, and for the files that already exist in there, to be replaced with the ones coming from /mv_schedule/.

It's currently returning this error via email when I try this with the current script:

mv: cannot move `mv_schedule/assets' to `public_html/assets': Directory not empty

How can I fix this?

2

2 Answers

What the error message is telling you is that it cannot mv the asset directory because there are files inside it. mv a directory means making a copy, then erasing it, and it cannot erase it because it still contains some files. This means you should mv the files inside asset as well, and even the subdirectories and files within subdirectories within asset, if they exist. But the mv command that issued can only copy (and the erase) what is present inside the mv_schedule directory, not what is present within the subdirectories (like asset) within mv_schedule.

What you want is a command that descends the directory tree down to the last leaf and copies it, something like a -r or -R option in rm, chmod, chown. However, mv has no such option, so you will have to use the command find, which descends the whole directory tree from a root you specify, and then executes an action you specify. In your case, a suitable command is:

SOURCE_DIR=$1
TARGET_DIR=$2
find $SOURCE_DIR -name '*' -type f -exec mv -f {} $TARGET_DIR \;

This will pile up all files into a single target directory. It assumes that you passed the source and target directory as input-line parameters to a bash script. The -f option prevents asking for confirmation in case of overwriting, you can change that to -n (do not overwrite) or -i (ask before overwriting).

If instead you wish to preserve the directory structure, remember that the command cp does have the ability to descend the directory tree, so that you may use that one, followed by a rm, since this command also has the ability to descend trees. One possible set of commands is:

SOURCE_DIR=$1
TARGET_DIR=$2
cp -a $SOURCE_DIR $TARGET_DIR
rm -rf $SOURCE_DIR

Notice the -a option in cp: it preserves timestamps and ownerships. If you do not care for such things, you can use -R instead.

9

There is a more generic discussion of this problem in the Unix section.

You can use the -l option of the cp command, which creates hard links of files on the same filesystem instead of full-data copies. The following command copies the folder source/folder to a parent folder (destination) which already contains a directory with the name folder.

cp -rl source/folder destination
rm -r source/folder

Notes:

  • You may also want to use the -P (--no-dereference - do not de-reference symbolic links) or -a (--archive - preserve all metadata, also includes -P option), depending on your needs.
  • Though there are two "I/O" steps involved, the steps are relatively simple metadata operations involving zero "data" transfers. Thus this method is magnitudes faster than a cp (sans -l) or rsync-based solution.
  • This does not work if your source and destination folders are on different filesystems
0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like