Ignore directory structure with tar.

18 02 2010

Hey all,

Extremely quick one today, but one I certainly found very useful. In some of my backup tarballs, I’ve ended up with some seriously deeply nested files, and extracting a single file into a folder, only to have it appear inside an otherwise completely empty 15 folder deep directory tree is pointless, and to be honest, slightly annoying. I found a snippet today which allows you to ignore a specified number of directories when extracting files…

tar --strip-components {N} -xvJf tarball.tar.xz {filename}

All you need to do is swap {N} for the number of folders to ignore, {filename} for the name of the file you wish to extract, and enter the tarball name instead of “tarball.tar.xz”. Here’s an example…

tar -C ~ --strip components 2 -xvJf Backups.tar.xz shares/public/code/example.php

This will extract the file “example.php” from the tarball “Backups.tar.xz”, and put it in your home folder in a subdirectory called “code”. To extract it to a folder called public/code, you would change the number to 1, and to put it directly in your home folder, change it to 3.

Right, off to do some work! Hope this has helped you out!!

Advertisements

Actions

Information

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 )

Google+ photo

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

Connecting to %s




%d bloggers like this: