I am trying to get the last 50 most recent modified files from a remote server using bash shellscript. Currently, I can get one file via sftp using the below code. What is a good solution for this problem?
I am not storing a "copy" of all the files from remote server on my server. So I don't believe rsync works in this context.
fileName=$(echo "ls -1tr" | sftp myid@removeserver | tail -1)
echo "get $fileName $local_directory" | sftp myid@removeserver
CodePudding user response:
With a current bash
and a for
loop:
destdir="/tmp"
# get last 50 file names and save in array fileName
mapfile -t fileName < <(echo "ls -1tr" | sftp myid@removeserver | tail -50)
# get files from array fileName and save in $destdir
for f in "${fileName[@]}"; do echo "get \"$f\" \"$destdir\""; done | sftp myid@removeserver
I assume that the file names do not contain line breaks.