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
By convention, a gzipped file should have .gz as its final suffix; not .tar .
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:
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
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?
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