0

I am trying to take a weekly archive of a folder to separate zip files.

find . -name '*.txt' -ctime +7 -exec zip "archive-$("date+%Y-%U").zip" {} \;

I am stuck on how to name the zip file. Any alternate way to do this would also be welcome. (like better compression using 7z or something else)

3 Answers 3

3

This should be enough for your case:

 find . -name '*.txt' -ctime +7 | zip archive-$(date +%Y%U).zip -@

example:

find . -name '*.txt' -ctime +7 | zip archive-$(date +%Y%U).zip -@

output:

  adding: a.txt (stored 0%)
  adding: b.txt (stored 0%)

Now to make sure of naming:

 ls

output is:

archive-201525.zip  a.txt  b.txt  c
2
  • I'm really sorry for not getting back to you sooner. I was not able to try it out until today. I'm getting a message "zip error: Nothing to do! (archive-201528.zip)".
    – Sunil J
    Jul 13, 2015 at 14:29
  • Figured out the problem. ctime +7. the files were created within last 7 days.
    – Sunil J
    Jul 14, 2015 at 12:48
0

First date has a format of YYYYMMDD, which is how I name my backups. Second example is week number (%U) with leading zero like you asked. Oops, gotta escape those backquotes.

#!/bin/bash # Do 'man strftime' for more date format options. mydate=`date +"%Y%m%d"` basedir=/home/username/backups zipfile=$basedir/backup-$mydate.zip echo File name is $zipfile # Now get week number with %U. mydate=`date +"%U"` echo Mydate with week number is $mydate zipfile=$basedir/backup-$mydate.zip echo Zipfile is $zipfile

-1

Try this script:

#!/bin/sh
   Case (find) in
     name="true"
     name="(name.zip)"
     find . -name '*.txt' -ctime +7 'name=("name of the file")' -exec zip "archive-$("date+%Y-%U").zip" {} \;
   endl

and it might work fine this way.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .