How to scp/tar files that are in between specific days?
As Bichoy indicated you can use the find
command to find files with a specific access, create and modification time. However -mtime takes an offset in 24 hour increments and is not always convenient to calculate unless you want something from a specific amount of numbers of 'days' ago. You will need to combine that with -daystart
to 'round' that to the beginning of the day.
I think more convenient in your case, is the -newermt option which takes a datestring (and not the name of a reference file like most -newerXY versions)
Combine that with find
's -print0
option to handle files with spaces in the name and optionally -type f
not to get any directories in the period you are interested in:
find /var/lib/edumate/backup/archive_logs/db2inst1/SAAS \
-newermt 20130310 -not -newermt 20130314 -type f -print0 \
| xargs -0 tar -cvzf /tmp/saas_archive_logs.tar.gz
There is one big problem with that: in case the number of files found becomes to long, xargs
will invoke its command (in this case tar
) multiple times as xargs
needs to fit the arguments on the commandline which is not infinite.
To circumvent that I always use cpio
, which reads filenames from stdin. With the --format=ustar
parameter to get a POSIX tar file, and in your case you would need to pipe the output through gzip
to get the desired result:
find /var/lib/edumate/backup/archive_logs/db2inst1/SAAS \
-newermt 20130310 -not -newermt 20130314 -type f -print0 \
| cpio --create --null --format=ustar \
| gzip > /tmp/saas_archive_logs.tar.gz
You can check the find
command to get a list of the files that needs to be tared.
You can specify a start and end date (up to seconds precision) using the normal -atime
, -btime
, -mtime
... arguments in combination with the -not
argument.
You can then pipe the output to xargs
and then to tar
. Check the man page of find
for details about time arguments.
Update:
As Anthon suggested, you may use the +/- modifiers with -mtime
to specify the period without using -not
. Here is an example:
find . -mtime -5d2h3m10s -mtime +4d0h15m20s -print0 | xargs -0 tar cjvf mytar.tar.bz2
Where d, h, m, s
corresponds to days, hours, minutes and seconds respectively. This will give files modified newer than 5d2h3m10s
and older than 4d0h15m20s