Home > Unix > UNIX – Lesson 007 – tar, gzip/gunzip and compress/uncompress commands

UNIX – Lesson 007 – tar, gzip/gunzip and compress/uncompress commands


tar
The “tar” command archives files
Syntax :
tar key [f device_file] [filename …]

Key argument :

  • c  a new archive is created
  • x  files are extracted from the archive
  • t   a table of contents of the archive is printed
  • r   files are added to the end of the archive
  • f   the file argument is used as the tarfile and /etc/default/tar is not searched
  • v  a verbose option, print the name of each file or archive member as it is processed

Example:
$ tar cvf Report.tar Report.log Report.log.bck
a Report.log 157K
a Report.log.link 157K

$ tar rvf Report.tar Report.log.bck2
a Report.log.link2 157K

$ tar -tvf Report.tar
-rw-r—– 30851/32 160498 Feb 21 11:42 2008 Report.log
-rw-r—– 30851/32 160498 Feb 21 11:42 2008 Report.log.link
-rw-r—– 30851/32 160498 Feb 21 11:43 2008 Report.log.link2

$ tar xvf Report.tar Report.log
# You extract only the Report.log file from archive

================================================================================
gzip/gunzip
The “gzip” command compresses/expands a file that is replaced by new one with the extension .gz, while keeping the same ownership modes, access and modification times.
Syntax :
gzip [-dlr] filename[.gz]
gunzip [-lr] filename.gz

Key argument :

  • -d   Decompress mode (like gunzip)
  • -f    Force compression or decompression even if the file already exists.
  • -l    List the following fields: compressed size, uncompressed size, ratio, uncompressed_name
  • -r   Recursive. If the file specified is a directory, gzip will descend into the directory and compress all the files it finds there (or decompress them in the case of gunzip)
  • -# -1 (or –fast) indicates the fastest compression method (less compression) and -9 (or –best) indicates the slowest compression method (best compression). The default compression level is -6.

Example:
$ gzip Report.tar
$ ls –al
total 80
drwxr—– 2   xarabas admins 96        Feb 21 12:03 .
drwxr—– 14 xarabas admins 8192    Feb 21 12:03 ..
-rw-r—–  1   xarabas admins 26346  Feb 21 12:03 Report.tar.gz

$gunzip Report.tar.gz
total 80
drwxr—– 2   xarabas admins 96        Feb 21 12:03 .
drwxr—– 14 xarabas admins 8192    Feb 21 12:03 ..
-rw-r—–  1   xarabas admins 32156  Feb 21 12:03 Report.tar

$ gzip -l Report.tar.gz
compressed uncompressed ratio uncompressed_name
26346 484864 94.6% Report.tar

$ gzip -9 Report.tar
$ls -al
total 80
drwxr—– 2   xarabas admins 96        Feb 21 12:03 .
drwxr—– 14 xarabas admins 8192    Feb 21 12:03 ..
-rw-r—–  1   xarabas admins 26275  Feb 21 12:03 Report.tar.gz

================================================================================
compress/uncompress
The “compress/uncompress” command compresses/expands a file, that is replaced by new one with the extension .Z, while keeping the same ownership modes, access and modification times.
Syntax :
compress filename
uncompress filename.Z

Example:
$ compress Report.log
$ ls -al

total 80
drwxr—– 2   xarabas admins 96        Feb 21 12:03 .
drwxr—– 14 xarabas admins 8192    Feb 21 12:03 ..
-rw-r—–  1   xarabas admins 28209  Feb 21 12:03 Report.log.Z

$ uncompress Report.log.Z
$ ls -al

total 80
drwxr—– 2   xarabas admins 96        Feb 21 12:03 .
drwxr—– 14 xarabas admins 8192    Feb 21 12:03 ..
-rw-r—–  1   xarabas admins 30215  Feb 21 12:03 Report.log

Categories: Unix
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: