2

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.

puyanera
  • 21
  • 4
  • What do you mean by "blank named folder"? The content of `source/folder` was not copied to `destination/folder`? – Freddy Nov 03 '19 at 01: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 03 '19 at 01: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?](https://unix.stackexchange.com/questions/131766/why-does-my-shell-script-choke-on-whitespace-or-other-special-characters) – cas Nov 03 '19 at 01: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 03 '19 at 01:56
  • ok, so not a whitespace issue then. what do you mean by "blank named folder"? – cas Nov 03 '19 at 02:08
  • @cas when i type the first line in the terminal, source "folder" and destination "folder" are merged. 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. – puyanera Nov 03 '19 at 10:55

2 Answers2

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

.

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

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. :-)

Edgar Magallon
  • 4,711
  • 2
  • 12
  • 27
captain
  • 41
  • 6