
Wie entpacke ich ein Verzeichnis, dessen Pfad ich nicht kenne? Ich kenne nur den Verzeichnisnamen.
Ich weiß, wie man eine einzelne Datei mit einem Platzhalter entpackt:tar -xf somefile.tar.gz --wildcards --no-anchored 'index.php'
Antwort1
Ich würde es einfach in zwei Schritten versuchen:
$ 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
Ich sehe in GNU Tar keine Option zum „Wildcard-Include“.
Antwort2
Eine Möglichkeit mit perl
:
Inhalt vonSkript.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( $_ );
}
}
Es akzeptiert zwei Argumente, das erste ist die TAR-Datei und das zweite das zu extrahierende Verzeichnis. Führen Sie es wie folgt aus:
perl script.pl test.tar winbuild