-   Programming (
-   -   Shell: Tar files: programmatically determining the name of the target directory (

stefanlasiewski 08-17-2005 07:30 PM

Shell: Tar files: programmatically determining the name of the target directory
Hey Gang,

As part of a larger project, I'm looking for a way to automatically do the following with a ton of different tar.gz files:

1. Determine the target directory of a .tar.gz file
2. Remove the directory, if it exists
3. Untar the new .tar file into the target directory again

Anyone have a good way to determine the target directory of a .tar.gz file?

This might seem easy, but it's complicated by the fact that the target directory and the .tar.gz file often have inconsistant names.

The tar file might expand into the CWD '.', or. In Debian, the .tar.gz is named apache2_2.0.54.orig.tar.gz and the directory is named "apache2-2.0.54"-- with a dash instead of an underscore.

Here is my first attempt:


#!/bin/bash -x


if [ -f "$TAR_FILE" ]

        # Programatically determine installation directory of the tar file
        # and untar it
        # While we are at it, get the name of the directory that
        # the tarfile expands to-- for use later on.
        # Some maintainers use inconsistant naming between
        # the tar file and the expanded directory:w

        # First, determine the directory that Tar will expand to
        TAR_DIR=`tar ztf $TAR_FILE |cut -f1 -d/ |head -1`

        echo "Directory name is $TAR_DIR"

        # Second, remove the directory if it exists
        # TODO: We should have a strategy for when TAR_DIR is '.'
        [[ -d $TAR_DIR ]] && rm -rf $TAR_DIR

        tar zxf $TAR_FILE
        echo "Could not find the file \"$TAR_FILE\""
        exit 1

Matir 08-17-2005 08:02 PM

Have you considered something like:

TAR_DIR=`tar zft $TAR_FILE | head -n1`

ilikejam 08-17-2005 08:21 PM


If the first part of the path is the same for _every_ file in the tar.gz, then you know that will be the directrory which will be created when it is untarred. In any other case, the tar file will unpack to '.'.

You could test for this with:

TARGETDIRNUM=`tar tzf abcdefg.tar.gz | cut -f1 -d/ | uniq | wc | awk '{print $1}'`
If $TARGETDIRNUM = 1 after that, then you know the tar file will unpack to a new (well, single) directory. If $TARGETDIRNUM > 1, then the tar file will unpack to '.'.

Assuming there is a target directory, you can get its name from the code in post #2, with a 'cut' command.


P.S. Watch what you're doing with paths that start with / , and don't be root, for obvious reasons.

stefanlasiewski 08-17-2005 08:48 PM

Thanks ilikejam, that's a good check.

I have a longer version of the code here, where I deal with special exceptions like '.' . Great pointer regarding paths that being with '/' though. This is getting so hairy ...

Here's a slightly modified version of your solution. I removed the awk part, since awk can be problematic on some systems.


TARGETDIRNUM=`tar jtf $TAR_FILE | cut -f1 -d/ | uniq |wc -l`

All times are GMT -5. The time now is 02:52 PM.