14

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)

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

8 Answers 8

12

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>

6

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
  • 1
    This is more effective than the accepted answer, since 7z e foo.tar.gz just leaves you with a foo.tar file. May 17, 2014 at 20:44
3

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

3

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.

2

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

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

just work.

3
  • 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. Aug 28, 2010 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, 2010 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. Nov 29, 2010 at 17:29
2

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.

2

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

Seems to fit the request to a tee.

1

The Unarchiver supports extraction of about 50 different formats with a consistent interface.

By default, a directory is created if there is more than one top-level file or folder.

View its man-page.

The command line version supports Linux and is available here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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