SCP how to copy files from a remote server to a local that match some criteria

Scenario: we need to copy from a remote server to local files that have been modified the last two days.

  1. the ssh part connects to remote_server and get files from directory /files that have been modified the last two days
  2. The xargs part gets the piped list of found files and pass them to scp which copies them to current directory of our local server
$ ssh remote_server 'find /files/ -mtime -2 -type f' | xargs -I {} scp remote_server:{} .

Written by

DevOps engineer, loves Linux, Python, cats and Amiga computers

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store