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.
317 Answers
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".
7Tar 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/*
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).
1Really with a frequent usage I make difference between extracting (x) data and compressing (c) data:
To extract:
tar xzf data.tgzTo compress:
tar czf data.tgzFurthermore 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 } Just type tar --help and there's your cheatsheet.
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".
2Man is your friend.
man tar
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.
I always read out the commands in my mind, while typing
compress verbose zip-file
tar cvzf FILEextract verbose zip file FILE
tar xvzf FILE I do the following
To create a tar:
tar czvf foo.tar.gz <files to be included>To untar:
tar zxvf foo.tar.gzThese should be the primary switches you need to worry about:
c - create
z - compress/uncompress
x - extracte
v - verbose
f - fileYou 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 - ) 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.
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/ 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.
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.gzTo un-gzip and untar
$ gunzip -c < out.tar.gz | tar xIf 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% Create Zip to Filename
tar -czf myfile.tar.gz myfile.sqleXtract Zip with Filename
tar -xzf myfile.tar.gz 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:
- x since you want to extract.
- z if the file ends with .gz. (gz is after tar in the file name) This option gunzip the archive.
- f for file followed by the archive name.
This is demonstrated in the example below:
tar xzf file.tar.gz 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 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.