'How to get latest file from sftp server to local using mget in linux?

Hi I am following below logic to get the latest file from sftp server. But it is copying all the files. Please help what I need to correct in my logic?

datadir="********"
cd ${datadir}
rm -f ${datadir}/my_data*.csv
rm -f ${logfile}
lftp<<END_SCRIPT
open sftp://${sftphost}
user ${sftpuser} ${sftppassword}
cd ${sftpfolder}
lcd $datadir
mget my_data*csv | sed 's/-\([1-9]\)\./-0\1\./g' | sort -r | sed 's/-0\([1-9]\)\./-\1\./g' | head -1


Solution 1:[1]

In this code, mget my_data*csv will execute first, and its output will be provided to sed as a parameter:

mget my_data*csv | sed 's/-\([1-9]\)\./-0\1\./g' | sort -r | sed 's/-0\([1-9]\)\./-\1\./g' | head -1

You just need to get the filename you want first and then do the mget filename.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jeremy Caney