Tar – How to Make Tar Globbing Work with the ‘Change Directory’ Option

tarwildcards

I have the followin directory structure:

base/
   files/
   archives/
   scripts/

I want a script to run from scripts/, compress files that match results.*.log in files/ into a gzipped tar archive in archives/.

I'm trying the following command:

tar czfC ../archives/archive.tar.gz ../files results.*.log

But I get

tar: results.*.log: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors

While

tar czfC ../archives/archive.tar.gz ../files results.a.log

works as expected. Also

tar czf ../archives/archive.tar.gz ../files/results.*.log

works the way I would like, except it adds the prefix files/ to the file and also emits a warning:

tar: Removing leading `../' from member names

So my conclusion is that tar globbing doesn't work properly when using the -C option. Any advice on how I make this work in a simple manner?

Best Answer

Write it the more portable way:

(cd ../files && tar cf - results.*.log) | 
  gzip -9 > ../archives/archive.tar.gz
Related Question