BASH - merge directories when using mv

Solution 1:

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.

Solution 2:

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