从大 tarball 中解压目录

从大 tarball 中解压目录

如何解压一个我不知道路径的目录?我只知道目录名称。

我知道如何使用通配符解压单个文件:tar -xf somefile.tar.gz --wildcards --no-anchored 'index.php'

答案1

我只会通过两次:

$ 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

一种使用方法perl

内容脚本.pl:

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( $_ );
    }   
}

它接受两个参数,第一个是 TAR 文件,第二个是要提取的目录。像这样运行它:

perl script.pl test.tar winbuild

相关内容