Results 1 to 8 of 8

Thread: Using bash to find the top level directory in a tar.

  1. #1
    Join Date
    Dec 2009
    Beans
    54

    Using bash to find the top level directory in a tar.

    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

    Code:
    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.

    Vamega

  2. #2
    Join Date
    Sep 2007
    Location
    England
    Beans
    1,103

    Re: Using bash to find the top level directory in a tar.

    Code:
    tar -tf archive.tar.gz | grep -o '^[^/]\+' | sort -u

  3. #3
    Join Date
    Dec 2009
    Beans
    54

    Re: Using bash to find the top level directory in a tar.

    That works great.
    Shall I take it that it isn't possible to simply untar only the top level directory?

  4. #4
    Join Date
    Sep 2007
    Location
    England
    Beans
    1,103

    Re: Using bash to find the top level directory in a tar.

    Following takes a single argument (the tarball) and extracts level 1 files
    (Level 1 being, files inside a top directory)

    Code:
    #!/bin/bash
    
    if [ -z "$1" ] || [ ! -f "$1" ]; then
    	echo "Err: File not found"
    	exit 1
    fi
    
    IFS=$'\n'
    ONLY_L1_FILES=$(tar -tf "$1" | grep '^[^/]\+/[^/]\+$')
    
    for FILE in $ONLY_L1_FILES; do
    	tar xzvvf "$1" "$FILE"
    done

  5. #5
    Join Date
    Feb 2007
    Beans
    4,045
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Using bash to find the top level directory in a tar.

    With pax, a POSIX archiver
    Code:
    pax -rzf archive.tar.gz -c '*/*/*'

  6. #6
    Join Date
    Sep 2009
    Location
    Freiburg/Germany
    Beans
    1,112
    Distro
    Ubuntu 12.10 Quantal Quetzal

    Re: Using bash to find the top level directory in a tar.

    Code:
    files = "$(tar --exclude '*/*' -tf public_html/drupal-6.16.tar.gz)"
    ClassicMenu Indicator - classic GNOME menu for Unity
    Unsettings - configuration program for the Unity
    Privacy Indicator - easily switch privacy settings in Unity
    Arronax - create and modify app starters

  7. #7
    Join Date
    Dec 2009
    Beans
    54

    Re: Using bash to find the top level directory in a tar.

    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.

    Vamega

  8. #8
    Join Date
    Jan 2008
    Location
    San Antonio, TX, USA
    Beans
    21

    Re: Using bash to find the top level directory in a tar.

    tar --strip-components 1 -xvf latest.tar.gz
    Registered Linux User # 290240

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •