Get last file only from sftp using shell script

I need to get only the last file from sftp, indicating their date, for example, yyyy-mm-dd. I tried the command to be low, but it will get the whole file that is in the directory.

latest_file = `ls -ltr | tail -1 | awk '{print $9}'`
scp -r $latest_file username@server_name:/path /my/directory

Is there any command to get the latest file from sftp using a shell script?

+4
source share
1 answer

To retrieve a remote file that was recently modified on a remote system in /path:

latest_remote_file = $(ssh username@server_name 'ls -tr /path | tail -n 1')
scp -r username@server_name:/path/$latest_remote_file /my/directory

There were problems with the initial script. The first command received the last edited local file, and the scp command had three arguments instead of two.

+2
source

Source: https://habr.com/ru/post/1629831/


All Articles