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.

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";

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.


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:

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:

scp -P12345                         \
    path1/subpath/file.*.ext        \
    path2/subpath2/nowildcard.ext2  \
    path3/subpath3/file3.*.ext3     \
  • 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 '14 at 6:39
  • 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. – Gordon Davisson Mar 2 '14 at 17:14

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.