10

Is it possible to have SCP filter the files that it copies by date, e.g., if you wanted to copy all files that were created on 12/29 and ignore the others?

7

You can't do this directly with scp. The unix way is to combine tools, you want the find command.

Here is an example of searching for a file with a given date:

touch --date "2007-01-01" start
touch --date "2008-01-01" end
find -type f -newer start -not -newer end

I took this example from here: http://www.cyberciti.biz/faq/linux-unix-osxfind-files-by-date/

To feed this into scp you could do this:

find -type f -newer start -not -newer end -exec scp {} dest: \;

This will call scp once per file, which could be slow because it needs to bring up the connection each time. If you only have a handful of files and there are no spaces in the names you can do this:

scp `find -type f -newer start -not -newer end` dest:
2
  • 1
    It's easy when you copy files from client to server, but what about the other way around? – Petr Peller Mar 30 '14 at 18:14
  • 3
    @PetrPeller To copy in the opposite direction you can do this: scp dest:`ssh dest find -type f -newer start -not -newer end` . – Edward Betts Mar 31 '14 at 11:11
0

An effective (one-line !) alternative (by @sudodus) is given here, which copies through an SSH channel using 'cpio' in copy-out mode. You can tailor the find arguments as you need, perhaps using the time/min/newer tests.

To get your exact date & time test, create a dummy file with the correct time & date on your source system, use the find -newer test, or use the man page's -newerXY test. See https://linux.die.net/man/1/find

The output of the find command running on the remote system is piped back to the local system securely via ssh, and saved.

It appears to do all you require, but does not use 'scp' - which might affect your marking. But it does use a combination of Unix tools - which is the Unix approach !

In any case, scp is a shortcut for the most common cases, this approach is more powerful, when filtering of files is required.

ssh username@ip-adress '(cd /path/to/sourcedir; find . -print | cpio -oBav -Hcrc)' | ( cd /path/to/targetdir && cpio -ivumd )

https://askubuntu.com/questions/1080590/how-to-use-find-in-scp-command

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.