I'm using bash on Windows (distributed along with msysgit) to zip log files - one archive per date. Problem is, following script does not work and I have no idea why. Cut is here to get part of the filename that corresponds to a date the log was written.

find . -type f -name '*.log' | cut -c3-12 | head -n 5 | xargs -I common printf "%s%s\n" common "*.log" | xargs -I fg tar cvzf hello.tgz fg

Before last step, each line looks like this - 2014-07-04*.log
At the last step it gives me output like this:

tar: 2014-07-01*.log: Cannot stat: No such file or directory

I suspect it's because of a newline at the end, because when I call tar command by hand it works fine. Any ideas what I missed? I read some of the similar answers, but they don't seem to apply in my case. Archive name is for example, later I'd make it more descriptive.


The problem is this part:

xargs -I common printf "%s%s\n" common "*.log"

The *.log is inserted in the template as is. I don't understand why you put the "*" there at all. This would fix that:

xargs -I common printf "%s%s\n" common .log

However, the last tar command won't actually work, because the tar cvzf hello.tgz fg command will be executed for each file, and therefore you'll end up with a tar with a single file in it (the last one).

I believe this should be closer to what you want:

find . -type f -name '*.log' | cut -c3-12 | uniq | xargs -I{} sh -c 'tar zcvf {}.tgz {}*.log'

If you have files like:


Then the command will put the first 2 into 2014-07-01.tgz and the last 3 into 2014-07-02.tgz.

  • no, I intentionally put * there. I want all files according to this pattern to be in one .tgz archive - because there are files like 2014-07-01.0.log, 2014-07-01.1.log and so on – chester89 Dec 27 '14 at 15:38
  • I would not recommend parsing ls output that way. It will break when files have whitespace in their name, and various other problems might occur. mywiki.wooledge.org/ParsingLs – slhck Dec 27 '14 at 15:50
  • @slhck I'm aware of that, but I don't have that many files, and I'm sure there are no spaces in filenames – chester89 Dec 27 '14 at 15:53
  • I'm aware of a problem with archive name. But if I get rid of the star, tar won't group several files of the same date, will it? – chester89 Dec 27 '14 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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