
Como faço para descompactar um diretório cujo caminho não conheço? Eu só sei o nome do diretório.
Eu sei como descompactar um único arquivo com um curinga:tar -xf somefile.tar.gz --wildcards --no-anchored 'index.php'
Responder1
Eu daria apenas duas tentativas:
$ 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
Não vejo uma opção de "inclusão de curinga" no GNU tar.
Responder2
Uma maneira usando perl
:
Conteúdo descript.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( $_ );
}
}
Ele aceita dois argumentos, o primeiro o arquivo TAR e o segundo o diretório a ser extraído. Execute como:
perl script.pl test.tar winbuild