85

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.

3
  • 1
    use the manual pages, then you don't have to create a wiki, and only have to remember the man command :-) man(1) Mar 25, 2011 at 14:09
  • 9
    eXtract Zee File is how I remember it Aug 5, 2013 at 20:20
  • Create Zee Vocking File might be appropriate for creating a .tar.gz Oct 14, 2013 at 9:00

17 Answers 17

69

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".

7
  • Props for actually answering the question :)
    – Rich
    Oct 21, 2008 at 22:28
  • Now, that is a COOL "qwerty" tip! :)
    – ramayac
    Oct 22, 2008 at 3:04
  • fricking awesome! Oct 22, 2008 at 13:14
  • 2
    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, 2009 at 14:38
  • 12
    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 Aug 8, 2009 at 14:52
45

Tar option summary

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

  • x - extract files
  • c - create archive
  • 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/*

23

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).

1
  • Even after using tar for quite a long time I still need to google for the stupid flags. unp is the way to go.
    – Yada
    May 23, 2015 at 9:23
8

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
 }
5

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

8
  • 15
    seriously, have you ever tried reading that man page? Oct 21, 2008 at 22:05
  • 3
    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, 2008 at 23:42
  • 1
    Jeremy, you are either an autistic savant or completely full of crap. Man output of tar is worse than useseless, grepping it is useless as well. Oct 22, 2008 at 13:13
  • 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. Oct 22, 2008 at 18:59
  • 2
    Jeremy, the real problem is you may not always know the proper vocabulary. As such, it's easy to get confused and find similar options and have them not work. Then, you have to place (some) of them in proper order. This is non-trivial for someone migrating away from Windows. Even more-so if they didn't know scripting in Windows.
    – Nazadus
    Aug 8, 2009 at 15:03
3

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".

2
  • 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, 2008 at 23:25
  • 1
    whats tabulating?
    – Claudiu
    Aug 8, 2009 at 15:59
3

Man is your friend.

man tar

2

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.


2

I always read out the commands in my mind, while typing

compress verbose zip-file

tar cvzf FILE

extract verbose zip file FILE

tar xvzf FILE
2

I do the following

To create a tar:

tar czvf foo.tar.gz <files to be included>

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 - <files to be copied> | (cd <target directory>; tar xvf - )
1

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.

1
  • 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. Jul 14, 2009 at 0:37
1

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.

1

you can also create an alias. just add these two lines to your ~/.bash_aliases file:

alias untar="tar xvf"
alias dotar="tar cvf"

you can then use untar foo.tar.gz to extract and dotar foo.bar to compress. you can use other aliases that are easy for you to remember.

1

All you need is tar x (extract from stdin), tar c (tar to stdout), gzip -c (gzip from stdin to stdout; -1 to -9 for compression levels; same goes for bzip2) and gunzip -c (gunzip from stdin to stdout) - everything else is pipes.

To tar and gzip:

$ tar c * | gzip -c > out.tar.gz

To un-gzip and untar

$ gunzip -c < out.tar.gz | tar x

If you want sweet progress bars, use pv which is similar to cat but shows progress bars:

$ tar c * | bzip2 -c | pv > out.tar.gz
 22.6MiB 0:00:03 [3.64MiB/s] [  <=>                           ]
$ pv in.tar.gz | bunzip2 -c | tar x
   80MiB 0:00:00 [  58MiB/s] [===============================>] 100%
1

Create Zip to Filename

tar -czf myfile.tar.gz myfile.sql

eXtract Zip with Filename

tar -xzf myfile.tar.gz
0

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 extract.
  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
0

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

  • x for eXtract 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. gunzip or bunzip2.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .