Untar, ungz, gz, tar - how do you remember all the useful options?

76

30

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.

deadprogrammer

Posted 2008-10-21T21:56:42.237

Reputation: 865

1use the manual pages, then you don't have to create a wiki, and only have to remember the man command :-) man(1) – Florenz Kley – 2011-03-25T14:09:07.600

9eXtract Zee File is how I remember it – Canadian Luke – 2013-08-05T20:20:59.580

Create Zee Vocking File might be appropriate for creating a .tar.gz – CousinCocaine – 2013-10-14T09:00:01.300

Answers

65

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

ADEpt

Posted 2008-10-21T21:56:42.237

Reputation: 484

Props for actually answering the question :) – None – 2008-10-21T22:28:56.960

Now, that is a COOL "qwerty" tip! :) – None – 2008-10-22T03:04:56.920

fricking awesome! – deadprogrammer – 2008-10-22T13:14:18.513

2Another 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 – 2009-08-08T14:38:16.947

10I'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 – 2009-08-08T14:52:50.263

i just memorized the easy to remember ditty - "ex vee vee zee eff"! but yeah i never could remember how to make an archive – Claudiu – 2009-08-08T15:58:24.700

One tip is that recent version of tar detect the format automatically on extract so you can just use -x to extract. – Maxim – 2017-02-16T18:44:33.940

41

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/*

Adam Pierce

Posted 2008-10-21T21:56:42.237

Reputation: 113

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

Vicent Marti

Posted 2008-10-21T21:56:42.237

Reputation:

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 – 2015-05-23T09:23:36.953

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
 }

Ali Mezgani

Posted 2008-10-21T21:56:42.237

Reputation: 111

5

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

Jeremy Ruten

Posted 2008-10-21T21:56:42.237

Reputation: 161

12seriously, have you ever tried reading that man page? – deadprogrammer – 2008-10-21T22:05:37.117

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 – 2008-10-21T22:16:45.550

really? how would you deduce tar -xvf from this? – deadprogrammer – 2008-10-21T22:36:37.583

1Great. 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. – None – 2008-10-21T23:42:00.167

Well it seems to work for me (with tar as well as other ocmmand-line linux programs). I just use the '--help' option or 'man' and scan for the information I need, and that's always worked well for me. If there's too much useless information I just skip over it. – Jeremy Ruten – 2008-10-21T23:53:09.693

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. – deadprogrammer – 2008-10-22T13:13:05.303

2I 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 – 2008-10-22T18:59:13.967

1Jeremy, 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 – 2009-08-08T15:03:15.563

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

Logan

Posted 2008-10-21T21:56:42.237

Reputation:

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 – 2008-10-21T23:25:00.047

1whats tabulating? – Claudiu – 2009-08-08T15:59:47.020

3

Man is your friend.

man tar

madlep

Posted 2008-10-21T21:56:42.237

Reputation:

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

Jauder Ho

Posted 2008-10-21T21:56:42.237

Reputation: 151

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.


Lazer

Posted 2008-10-21T21:56:42.237

Reputation: 13 841

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

feeela

Posted 2008-10-21T21:56:42.237

Reputation: 99

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%

porty

Posted 2008-10-21T21:56:42.237

Reputation: 1

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.

ramayac

Posted 2008-10-21T21:56:42.237

Reputation: 111

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 – 2009-07-14T00:37:31.397

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.

Dave Dribin

Posted 2008-10-21T21:56:42.237

Reputation: 171

0

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.

foerno

Posted 2008-10-21T21:56:42.237

Reputation: 181

0

Create Zip to Filename

tar -czf myfile.tar.gz myfile.sql

eXtract Zip with Filename

tar -xzf myfile.tar.gz

littledynamo

Posted 2008-10-21T21:56:42.237

Reputation: 141

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

Espen

Posted 2008-10-21T21:56:42.237

Reputation:

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

mmacaulay

Posted 2008-10-21T21:56:42.237

Reputation: 663