How to compress/decompress in Linux

Linux has some tools to compress files and extract archives. You can see some of them listed in the tables below. First table has the decompression commands:

Decompress CommandInformation
gzip -d file.php.gz
gunzip file.php.gz
Extract archives created with the gzip utility.
bzip2 -d file.php.bz2
bunzip2 file.php.bz2
Extract archives created with the bzip2 utility.
unzip archive.zipExtract archives created with the zip utility.
tar -zxvf archivefile.tgz
tar -jxvf archivefile.tbz2
Extract archives created with tar + gzip/bzip2.
-z for gzip decompression ; -j for bzip2 decompression

To see system information for each command, type commandname --help at the terminal, like:

root@web [~]# gzip --help
Usage: gzip [OPTION]... [FILE]...
Compress or uncompress FILEs (by default, compress FILES in-place).

Mandatory arguments to long options are mandatory for short options too.

  -c, --stdout      write on standard output, keep original files unchanged
  -d, --decompress  decompress
  -f, --force       force overwrite of output file and compress links
  -h, --help        give this help
  -l, --list        list compressed file contents
  -L, --license     display software license
  -n, --no-name     do not save or restore the original name and time stamp
  -N, --name        save or restore the original name and time stamp
  -q, --quiet       suppress all warnings
  -r, --recursive   operate recursively on directories
  -S, --suffix=SUF  use suffix SUF on compressed files
  -t, --test        test compressed file integrity
  -v, --verbose     verbose mode
  -V, --version     display version number
  -1, --fast        compress faster
  -9, --best        compress better
    --rsyncable   Make rsync-friendly archive

With no FILE, or when FILE is -, read standard input.

Report bugs to <bug-gzip@gnu.org>.
root@web [~]#

So, for extracting a .gz archive, use the gzip tool:

root@web [/]# gzip -d archive_file.gz

or gunzip

root@web [/]# gunzip archive_file.gz

But how you compress your files? In the table below see some commands for compressing files/directories:

Compress CommandsInformation
gzip file1.htmCompress the file using gzip. 
bzip2 file1.htmCompress the file using bzip2.
zip file1.zip file1.htmCompress the file1.htm into the archive file1.zip.
tar -zcvf files1.tgz file.htm
tar -zcvf files1.tgz *.htm
tar -zcvf files1.tgz /directory/dir1/
tar -jcvf files1.tbz2 file.htm
tar -jcvf files1.tbz2 *.htm
tar -jcvf files1.tbz2 /directory/dir1/
With tar utility you can compress many files.
-z for gzip compression ; -j for bzip2 compression

For example, to compress a full directory(with all subdirectories), use:

root@web [/]# tar -czvf archive.tar.gz /home/temp2/

Another common archive type is RAR. You can download the RAR program from http://rarlabs.com/download.htm (for Linux 32/64 only a command line version is available).
How to install it:

root@web [/temp2]# wget http://rarlabs.com/rar/rarlinux-x64-5.5.b3.tar.gz
--2017-05-26 14:54:47--  http://rarlabs.com/rar/rarlinux-x64-5.5.b3.tar.gz
Resolving rarlabs.com (rarlabs.com)... 5.135.104.98
Connecting to rarlabs.com (rarlabs.com)|5.135.104.98|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 534195 (522K) [application/x-gzip]
Saving to: 'rarlinux-x64-5.5.b3.tar.gz'

100%[========================================================================================================

2017-05-26 14:54:48 (845 KB/s) - 'rarlinux-x64-5.5.b3.tar.gz' saved [534195/534195]
root@web [/temp2]# tar -zxvf rarlinux-x64-5.5.b3.tar.gz
rar/
rar/order.htm
rar/acknow.txt
rar/readme.txt
rar/default.sfx
rar/license.txt
rar/rarfiles.lst
rar/whatsnew.txt
rar/makefile
rar/rar
rar/unrar
rar/rar.txt
root@web [/temp2]# cd rar
root@web [/temp2/rar]# install rar unrar
root@web [/temp2/rar]# rar -?

RAR 5.50 beta 3   Copyright (c) 1993-2017 Alexander Roshal   23 May 2017
Trial version             Type 'rar -?' for help

Usage:     rar <command></command> - -  
               <@listfiles...> <path_to_extract\>

  a             Add files to archive
  c             Add archive comment
  ch            Change archive parameters
  cw            Write archive comment to file
  d             Delete files from archive
  e             Extract files without archived paths
  f             Freshen files in archive
  i[par]=  Find string in archives
  k             Lock archive
  l[t[a],b]     List archive contents [technical[all], bare]
  m[f]          Move to archive [files only]
  p             Print file to stdout
  r             Repair archive
  rc            Reconstruct missing volumes
  rn            Rename archived files
  rr[N]         Add data recovery record
  rv[N]         Create recovery volumes
  s[name|-]     Convert archive to or from SFX
  t             Test archive files
  u             Update files in archive
  v[t[a],b]     Verbosely list archive contents [technical[all],bare]
  x             Extract files with full path
....

This Post Has 2 Comments

  1. sam smith

    RAR file is used to store other files inside,it is a compressed folder if you want to open RAR files then you have to UNRAR it you need to download it from internet using free tool.if you having any kid of issue follow this link.open-rar-file

Leave a Reply