大きな tarball からディレクトリを展開する

大きな tarball からディレクトリを展開する

パスが分からないディレクトリを解凍するにはどうすればいいですか? ディレクトリ名しか分かりません。

ワイルドカードを使用して単一のファイルを解凍する方法を知っています:tar -xf somefile.tar.gz --wildcards --no-anchored 'index.php'

答え1

私は2回試してみます:

$ tar -tf somefile.tar.gz | grep dir-i-am-looking-for | head -1
./foo/bar/dir-i-am-looking-for/somefile/bla/bla/bla
$ tar -xf somefile.tar.gz ./foo/bar/dir-i-am-looking-for

GNU tar に「ワイルドカードを含める」オプションがありません。

答え2

使用方法の1つperl:

の内容スクリプト:

use warnings;
use strict;
use Archive::Tar;

## Check input arguments.
die qq[perl $0 <tar-file> <directory>\n] unless @ARGV == 2;

my $found_dir;

## Create a Tar object.
my $tar = Archive::Tar->new( shift );

## Get directory to search in the Tar object.
my $dir = quotemeta shift;

for ( $tar->get_files ) { 

    ## Set flag and extract when last entry of the path is a directory with same 
    ## name given as argument
    if ( ! $found_dir &&  $_->is_dir && $_->full_path =~ m|(?i:$dir)/\Z|o ) { 
        $found_dir = 1;
        $tar->extract( $_ );
        next;
    }   

    ## When set flag (directory already found previously), extract all files after
    ## it in the path.
    if ( $found_dir && $_->full_path =~ m|/(?i:$dir)/.*|o ) { 
        $tar->extract( $_ );
    }   
}

2 つの引数を受け入れます。最初の引数は TAR ファイル、2 番目の引数は抽出するディレクトリです。次のように実行します。

perl script.pl test.tar winbuild

関連情報