Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 08-17-2005, 08:30 PM   #1
Registered: Aug 2003
Location: Berkeley, California, USA
Distribution: Red Hat Enterprise Linux, Debian & Ubuntu
Posts: 92

Rep: Reputation: 16
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
Old 08-17-2005, 09:02 PM   #2
LQ Guru
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 124Reputation: 124
Have you considered something like:
TAR_DIR=`tar zft $TAR_FILE | head -n1`
Old 08-17-2005, 09:21 PM   #3
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96

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.

Last edited by ilikejam; 08-17-2005 at 09:25 PM.
Old 08-17-2005, 09:48 PM   #4
Registered: Aug 2003
Location: Berkeley, California, USA
Distribution: Red Hat Enterprise Linux, Debian & Ubuntu
Posts: 92

Original Poster
Rep: Reputation: 16
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`


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to untar all tar-files in a directory? zoomzoom Linux - Newbie 13 05-09-2017 12:52 PM
Does anyone know how to move files to a particular directory using shell scripting RowanB Programming 3 11-11-2004 02:46 PM
Specifying target directory for command in bash shell script? spectrescape Programming 1 07-22-2004 06:37 PM
Tar files in a dir modified before 7 days in a shell script jayachristina Linux - Newbie 4 05-14-2004 03:49 AM
How do I decompress and desarchive multiple .tar.gz files to a single directory? Baldorg Linux - Software 7 11-27-2003 01:42 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:15 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration