up vote 20 down vote favorite
4
share [g+] share [fb]

I am pretty sure I am not the only one with the following problem: every time I need to uncompress a file in *nix I can't remember all the switches, and end up googling it, which is surprizing considering how often I need to do this.

Do you have a good compression cheat sheet? Or how about a mnemonic for all those nasty switches in tar?

I am making this article a wiki so that we can create a nice cheat sheet here.

Oh, and about man pages: is there's one thing they are not helpful for, it's for figuring out how to uncompress a file.

link|improve this question
If you want to make a question Community Wiki, simply check the box on the bottom right corner of the question editor – Ivo Flipse Jun 25 '10 at 6:25
use the manual pages, then you don't have to create a wiki, and only have to remember the man command :-) man(1) – Florenz Kley Mar 25 '11 at 14:09
feedback

migrated from stackoverflow.com Jun 24 '10 at 13:11

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

17 Answers

up vote 28 down vote accepted

Or how about using the shell with advanced completion capabilities (like zsh or fresh versions of bash) which will complete the options for you, with comprehensive help? :))

Regarding tar: just look at the "qwerty" keyboard. There are letters "zxcvf" next to each other. You need either "tar czvf file.tar.gz files" or "tar xzvf file.tar.gz".

link|improve this answer
Props for actually answering the question :) – Rich Oct 21 '08 at 22:28
Now, that is a COOL "qwerty" tip! :) – ramayac Oct 22 '08 at 3:04
fricking awesome! – deadprogrammer Oct 22 '08 at 13:14
Another tip is that the z option (or the j option for .bz2 files) is not needed when extracting with modern versions of tar. It automatically uncompresses the file. – Nate Aug 8 '09 at 14:38
3  
I've used tar about ten billion times and never noticed that all the most commonly used options are right next to each other on the keyboard. headsmack – Tyler McHenry Aug 8 '09 at 14:52
show 1 more comment
feedback

There's a small Perl script called "unp".

unp filename.tar.gz

...and it extracts everything. Works with any compressed file as long as you have the right binaries. And you just forget about syntax or any of that crap. Check your Linux distribution's repositories. It should be there (at least on Arch, Debian and Ubuntu).

link|improve this answer
Cool, that is new to me. Thanks for the tip! – ramayac Oct 21 '08 at 22:33
feedback

Just type tar --help and there's your cheatsheet.

link|improve this answer
2  
seriously, have you ever tried reading that man page? – deadprogrammer Oct 21 '08 at 22:05
It might be a bit unreadable and long but I think it works well for just looking up the name of an option. You can also grep for the option you're looking for, i.e. 'tar --help | grep extract'. – Jeremy Ruten Oct 21 '08 at 22:16
really? how would you deduce tar -xvf from this? – deadprogrammer Oct 21 '08 at 22:36
1  
Great. And then you have to wade through a man-page that (among the stuff you are looking for) tells you tons of archaic streamer commands and options noone except the developer needs. – Nils Pipenbrinck Oct 21 '08 at 23:42
2  
I really don't see what's so hard about it. To extract an archive, just find the extract option and add -x to your command. If you want it verbose, find that option and add a v to get -xv. Then continue until you get the exact command you want. It even has example commands for common operations. – Jeremy Ruten Oct 22 '08 at 18:59
show 3 more comments
feedback

Tar option summary

Y'all are welcome to edit this to add more esoteric switches but here are the basics:

  • x - extract files
  • c - compress files
  • t - list files
  • v - verbose (list files as it processes them)
  • j - use bz2 compression
  • z - use gz compression
  • f - read or write files to disk

Examples

Uncompress a tar.gz file: tar zxf tarball.tar.gz

Uncompress a tar.bz2 file: tar jxf tarball.tar.bz2

Create a tar.gz file: tar zcvf tarvall.tar.gz mydir/*

link|improve this answer
feedback

If you have trouble remembering the tar options, try using pipes:

  • zcat file.tar.gz | tar xvf -
  • bzcat file.tar.bz2 | tar xvf -

Replace tar xv with tar tv to just view the tarball's contents.

Personally, I use the following mnemonics:

  • t, x, or c for "tabulating", "xtracting", or "creating", respectively.
  • v for listing all the files.
  • z, j, or nothing for tar.gz, tar.bz2, or plain .tar, respectively.
  • f for giving a filename, vs. the default of using stdin/stdout or a tape device.

Although "j" and "bzip2" seem to have nothing to do with each other, I find it easy to remember this exception.

Also, I find it funny that Googling has replaced "man tar".

link|improve this answer
You should get in the habit of always providing the f option: it's only recently that distributions of tar default to f -, the historical default is f /dev/rmt0 or similar. After all, tar is the Tape ARchiver. – ephemient Oct 21 '08 at 23:25
1  
whats tabulating? – Claudiu Aug 8 '09 at 15:59
feedback

I do the following

To create a tar: tar czvf foo.tar.gz

To untar tar zxvf foo.tar.gz

These should be the primary switches you need to worry about.

c - create z - compress/uncompress x - extracte v - verbose f - file

You can do some fancier stuff like tar and untar inline while you are trying to move files across directories like so

tar cf - | (cd ; tar xvf - )

HTH

link|improve this answer
feedback

Man is your friend.

man tar

link|improve this answer
feedback

i haven't used nix in 5 years and i remember tar zxvf. i'm not really sure how you don't have it burned into your head after doing it the first 50 times.

link|improve this answer
Same. The muscle memory is so strong on that command I don't even think about it. – mmacaulay Aug 8 '09 at 15:20
feedback

90% of the time I just use

tar -xvf file.*

x: extract
v: be verbose (optional)
f: input file (tar, gz, bzip2, etc...)

Easy, huh? :)
Use Fileroller, or Ark if your not in a console.

link|improve this answer
Until now, I've been using fileroller, but currently I'm out of luck because the version I have uses 1.1 Gb of memory. – Andrew Grimm Jul 14 '09 at 0:37
feedback

I've been using a Perl script called aunpack, part of the atool project, for many years now. So you just run: aunpack foo.{zip/tar.gz/tar.bz2} and it does the correct thing based on the file extension.

The other benefit of aunpack is that it won't pollute the current directory with lots of files. Say there are 20 files in foo.zip that are not in a subdirectory. Instead of spewing these files in the current directory, aunpack will create a foo subdirectory and put all files in there.

link|improve this answer
feedback

Really with a frequent usage I make difference between extracting (x) data and compressing (c) data:

To extract:

tar xzf data.tgz

To compress:

tar czf data.tgz

Furthermore you can add two functions too your .bashrc :

function extract () {     
        if ($# -ne 1); then
                echo "Usage: $0  `<compressed archive>"`
                exit 1
        fi
        tar xzf $1
}

function compress () {
        if ($# -ne 2); then
                echo "Usage: $0 `<compressed archive> <files|directories>"`
                exit 1
        fi
        tar czf $1 $2
}

There is another nice extract function, it detect the extension of your compressed file and do the job for you:

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
 }
link|improve this answer
feedback

I do not remember :). I just use "tar xf file" to compress, and use FileRoller to create packages.

link|improve this answer
tar xf extracts, and neither compresses nor decompresses... so it's probably best you stick to FileRoller! – Rich Oct 21 '08 at 22:29
tar xf on a compressed file (file.tar.bz2 for example) extracts and decompress it... – Kknd Oct 22 '08 at 21:30
feedback

It's easier than cpio used to be.

link|improve this answer
I actually find cpio's option set easier to remember than tar's. -i for in, -o for out, -p for passthrough. – ephemient Oct 21 '08 at 23:26
But you also have to know all the find and exec args to get it to generate the file list – Martin Beckett Oct 22 '08 at 13:16
feedback

I think it´s easy to think of what you want and not just some letters.

To illustrate, I will use an example of how to extract file.tar.gz:

Since it´s a tar file, you should use the the tar program. Then it´s the options:

  1. x since you want to e**x**tract.
  2. z if the file ends with .gz. (gz is after tar in the file name) This option gunzip the archive.
  3. f for file followed by the archive name.

This is demonstrated in the example below:

tar xzf file.tar.gz
link|improve this answer
feedback

The way I remember is that it's always "tar XXvf ", where the two Xs are

  • x for e**X**tract or c for Create and,
  • either z = gzip, j = bzip2 (gzip is more common so it's usually z, for whatever reason the j = bzip2 is just stuck in my head).

Occasionally you run into compressed but not tarred files where you add un to whatever the compression type is. e.g. g**un**zip or b**un**zip2.

If anything, I forget the 2 and sometimes type "bzip".

link|improve this answer
feedback

I agree, man pages can sometimes be hard to read.

Have you tried a GUI application for viewing man pages? When I can't understand/read a man page I use one, its a lot easier to understand/read than text on a console ;D.

link|improve this answer
feedback

The Three Most Frequently Used Operations:

--create -c

Create a new tar archive.

--list -t

List the contents of an archive.

--extract -x

Extract one or more members from an archive.


The Five Advanced tar Operations:

--append -r

Add new entries to an archive that already exists.

--update -u

Add more recent copies of archive members to the end of an archive, if they exist.

--concatenate --catenate -A

Add one or more pre-existing archives to the end of another archive.

--delete

Delete items from an archive (does not work on tapes).

--compare --diff -d

Compare archive members to their counterparts in the file system.


link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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