Chris Mellor
Chris Mellor

Reputation: 377

Grep Pattern From TAR Output

===UPDATE===

@Cyrus answer was the one that worked, but, this led to another issue that's related that I missed.

There are some folders with folders within folders, so httpdocs/<folder1>/<folder2>/<etc>/index.php and so the '*' is picking them up too.

Really all I want is to match the pattern httpdocs/<folder>/index.php

I'm rubbish with RegEx so unsure of what I could put. Any ideas?


I have a 25GB tar.gz file, and from this I want to extract certain files. The file location is:

httpdocs/<account>/index.php

The <account> just means that it's just a name - not overly important.

There's around 70+ of these index.php files in this exact format, and I need to extract only them files from the TAR.

I can do it per individual file, like so (tar -xf site_support_server.com_user-data_1509221232.tgz httpdocs/hotel/index.php) but as I say their's around 70 of these and I don't want to go through it 1 by 1.

I thought maybe tar -xf site_support_server.com_user-data_1509221232.tgz httpdocs/*/index.php would work, but I don't think it recognizes the '*' as a wildcard.

Am I missing something or does anybody else have any suggestions on how to quickly do this?

Many thanks!

Upvotes: 0

Views: 155

Answers (2)

Cyrus
Cyrus

Reputation: 88766

With GNU tar:

tar -xvzf your_file.tgz --wildcards "*/index.php"

Update

tar -tvzf your_file.tgz --wildcards "httpdocs/*/index.php" --exclude="httpdocs/*/*/index.php"

Upvotes: 1

viraptor
viraptor

Reputation: 34185

You can list multiple archive members in one command. That means you can do:

tar -tf some.tar.gz | grep '/index.php$' | xargs tar -xf some.tar.gz

Watch out for spaces in the paths though - maybe you need to replace newlines with null bytes and use xargs -0

Upvotes: 0

Related Questions