Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I need to copy db log files between two suse servers where I am interested in files ONLY in between 10.3.2013 - 13.3.2013

It is desired to compress the files before copying so I tar them and scp. Currently I am using tar -cvzf /tmp/saas_archive_logs.tar.gz /var/lib/edumate/backup/archive_logs/db2inst1/SAAS --newer-mtime=2013-03-10 that gives me all files from 10.3.2013 till now. But I don't need all of them. And I didn't find any tar switch that would help me.

share|improve this question

2 Answers 2

up vote 4 down vote accepted

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
share|improve this answer
    
Can I specify hours and minutes when using your code? –  Radek Apr 15 '13 at 6:17
    
I tried before posting my comment but ... "find: I cannot figure out how to interpret `201302050000' as a date or time" –  Radek Apr 15 '13 at 6:30
    
@Radek The short answer is Yes. I would use YYYYMMDDThhmm in order not have to deal with spaces. The long answer: man find will tell you: Time specifications are interpreted as for the argument to the -d option of GNU date and with man date you will find ... a mostly free format human readable date string ..... A date string may contain items indicating calendar date, time of day, time zone, day of week, relative time, relative date, and numbers –  Anthon Apr 15 '13 at 6:43
    
@Radek thanks for pointing the error out, since I could not edit my comment, I reinserted a new one with a T inserted between the Days and Hours. That I tested and works. Somehow find does not know how to split the string otherwise (probably because it could be YYMMDDhhmmss instead of YYYYMMDDhhmm. I thought that date would need a dot for the seconds (.ss) but that is probably only for setting the date. –  Anthon Apr 15 '13 at 6:44

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

share|improve this answer
2  
the argument to -mtime has to be a offset in days that is not always unless you that offset is fixed between running times. You can also use -mtime +X and -mtime -X to specify before and after X days ago instead of using -not. –  Anthon Apr 12 '13 at 5:01
    
What would be the syntax to specify absolute date & time? –  Radek Apr 15 '13 at 6:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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