Ubuntu: Zip files into weekly archive with week number



Question:

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)


Solution:1

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  


Solution:2

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


Solution:3

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »