Using wildcards in scp command

6,533

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/
Share:
6,533

Related videos on Youtube

Ssey
Author by

Ssey

Updated on September 18, 2022

Comments

  • Ssey
    Ssey over 1 year

    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.

  • Gordon Davisson
    Gordon Davisson about 10 years
    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.