0

I am writing a bash script that will get files from one directory of remote host. Here, I want to get files that were modified after given time.

sftp userme@remote_host_addr:/somedirectoryonremoteserver
> mget 

Here, how can I pass the timing to sftp command to get only files whose modified time is greater than given time.

Martin Prikryl
  • 167,268
  • 50
  • 405
  • 846
usr_11
  • 462
  • 7
  • 23

1 Answers1

1

It's rather difficult to implement this using the OpenSSH sftp client.

You would have to:

  • list the directory using ls -l command;
  • parse the results (in shell or other script) to find names and times;
  • filter the files you want;
  • generate another sftp script to download (get) the files you found.

A way easier and more reliable would be to give up on the command-line sftp. Instead, use your favorite scripting language (Python, Perl, PHP) and its native SFTP implementation.

For an example, see:
Download files from SFTP server that are older than 5 days using Python


Or use a more capable SFTP client.

Martin Prikryl
  • 167,268
  • 50
  • 405
  • 846
  • 1
    I achieved it using rsync (only modified files will copy) since it was difficult to implement using sftp – usr_11 Mar 02 '22 at 13:59