1

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.

6
  • What do you mean by "blank named folder"? The content of source/folder was not copied to destination/folder? – Freddy Nov 3 '19 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 Nov 3 '19 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 Nov 3 '19 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 Nov 3 '19 at 1:56
  • ok, so not a whitespace issue then. what do you mean by "blank named folder"? – cas Nov 3 '19 at 2:08
0

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   

or

  man cp  

When terminal works, try it in your bash script

.

2
  • 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 Nov 3 '19 at 1:47
  • From help -l, --link hard link files instead of copying -L, --dereference always follow symbolic links in SOURCE – polpak Nov 3 '19 at 2:23

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.