Untar, ungz, gz, tar - how do you remember all the useful options?
Solution 1
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".
Solution 2
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/*
Solution 3
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).
Solution 4
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
}
Solution 5
Just type tar --help
and there's your cheatsheet.
Related videos on Youtube
deadprogrammer
Updated on September 17, 2022Comments
-
deadprogrammer over 1 year
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.
-
Florenz Kley about 13 yearsuse the manual pages, then you don't have to create a wiki, and only have to remember the man command :-) man(1)
-
Canadian Luke over 10 yearseXtract Zee File is how I remember it
-
CousinCocaine over 10 yearsCreate Zee Vocking File might be appropriate for creating a .tar.gz
-
-
deadprogrammer over 15 yearsseriously, have you ever tried reading that man page?
-
Paige Ruten over 15 yearsIt 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'.
-
deadprogrammer over 15 yearsreally? how would you deduce tar -xvf from this?
-
pxl over 15 yearsYou should get in the habit of always providing the
f
option: it's only recently that distributions of tar default tof -
, the historical default isf /dev/rmt0
or similar. After all, tar is the Tape ARchiver. -
Admin over 15 yearsGreat. 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.
-
Paige Ruten over 15 yearsWell 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.
-
Admin over 15 yearsNow, that is a COOL "qwerty" tip! :)
-
deadprogrammer over 15 yearsfricking awesome!
-
Paige Ruten over 15 yearsI 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.
-
Andrew Grimm almost 15 yearsUntil now, I've been using fileroller, but currently I'm out of luck because the version I have uses 1.1 Gb of memory.
-
Nate over 14 yearsAnother tip is that the
z
option (or thej
option for.bz2
files) is not needed when extracting with modern versions of tar. It automatically uncompresses the file. -
Tyler McHenry over 14 yearsI'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
-
Nazadus over 14 yearsJeremy, 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.
-
Claudiu over 14 yearsi just memorized the easy to remember ditty - "ex vee vee zee eff"! but yeah i never could remember how to make an archive
-
Claudiu over 14 yearswhats tabulating?
-
iacnats almost 9 yearsEven after using tar for quite a long time I still need to google for the stupid flags. unp is the way to go.
-
Maxim about 7 yearsOne tip is that recent version of tar detect the format automatically on extract so you can just use -x to extract.