usr_11
usr_11

Reputation: 576

Download files modified after given time with sftp in bash

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.

Upvotes: 1

Views: 1280

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202282

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.

Upvotes: 2

Related Questions