Extract .tgz to a specific subfolder only if there are files in tar that will be extracted to my CWD

Most tar files are extracted to their own subfolder (because the people who write open source utilities are amazing people).

Some extracts to my CD, which is cluttering everything up. I know there is a way to see what is in tar, but I want to write a bash script that essentially guarantees that I will not exit the 15 files extracted to my home folder.

Any pointers?

pseudo code:

if [listing of tar files] has any file that doesn't have a '/' in it: mkdir [tar filename without extension] tar xzvf [tar filename] into [the new folder] else: tar xzvf [tar filename] into cwd 

EDIT:

Both solutions are great, I chose the solution below because I requested a bash script and it does not rely on additional software.

However, on my own machine, I use aunpack because it can handle many, many other formats.

I use it with a shell script that downloads and unpacks everything at once. Here is what I use:

 #!/bin/bash wget -o temp.log --content-disposition $1 old=IFS IFS=' ' r=`cat temp.log` rm temp.log for line in $r; do substring=$(expr "$line" : 'Saving to: `\(.*\)'\') if [ "$substring" != "" ] then aunpack $substring rm $substring IFS=$old exit fi done IFS=$old 
+4
source share
2 answers

You can use a combination of tar options to achieve this: tar option to list:

  -t, --list list the contents of an archive 

tar option to extract to another directory:

  -C, --directory DIR change to directory DIR 

So, in the script you can list the files and check if there are any files in the list that do not have a "/", and based on this output, you can call tar with the appropriate parameters.
A sample for your reference is as follows:

 TAR_FILE=<some_tar_file_to_be_extracted> # List the files in the .tgz file using tar -tf # Look for all the entries w/o "/" in their names using grep -v # Count the number of such entries using wc -l, if the count is > 0, create directory if [ `tar -tf ${TAR_FILE} |grep -v "/"|wc -l` -gt 0 ];then echo "Found file(s) which is(are) not in any directory" # Directory name will be the tar file name excluding everything after last "." # Thus "test.a.sh.tgz" will give a directory name "test.a.sh" DIR_NAME=${TAR_FILE%.*} echo "Extracting in ${DIR_NAME}" # Test if the directory exists, if not then create it [ -d ${DIR_NAME} ] || mkdir ${DIR_NAME} # Extract to the directory instead of cwd tar xzvf ${TAR_FILE} -C ${DIR_NAME} else # Extract to cwd tar xzvf ${TAR_FILE} fi 

In some cases, the tar file may contain different directories. If you find it a little annoying to look for different directories that are extracted by the same tar file, then the script can be modified to create a new directory, even if the list contains different directories. A slightly advanced sample is as follows:

 TAR_FILE=<some_tar_file_to_be_extracted> # List the files in the .tgz file using tar -tf # Look for only directory names using cut, # Current cut option used lists each files as different entry # Count the number unique directories, if the count is > 1, create directory if [ `tar -tf ${TAR_FILE} |cut -d '/' -f 1|uniq|wc -l` -gt 1 ];then echo "Found file(s) which is(are) not in same directory" # Directory name will be the tar file name excluding everything after last "." # Thus "test.a.sh.tgz" will give a directory name "test.a.sh" DIR_NAME=${TAR_FILE%.*} echo "Extracting in ${DIR_NAME}" # Test if the directory exists, if not then create it # If directory exists prompt user to enter directory to extract to # It can be a new or existing directory if [ -d ${DIR_NAME} ];then echo "${DIR_NAME} exists. Enter (new/existing) directory to extract to" read NEW_DIR_NAME # Test if the user entered directory exists, if not then create it [ -d ${NEW_DIR_NAME} ] || mkdir ${NEW_DIR_NAME} else mkdir ${DIR_NAME} fi # Extract to the directory instead of cwd tar xzvf ${TAR_FILE} -C ${DIR_NAME} else # Extract to cwd tar xzvf ${TAR_FILE} fi 

Hope this helps!

+2
source

The aunpack package aunpack command does the following:

aunpack extracts files from the archive. Often you want to extract all the files in the archive into one subdirectory. However, some archives contain several files in the root directories. The aunpack program overcomes this problem by first extracting the files to a unique (temporary) directory, and then, if possible, move its contents. This also prevents overwriting local files by mistake.

+7
source

All Articles