View Full Version : Using bash to find the top level directory in a tar.

May 31st, 2010, 10:56 AM
I'm trying to write a script that extracts a tar.gz archive. I know that the archive will contain just a single folder at the top level, and that this folder will contain a directory structure under it. The name of the folder at the top level is not known.

I started by using

files = "$(tar --no-recursive -tf public_html/drupal-6.16.tar.gz)"

This still lists the entire directory structure, even though the --no-recursive option is given.
Finally if I cannot simply find the name of the top level directory, then how can I extract the first line from the output of the above command.

Thanks a lot in advance.


May 31st, 2010, 11:02 AM
tar -tf archive.tar.gz | grep -o '^[^/]\+' | sort -u

May 31st, 2010, 11:06 AM
That works great.
Shall I take it that it isn't possible to simply untar only the top level directory?

May 31st, 2010, 12:54 PM
Following takes a single argument (the tarball) and extracts level 1 files
(Level 1 being, files inside a top directory)


if [ -z "$1" ] || [ ! -f "$1" ]; then
echo "Err: File not found"
exit 1

ONLY_L1_FILES=$(tar -tf "$1" | grep '^[^/]\+/[^/]\+$')

for FILE in $ONLY_L1_FILES; do
tar xzvvf "$1" "$FILE"

May 31st, 2010, 07:27 PM
With pax, a POSIX archiver

pax -rzf archive.tar.gz -c '*/*/*'

May 31st, 2010, 09:24 PM
files = "$(tar --exclude '*/*' -tf public_html/drupal-6.16.tar.gz)"

June 2nd, 2010, 10:49 AM
Thanks guys.
All the above solutions work fine.
I just decided to go with diesch's method as I didn't want to mkae pax a dependancy of the script, and found it more efficient that using grep and then sorting for unique entries.

Thanks a lot.


December 29th, 2011, 11:14 PM
tar --strip-components 1 -xvf latest.tar.gz