3

I have a bash script which copies several files to a remote server using scp. This script works fine but now I need to add a file that contains a wildcard in the name and i have a problem with it.

#!/bin/sh
files=('path1/subpath/file.*.ext' 'path2/subpath2/nowildcard.ext2' 'path3/subpath3/file3.*.ext3');

for j in "${files[@]}"; do
    echo "File \033[1;38;5;226m$j\033[0m is copying."
    scp -P12345 $j "name@host:/permanent/path/$j";
done

The script copies the files with wildcard (e.g. file.12345.ext or file3.4321.ext3) but saves on the remote server as file.* .ext and file3.*.ext3. I tried to use a backslash in the file names but in this case the script does not copy the files at all.

How to fix this issue?

Thanks in advance.

1 Answer 1

4

Arrays are not a feature found in /bin/sh, so use #!/bin/bash

scp by default shows a progress of the files transferred so I question whether you really need to print out each filename by yourself.

Your wildcards are not expanding because you add them into the array with quotes, and then you quote the array expansion in the for loop, so the wildcards are not expanded then either.

Let the wildcards expand when you store them in the array, and send all the filenames separately as scp arguments:

#!/bin/bash
files=( 
    path1/subpath/file.*.ext 
    path2/subpath2/nowildcard.ext2 
    path3/subpath3/file3.*.ext3
)
scp -P12345 "${files[@]}" name@host:/permanent/path/

You can further simplify by omitting the array altogether, in which case you can fall back to /bin/sh:

#!/bin/sh
scp -P12345                         \
    path1/subpath/file.*.ext        \
    path2/subpath2/nowildcard.ext2  \
    path3/subpath3/file3.*.ext3     \
    name@host:/permanent/path/
2
  • Thank you for your help. However, please let me disagree a bit with you:). This script works perfectly with /bin/sh (Mac OS 10.9). The solution you provided me with works. It copies the files with wildcards file.12345.ext and file3.4321.ext3 but saves them in the root directory (/permanent/path/). But I need to retain their original paths. That's why I used /permanent/path/$j in my script. You're right about the quotes – that was the origin of the problem. Just removed them in my script and it started working as it is supposed to. Thank you so much for your help!
    – Ssey
    Mar 2, 2014 at 6:39
  • 1
    You're getting away with #!/bin/sh because on recent versions of OS X, sh is really bash running in compatibility mode. But just because you can get away with it, doesn't mean it's a reasonable thing to do. Mar 2, 2014 at 17:14

You must log in to answer this question.

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