대형 타르볼에서 디렉토리 압축 해제

대형 타르볼에서 디렉토리 압축 해제

경로를 모르는 디렉토리를 어떻게 압축 해제합니까? 나는 디렉토리 이름만 알고 있다.

와일드카드를 사용하여 단일 파일의 압축을 푸는 방법을 알고 있습니다.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:

의 내용script.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

관련 정보