A useful tip for TAR (pattern matching & file globbing)
I’ll admit it. I’m a little lazy with this tip sharing effort. No excuses.
Anyhow, the other day I came across a situation when trying to compact a group of files. At first glance seemed really simple but proved to be a little more challenging.
Problem:
I wanted to tar all the .ini files from a certain folder. From anywhere. Without full paths.
tar -jcvf backup.tar.bz2 full_path/*.iniNOPE! Full paths!
Lets try the -C option to switch to the relevant path before gobbling so that we only get the files without the path.
tar -jcvf backup.tar.bz2 -C full_path *.iniNOPE! No files found!
Well, it seems that when you’re using -C, tar expects a list of files. And while pattern matching should be possible you’re not actually in the right path when trying to get the list of files. Get it? Tar is there expecting files from that path but the CLI is not.
Solution:
So after few too many tries to find a simple solution I arrived at this:
tar -jcvf backup.tar.bz2 -C full_path `(cd full_path; ls *.ini)`
Not so pretty but this way you might even apply other gobbling commands like find to better suit your needs. I bet sysadmins were already in on this issue but developers trying to code backup or conversion utilities might find it useful. Well, did you… punk?