Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.


shopt -s dotglob nullglob
mv mv_schedule/* public_html/

Source: /mv_schedule/ (containing folders/files)


Destination: /public_html/ (previous folders already exist)


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?

share|improve this question
Are you running this bash script as root user or normal user ? if it is a root user then what is the output of alias -a / – max Oct 9 '13 at 5:39
Unfortunately, I'm not sure. I'm only on cPanel of shared hosting, so I probably don't have true root access. – The Bash Oct 9 '13 at 5:45
up vote 6 down vote accepted

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:

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:

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.

share|improve this answer
I'm afraid I don't completely understand your answer. But I would place SOURCE_DIR and TARGET_DIR with dirName/? Also, I see you mentioned -i(ask before overwriting). I want it to overwrite existing files, and merge with existing directories, to keep all existing files in the destination directory and add(also replace existing files) the new ones from same name directory from source. – The Bash Oct 9 '13 at 6:16
you can set SOURCE_DIR and TARGET_DIR to the values you want, in this case SOURCE_DIR=mv_schedule and then TARGET_DIR=public_html. You can overwrite with the -f option, but you have not clarified what you want to do with the files in, say, the directory asset. Should they go 1) inside the public_html directory, or should they go into the directory public_html/assets? The two options I have proposed do these two different things, respectively. – MariusMatutiae Oct 9 '13 at 6:41
I added a couple of lines to my answer, trying to clarify it. – MariusMatutiae Oct 9 '13 at 6:54
rm -rf $SOURCE_DIR will remove the source directory, perhaps it's better to preserve it and use rm -rf "$SOURCE_DIR/*" -- It's also advised to use quotes around variables (in case directory names have spaces) – medigeek Oct 9 '13 at 7:07
Well, since I know it's not possible to use mv to simply move the files.. I'd like to leave the contents of the source directory intact. And to clarify again so everyone understands, it's 'only' the contents of /mv_schedule/, not the directory /mv_schedule/` itself. – The Bash Oct 9 '13 at 8:01

Excellent answer from palswim. (Also, thanks for the upvote for those last two points needed to be able to upvote myself!)

The cp -rl ; rm sorts out the issue and with minimal I/O (cp and rsync are stupidly expensive in that regard).

There are two minor issues with it:

  • it is not 100% atomic - it takes two I/O operations (this applies to the rsync method as well, as --remove-source-files actually still requires a separate I/O operation).
  • it does not work if the source and destination are on different filesystems
share|improve this answer

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


  • 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
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .