
¿Cómo descomprimo un directorio cuya ruta desconozco? Sólo sé el nombre del directorio.
Sé cómo descomprimir un solo archivo con un comodín:tar -xf somefile.tar.gz --wildcards --no-anchored 'index.php'
Respuesta1
Solo le daría dos pases:
$ 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
No veo una opción de "inclusión de comodines" en GNU tar.
Respuesta2
Una forma usando perl
:
Contenido 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( $_ );
}
}
Acepta dos argumentos, el primero el archivo TAR y el segundo el directorio a extraer. Ejecútelo como:
perl script.pl test.tar winbuild