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:
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\""