At the moment, if I download a compressed file, it could be any of a .tar.gz archive, a tar.bz2 arhive, a .zip archive or a .gz archive. And each time I do so, I have to remember what the command line options for that program are.

Is there any CLI program where I can just go:

undocompression somefile.??

and let it figure out what format the archive is in? (overly long name used to avoid conflicting with any real program)

  • I couldn't find or create any tags such as archive, compression etc. Could someone else add the correct one? – Macha Aug 28 '10 at 15:43
  • @Tshepang: Fixed. – Macha Nov 29 '10 at 12:45
up vote 11 down vote accepted

You can use p7zip. It automatically identifies the archive type and decompress it.

p7zip is the command line version of 7-Zip for Unix/Linux, made by an independent developer.

7z e <file_name>

I found this little snippet a while ago and have been using it since. I just have it in my .bashrc file

extract () {
if [ -f $1 ] ; then
    case $1 in
        *.tar.bz2)  tar xjf $1      ;;
        *.tar.gz)   tar xzf $1      ;;
        *.bz2)      bunzip2 $1      ;;
        *.rar)      rar x $1        ;;
        *.gz)       gunzip $1       ;;
        *.tar)      tar xf $1       ;;
        *.tbz2)     tar xjf $1      ;;
        *.tgz)      tar xzf $1      ;;
        *.zip)      unzip $1        ;;
        *.Z)        uncompress $1   ;;
        *)          echo "'$1' cannot be extracted via extract()" ;;
    esac
else
    echo "'$1' is not a valid file"
fi
}
  • 1
    This is more effective than the accepted answer, since 7z e foo.tar.gz just leaves you with a foo.tar file. – Wilfred Hughes May 17 '14 at 20:44

In Debian/Ubuntu there is the unp package, which is a Perl script that acts as a frontend for many archiving utilities.

From another question: atool, which also handles various archive types and is more powerful than unp because it also handles listing of contents, finding differences between archives etc.

GNU tar (and star) has at least some compression auto-detection capabilities:

tar xf foo.tar.gz
tar xf foo.tar.bz

just work.

  • It does depend on the version of tar which formats it can autodetect but other than that it works well... IIRC star is actually a more standardized way, where GNU's tar is a non standard extension. – xenoterracide Aug 28 '10 at 17:05
  • Latest version of GNU tar can uncompress all compressed archives, which are created with any of the compression filter switches (z, j, J, --lzma), it will detect compression automatically. – polemon Nov 29 '10 at 15:58
  • @xenoterracide: Well, the author of star has criticized GNU tar a lot in his usual style - these writings could be biased (ignoring bad points about star and good points about GNU tar), contain probably some FUD and are probably outdated. – maxschlepzig Nov 29 '10 at 17:29

I think ark the KDE archiving tool can be run without a GUI. From the ark manpage

ark --batch archive.tar.bz2

Will extract archive.tar.bz2 into the current directory without showing any GUI.

Arks support of various archive formats depends on which apps you have installed (e.g. for rar it depends on unrar ), but I don't know of any formats it can't handle.

I'm surprised no one mentioned the dtrx tool that was suggested in this answer.

Seems to fit the request to a tee.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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