28
Jan

Extract/Uncompress/Unarchive Almost Any File in Linux (tar, tar.gz, tar.bz2, gz, bz, zip, 7z, rar, etc…)

This is a no-frills Linux command line guide/cheat sheet that will help you extract or unarchive or uncompress just about any file that you’re bound to come across. If you’d like to have more options, read the man pages! Also, the opposite to this guide about creating/compressing/archiving files in Linux can be found here.

Preliminary:

Most compressed or archived files that you’ll come across already have built-in support under popular distributions (Ubuntu, Fedora, Suse, etc…), but some of them aren’t.  I would recommend installing this group of packages using apt-get, yum, or an equivalent before using this guide (unless of course you already know what you need):

su -c 'yum install unzip p7zip unrar bzip2 gzip lzma'

.tar

tar xvf filename.tar

.tar.gz

tar xzvf filename.tar.gz

.tgz

tar xvzf filename.tgz

.tar.gz2

You probably mean .tar.bz2

.tar.bz

tar xjvf filename.tar.bz

.tbz

tar xjvf filename.tbz

.tar.bz2

tar xjvf filename.tar.bz2

.tar.Z

zcat file.tar.Z | tar xvf -

.tar.xz

lzcat filename.tar.xz | tar xvf -

.gz

gunzip filename.gz

.gz2

You probably mean .bz2

.bz

bunzip filename.bz

.bz2

bunzip2 filename.bz2

.Z

uncompress filename.Z

.xz

unlzma filename.xz

.zip

unzip filename.zip

.7z

7z x filename.7z

.rar

unrar x filename.rar

.dmg

This isn’t a file that can be ‘extracted’ but you can mount and save the files using:

mkdir /mnt/source
mount -o loop -t hfs filename.dmg /mnt/source
cp /mnt/source/* /home/username/destination/

.img, .dd

These aren’t files that can be ‘extracted’ but you can mount and save the files using:

mkdir /mnt/source
mount -o loop -t iso9660 filename.img /mnt/source
cp /mnt/source/* /home/username/destination/

.more?

Have more to share? I wouldn’t mind hearing about it in the comments if you do!

.trouble?

Let me know about it in the comments. We’ll get it straightened out.

.notes

Apparently with newer versions of tar, you don’t need to specify the j or z options when dealing with .tar.xxx files, but I’m still going to include them here just in case there are users out there using an older version still.

Feel free to donate if this post prevented any headaches! Another way to show your appreciation is to take a gander at these relative ads that you may be interested in:


There's 6 Comments So Far

  • gentooROCKS!
    April 10th, 2010 at 8:22 am

    unrar x is better than unrar e because it preserves the original structure.

  • [email protected]
    April 10th, 2010 at 9:36 am

    Heh, what do you know… Sure enough, x is a better option than e. I appreciate the feedback! Thanks.

  • Totti
    December 13th, 2010 at 11:43 pm

    Well you can make your live much easier by using the atools
    after installation substitute any of the standard commands above by

    aunpack filename.whatever % to unpack
    apack filename.whatever files % to pack
    als filename.whatever % to list

    there is even more check atool manpages

  • [email protected]
    September 28th, 2012 at 9:44 am

    extract () {
    if [ -f $1 ] ; then
    case $1 in
    *.tar.bz2) tar xvjf $1 ;;
    *.tar.gz) tar xvzf $1 ;;
    *.bz2) bunzip2 $1 ;;
    *.rar) unrar x $1 ;;
    *.gz) gunzip $1 ;;
    *.tar) tar xvf $1 ;;
    *.tbz2) tar xvjf $1 ;;
    *.tgz) tar xvzf $1 ;;
    *.zip) unzip $1 ;;
    *.Z) uncompress $1 ;;
    *.7z) 7z x $1 ;;
    *) echo “don’t know how to extract ‘$1’…” ;;
    esac
    else
    echo “‘$1’ is not a valid file!”
    fi
    }

  • NetworkEngineer
    February 3rd, 2014 at 2:34 pm

    [email protected]:/media/Dumps# lzcat tor-browser-linux64-3.5_en-US.tar.xz | tar xvf –
    tor-browser-linux64-3.5_en-US.tar.xz: unknown suffix — unchanged
    tar: This does not look like a tar archive
    tar: Exiting with failure status due to previous errors

  • SJ
    November 10th, 2014 at 4:40 am

    I have one file with extension .img.xz i have to extract it in particular directory can u suggest me a command which can do this?

Share your thoughts, leave a comment!