tong cat
tong cat

Reputation: 75

Get latest file only from sftp using shell script

I need to get only the latest file from sftp by following their date, for example it's yyyy-mm-dd. I tried the command be low but it will get all the file that 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 shell script?

Upvotes: 4

Views: 5609

Answers (1)

user2664470
user2664470

Reputation: 801

To get the remote file that was most recently changed on the 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 two problems with your initial script. The first command was getting the last edited local file, and the scp command had three arguments instead of two.

Upvotes: 2

Related Questions