1

GNU tar allows to extract the contents of an archive to a directory named after the basename of the archive.

$ touch foobar
$ gnutar czf archive.tar.gz foobar
$ gnutar --one-top-level -xf archive.tar.gz
$ ls archive/foobar
archive/foobar

I can get the same result with BSD tar by running multiple commands.

$ destdir=$(basename archive.tar.gz .tar.gz)
$ mkdir $destdir
$ bsdtar -C $destdir -xf archive.tar.gz
$ ls archive/foobar
archive/foobar

I would however like to run a single command just as with GNU tar. One could of course wrap the commands into shell script or shell function but I'm wondering if there is some builtin of BSD tar that produces the same result thus requiring only a single command?


Background: I would like to use BSD tar because GNU tar currently does not seem to support extended attributes on non-Linux plattforms, at least GNU tar installed via Homebrew on macOS is currently not compiled with extended attributes support.

2 Answers 2

0

You've almost solved your own problem. With some slight parameterization of your code, you could create a function and place it in your .profile or other bash start-up script.

guntar() {

destdir="$(basename "$1" .tar.gz)"
mkdir "$destdir"
bsdtar -C "$destdir" -xf "$1"
ls -l "$destdir"/

}

$ guntar archive.tar.gz 
total 1
-rw-------  1 jim  wheel  0 Jan 11 09:34 foobar
0

With zsh:

() { bsdtar -"s|^|${${1:t}%.(tar*|t?z|xip|iso|7z)}/|' -xf $1; } path/to/some/file.tgz

Which passes the path of the file to an anonymous function inside which we use the -s option to prepend the basename stripped of the suffix for some of the archive formats supported by bsdtar to the extracted paths.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .