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?
2 Answers
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:
- 206
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
- 148
- 6
scp dest:\ssh dest find -type f -newer start -not -newer end` .` – Edward Betts Mar 31 '14 at 11:11