I am writing a bash script to copy/move a folder called "folder" to a directory that already contains "folder" and I would like the contents to be merged.

I am attempting to use a solution from this question:

Merging folders with mv?

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

If i type the first line in the terminal, source "folder" and destination "folder" are merged as expected.

However, When i run the script with the line in it, instead of merging the folders the destination now contains two folders; "folder" and "blank", where "blank" has the contents of the source "folder" in it.

  • What do you mean by "blank named folder"? The content of source/folder was not copied to destination/folder?
    – Freddy
    Commented Nov 3, 2019 at 1:46
  • After i run the script, destination now contains two folders; "folder" and "blank", where "blank" has the contents of the source "folder" in it.
    – puyanera
    Commented Nov 3, 2019 at 1:51
  • 1. are you using variables to hold the source and destination directory names? 2. if so, are you double-quoting the variables when you use them (i.e. cp "$source" "$destination" rather than cp $source $destination - the latter, without the double-quotes, will fail in all sorts of cases that the former, with quotes, will handle without problem). see Why does my shell script choke on whitespace or other special characters?
    – cas
    Commented Nov 3, 2019 at 1:52
  • @cas It's a very short script so currently the source and destination are hard coded: cp -rl /volume1/Sync/TVSync/* /volume1/Sync/TempSync/
    – puyanera
    Commented Nov 3, 2019 at 1:56
  • ok, so not a whitespace issue then. what do you mean by "blank named folder"?
    – cas
    Commented Nov 3, 2019 at 2:08

2 Answers 2


To append SOURCE to DESTINATION to become SOUCE+DESTINATION is more complicated (for self), look up

 man append 

You might try:

    cp  --recursive  --preserve --update  SOURCE DESTINATION     

use "--preserve" to preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all

use "--recursive" to copy directories recursively

use "--update" to copy when the SOURCE file is newer than the destination file or when the destination file is missing.

For help in terminal:

   cp --help   


  man cp  

When terminal works, try it in your bash script


  • Thanks @polpak , the odd thing is that the first line works as expected in the terminal but not in the script. I'll edit the question to mention that.
    – puyanera
    Commented Nov 3, 2019 at 1:47
  • From help -l, --link hard link files instead of copying -L, --dereference always follow symbolic links in SOURCE
    – polpak
    Commented Nov 3, 2019 at 2:23

I think that a better way of doing this would be to use a better tool.

rsync -avhP --remove-source-files "source/folder" destination

-a is for archiving (keep user/group, modes, etc. in tact)

-v just for verbosity, so you can watch what it's doing

-h because I like Human Readable info

-P so rsync will pick up on partial transfers, just in case

--remove-source-files will remove everything that rsync transfers

I find that I often have to kludge empty folder removal as housekeeping:

find source/folder -type d -exec rmdir {} \;

I prepend that in zsh with repeat 10 and it cleans 10 levels deep of empty folders. :-)

You must log in to answer this question.

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