Compressing Files In Linux – A Quick Guide Of Linux Commands

Decompressing Files

How do I decompress a .tar type file?
tar xf file.tar
How do I decompress a .gz type file?
gunzip file.gz
How do I decompress a .tar.gz type file?
gunzip file.tar.gz
How do I decompress a .bz2 type file?
bunzip2 file.bz2
How do I decompress a .tar.bz2 type file?
bunzip2 file.tar.bz2
How do I decompress a .zip type file?
unzip file.zip
unzip can decompress win32 self-extracting .exe files!!
How do I decompress a .Z type file?
uncompress file.Z
How do I decompress a .7z or .7za type file?
7za x newarchive.rar
How do I decompress a .rar type file?
unrar x newarchive.rar

Compressing Files

How do I create a .tar file of a directory/file?
tar cf file.tar file/
How do I create a .gz file of a certain directory/file?
gzip file
How do I create a .tar.gz file of a certain directory/file?
tar czspf file.tar.gz directory/ or file
How do I create a .bz2 file of a certain directory/file?
bzip2 file
How do I create a .tar.bz2 file of a certain directory/file?
tar cjspf file.tar.bz2 directory/ or file
How do I create a .zip file of a certain directory/file?
zip file (yes, it really is this simple)
How do I create a .Z file of a certain directory/file?
compress file.Z
How do I create a .7z or .7za file of a certain directory/file?
7za a newarchive.7z files*
For full command syntax and options run 7za without any parameters.
Note: 7za can also extract RAR archives.
How do I create a .rar file of a certain directory/file?
rar a newarchive.rar files*
For full command syntax run rar and unrar without any parameters.

Putting Them Together

Various other ways for decompressing a .tar.gz type file.
gunzip file.tar.gz; tar xf file.tar
gunzip < file.tar.gz | tar xf -
tar xzf file.tar.gz (z tells tar to use gunzip)
tar xzf file.tar.gz
tar xzf file.tar.gz (the f option must be last)
tar xzvf file.tar.gz (v lists every file as it untars, it’s “verbose”)
Various other ways for decompressing a .tar.bz2 type file.
bunzip2 file.tar.bz2; tar xf file.tar
bunzip2 < file.tar.bz2 | tar xf -
tar xjf file.tar.bz2 (j tells tar to use bunzip2 (“b” was already taken))
tar xjf file.tar.bz2
tar xjvf file.tar.bz2
different versions of tar may use j, I, or y
How can I move lots of directories and files and preserve them perfectly?
cd /source/directory; tar cf – . | tar xf – -C /destination/directory