Home > Web Development > A useful tip for TAR (pattern matching & file globbing)

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/*.ini

NOPE! 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 *.ini

NOPE! 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?

Advertisement
Categories: Web Development
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.