Tar archives monthly

Hi,

I want to archive files by month,
is there anyway of this code looks better?

find /tmp/w/ -type f   -newermt '2014-01-01' ! -newermt '2014-02-01' | xargs tar -czvf files01.tar 
find /tmp/w/ -type f   -newermt '2014-02-01' ! -newermt '2014-03-01' | xargs tar -czvf files02.tar 
find /tmp/w/ -type f   -newermt '2014-03-01' ! -newermt '2014-04-01' | xargs tar -czvf files03.tar 

There are a couple of strange things with the command line:

find /tmp/w/ -type f   -newermt '2014-03-01' ! -newermt '2014-04-01' | xargs tar -czvf files03.tar
  1. By convention, a gzipped file should have .gz as its final suffix; not .tar .
  2. The reason for using xargs command (or find ... -exec command + ) is to run the given command as many times as are needed to meet ARG_MAX limitations. But, if you invoke tar multiple times with the -c option, your output file will be recreated from scratch on each invocation (wiping out the data stored in the previous invocation). You could get around that problem if you remove your output file before you start and use tar -r instead of tar -c , but on many systems you can't use the -z option with -r .

If you use pax instead of tar to create the archive, you can feed it the names of files to be processed through its standard input instead of worrying about command line argument length limits. So you might want to try something more like:

find /tmp/w/ -type f -newermt '2014-03-01' ! -newermt '2014-04-01' | pax -w | gzip > files03.tar.gz
1 Like
find /tmp/w/ -type f -newermt '2014-01-01' ! -newermt '2014-02-01' -print0 | tar czvf file01.tar.gz --null -T -

thanks for yours input , i will take that into consideration, but about the main question,
how can i make this code in better way , so i do not have to right it multiple times?
i try to do it with for cycles but as i have -newermt 2 times in same iteration it makes it harder

find /tmp/w/ -type f -newermt '2014-01-01' ! -newermt '2014-02-01' | pax -w | gzip > files01.tar.gz
find /tmp/w/ -type f -newermt '2014-02-01' ! -newermt '2014-03-01' | pax -w | gzip > files02.tar.gz
find /tmp/w/ -type f -newermt '2014-03-01' ! -newermt '2014-04-01' | pax -w | gzip > files03.tar.gz
year=$(date +%Y)
for month in 01 02 03
do
 if [ $month -ne 12 ]
 then
  endm=$(printf "%02d" $((month+1)))
  endy=$year
 else
  endm=01
  endy=$((year+1))
 fi
 find /tmp/w/ -type f -newermt "$year-$month-01" ! -newermt "$endy-$endm-01" | pax -w | gzip > "files$month.tar.gz"
done

thanks for u answers, but i just wonder how can i save file01.tar.gz in the folder where it find archives and not on local folder ?

coz this function save archive on directory where i run the script

find /tmp/w/ -type f -newermt '2014-01-01' ! -newermt '2014-02-01' -print0 | tar czvf file01.tar.gz --null -T -

Simply give your output file a full path:-

find /tmp/w/ -type f -newermt '2014-01-01' ! -newermt '2014-02-01' -print0 | tar czvf /full/path/to/file01.tar.gz --null -T -

Is this what you need?

Robin

Indeed, but there is way to do it automatically, without have to right the /full/path/to/

I want that he tar the file in the directory where he finds the file

That depends on the file/directory structure that you have. Are there one or many directories? Are there one or many matching files in each directory? Should one or all matching files be tar red together, within or across directories?

i have few directories

under /tmp/w/a 
files  test  2014 01 05
         test1 2014 01 08
         test3 2014 02 09
under /tmp/w/b
files  test6  2014 01 05
         test7 2014 02 08
         test8 2014 02 10

if i run

find /tmp/w/ -type f -newermt '2014-01-01' ! -newermt '2014-02-01' -print0 | tar czvf file01.tar.gz --null -T -

in /tmp , it will compress the files under /tmp/
but i pretend that files would be compress in their in directory that they are by month ,
this is the only detail that is missing