Search code examples
bashgetsftplftp

How to get specific files with lftp batch file


I'm actually trying to get all files beginning with the current date from another server with SFTP. Nevertheless, * does not work in SFTP so I don't see how I can do...

Here is the batch I've done so far:

#!/bin/sh
today=$(date +%Y%m%d)
lftp<<END_SCRIPT
open sftp://name@server
get $today*
bye
END_SCRIPT

@Martin Prikryl

Thank you, it works fine now. I've search a bit before you answered and I've found this (a lot less efficient^^):

today=$(date +%Y%m%d)
var=`echo `echo ls -1 | sftp name@domain:dir1/subdir` | sed -n 's/ /\n/gp'|sed -n '/^$today/p'`
lftp<<END_SCRIPT
open sftp://name@server
get $var
bye
END_SCRIPT

Solution

  • Use the mget instead of the get.

    See https://lftp.yar.ru/lftp-man.html

    get
    ...
    Does not expand wildcards, use mget for that.
    ...
    mget
    ...
    Gets selected files with expanded wildcards.