使用通配符和循环从sftp服务器下载文件

Downloading files from sftp server with wildcard and in a loop

我正在尝试从 sftp 服务器下载一堆文件。服务器的组织如下:每年有一个文件夹,每个年度文件夹,每天有一个文件夹。我每天要下载 8 个以 .nc 结尾的文件。文件本身的名称太疯狂了,无法跟踪。我尝试了几种不同的方法,但未能成功地给出 get 获取文件的正确说明。 我当前的版本是在连接到 sftp 之前有一个循环,这样我就可以写下名称,然后连接到 sftp 并下载该文件:

for i in `seq 1 1 366`;
        do
            if [ $i -lt 10 ]; then
                today='00$i/*.nc' 
            elif [ $i -ge 10 ] && [ $i -lt 100 ]; then
                today= '0$i/*.nc' 
            elif [ $i -ge 100 ]; then
                today= '$i/*.nc' 
            fi
        done

sshpass -p $(cat ~/Code/sftp_passwd.txt) sftp cgerlein@cygnss-sftp-1.engin.umich.edu <<EOF

cd ..
cd ..
cd /data/cygnss/products/CDR/l1/2019/

get $today /scratch/myfolder/ 
quit
EOF

我认为 get 不喜欢其中的通配符。这甚至可能不是最好的方法。有什么建议么?谢谢!

您可以使用 printf() 格式化带前导零的文件名。

sftp 命令需要在循环内。

today= 后不能有 space。

在更改为绝对路径名之前不需要 cd ..

for i in {1..366}
do
    today=$(printf "%03d/*.nc" $i)
    sshpass -p $(cat ~/Code/sftp_passwd.txt) sftp cgerlein@cygnss-sftp-1.engin.umich.edu <<EOF
cd /data/cygnss/products/CDR/l1/2019/
get $today /scratch/myfolder/ 
quit
EOF
done

顺便说一句,您可以使用 curl 进行通配符下载,请参阅 downloading all the files in a directory with cURL