Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6630371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:23:38+00:00 2026-05-25T22:23:38+00:00

Most tar files extract into their own subfolder (because the people that write open

  • 0

Most tar files extract into their own subfolder (because the people that write open source utilities are amazing people).

Some extract into my cwd, which clutters everything up. I know there’s a way to see what’s in the tar, but I want to write a bash script that essentially guarantees I won’t end up with 15 files extracted into 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 below solution because I was asking for a bash script, and it doesn’t rely on extra software.

However, on my own machine, I am using aunpack because it can handle many, many more formats.

I am using it with a shell script that downloads and unpacks all at once. Here is what I am using:

#!/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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:23:39+00:00Added an answer on May 25, 2026 at 10:23 pm

    You can use combination of tar options to achieve this:
    tar option for listing is:

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

    tar option to extract into different directory is:

       -C, --directory DIR
              change to directory DIR
    

    So in your script you can list the files & check if there are any files in the listing which do not have “/” and based on that output you can call tar with appropriate options.
    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 which are extracted by the same tar file then the script can be modified to create a new directory even if the listing contains different directories. The 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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a bunch of tar and tar.gz files that I would like to
I wrote a script that collects all our log files to one tar file.
It is my understanding that a .tar file contains directories and other files, and
Most of my C/C++ development involves monolithic module files and absolutely no classes whatsoever,
Most mature C++ projects seem to have an own reflection and attribute system ,
Most of my users have email addresses associated with their profile in /etc/passwd .
Most people say never throw an exception out of a destructor - doing so
Most of our Eclipse projects have multiple source folders, for example: src/main/java src/test/java When
I'm trying to install Python from source on my Mac. (OS X 10.6.2, Python-2.6.5.tar.bz2)
Ok, just wondering on the versions of PHP that this class is built into.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.