Sign up ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is there any UNIX shell command to expand a compressed file which type is not known in advance?

I should be able to use this command to expand at least any of the following types of files, though the ability to work with any other known types (like .rar or .7z) would be welcomed:

  • tar
  • tar.gz
  • tgz
  • gz
  • gzip
  • bz2
  • zip

Simpler ideas involving one-liner commands would be the best solutions, but any comment is appreciated.

share|improve this question

migrated from Dec 10 '12 at 3:04

This question came from our site for professional and enthusiast programmers.

how do you want it to decompress? into the same folder? into a new folder with the name of the compressed file? –  Trevor Rudolph Dec 10 '12 at 1:40
Just getting the files out into the same folder would be right, but it's not important really –  NPalopoli Dec 10 '12 at 23:22

1 Answer 1

It's not all that hard to write such a script, but you need to be a bit careful. The problem is that zip files are like compressed tar files and unzipping them extracts the files. Not all files compressed with gzip, bzip2 or xz are necessarily tar files underneath (of course, a .tar.bz2 file is; so is a .tgz file). So you have to define what you want done with a plain .gz file, and whether you always want a tar-file untarred.

Given that preamble, you can write a script like:


# Optionally override the default commands
: ${GZIP:=gzip}
: ${TAR:=tar}
: ${BZIP2:=bzip2}
: ${XZ:=xz}
: ${UNZIP:=unzip}

for file in "$@"
    case "$file" in
        $TAR -xf "$file";;
        $XZ -cd "$file" | $TAR -xf -;;
        $XZ -d "$file";;
        $GZIP -d "$file";;
        $TAR -xf "$file";;
        $BZIP2 -d "$file";;
        $UNZIP "$file";;
    (*) echo "$0: unrecognized file type $file" >&2;;

There's not much rocket science in there. I'm assuming that your version of tar recognizes gzip and bzip2 files automatically; if not, then you need to do more or less as I did for xz. There is also a --use-compress-program=XXX option to GNU tar, but not all variants of tar recognize it. Clearly, you can add more patterns and (de)compressors as you see fit. Recognizing CPIO and shar files wouldn't be all that hard. The list goes on.

Most people don't bother with such a script. For example, I usually check the contents of the file before actually extracting it. You could add argument handling to the script to allow for verbose output, or list instead of extract (-t for tar; -l for unzip) and perhaps verbosity.

One step harder would be identifying the file type when the suffix is not recognized. You might run file on the file and analyze what it says. Another possibility is to analyze whether the contents of the file will be placed in a sub-directory or not; if not, you might create a sub-directory and then extract the contents into the sub-directory. The opportunities are legion.

share|improve this answer
I was aware of an approach like this and expected a simpler, shorter solution would be possible, so I will leave the question unanswered now. However given the limited information I provided, your solution is a fair one and gives plenty of options for extending to other file types. I appreciate that you took the time to call the atention to other related aspects. –  NPalopoli Dec 11 '12 at 0:44

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.