LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 08-17-2005, 07:30 PM   #1
stefanlasiewski
Member
 
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:

Code:
#!/bin/bash -x

TAR_FILE=$1

if [ -f "$TAR_FILE" ]
then

        # 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
else
        echo "Could not find the file \"$TAR_FILE\""
        exit 1
fi
 
Old 08-17-2005, 08:02 PM   #2
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

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

Rep: Reputation: 96
Hi.

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

Dave

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 08:25 PM.
 
Old 08-17-2005, 08:48 PM   #4
stefanlasiewski
Member
 
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.

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


Reply


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 6 07-22-2009 10:03 AM
Does anyone know how to move files to a particular directory using shell scripting RowanB Programming 3 11-11-2004 01:46 PM
Specifying target directory for command in bash shell script? spectrescape Programming 1 07-22-2004 05:37 PM
Tar files in a dir modified before 7 days in a shell script jayachristina Linux - Newbie 4 05-14-2004 02:49 AM
How do I decompress and desarchive multiple .tar.gz files to a single directory? Baldorg Linux - Software 7 11-27-2003 12:42 PM


All times are GMT -5. The time now is 12:17 PM.

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