Compressing / Archiving files 


For a large file, it's taking more disk space and time to transfer from one location to another over communication like FTP, SFTP etc. But when we compress file, file size becomes smaller and takes up less disk space. It is efficient to transfer file as well as for uploading and downloading files after compression.


Archiving file is different from compressing file. File archive is nothing but collecting all files at one place in one file. If it is only archival it takes the same amount of disk space as each individual files are taking inside it. When we compress large and infrequently used files and make an archival, we are gonna save our disk space.






zip command


This command is used for both compressing as well as archiving files.


Syntax:

zip [option] target_file.zip source_file1 source_file2... 


Example:

$zip destzip.zip A_dir B_dir C_dir
  adding: A_dir/ (stored 0%)
  adding: B_dir/ (stored 0%)
  adding: C_dir/ (stored 0%)
$ls -l
total 72
drwxr-xr-x  3 pinku  guest   512 Dec 24 08:10 A_dir
drwxr-xr-x  2 pinku  guest   512 Dec 24 08:12 B_dir
drwxr-xr-x  3 pinku  guest   512 Dec 25 00:14 C_dir
-rw-r--r--  1 pinku  guest  6345 Dec 29 00:53 Cocoon
drwxrwxrwx  3 pinku  guest   512 Dec 24 08:53 G_dir
-rw-r--r--  1 pinku  guest  2400 Dec 29 01:04 ShortStory.txt
-rw-r--r--  1 pinku  guest   442 Jan  6 00:03 destzip.zip


unzip command

This command decompress the archive for a set of files.


Syntax:

unzip [option] file.zip


Example:

$unzip destzip.zip
Archive:  destzip.zip
d A_dir
d B_dir
d C_dir



Options to use with zip command:


  • -r  recursive, all the files and directories from source files are saved in a target zip archive


  • -@  zip takes the list of input files from standard input instead of  from the command line


  • -u  update, update  existing entries if newer on the file system and add new files. If the archive does not exist issue warning then create a new archive


  • -d  select entries in an existing archive and delete them


  • -e  encrypt, contents of the zip archive using a password which is entered on the terminal in response to a prompt


  • -i  include only the specified files in archive


  • -ic  ignore-case, when matching archive entries


  • -o  It will change the last modified time of archive to the latest time of the entries in archive


  • -p  Include relative file paths as part of the names of files stored in the archive, This is the default


  • -P  password, use password to encrypt zip file entries if any


  • -v  verbose mode on, this option enables the  display of a progress indicator during compression


  • -x  Explicitly exclude the specified files


  • -#  "#" is a digit 0-9, where "-0" indicates no compression and only stores all  files.  "-1" indicates the fastest compression speed but less compression ratio and "-9" indicates the slowest compression speed but highest compression ratio, ignores the suffix list. The default compression level is "-6"



Options to use with unzip command:


  • -l  list out files present in zip archive


  • -v  view file present in zip archive 


  • -n   no overwrite, if a file with the same name already exists on disk, the file is silently skipped 


  • -o   overwrite, if a file with the same name already exists on disk, the existing file is replaced with the file from the zip file 








Examples:


$zip target_A.zip A_dir
  adding: A_dir/ (stored 0%)
$zip -r target_R.zip A_dir
  adding: A_dir/ (stored 0%)
  adding: A_dir/myfile2.txt (stored 0%)
  adding: A_dir/file1.txt (stored 0%)
  adding: A_dir/A1/ (stored 0%)
  adding: A_dir/A1/A2/ (stored 0%)
  adding: A_dir/file2.txt (stored 0%)
$

$unzip -l target_A.zip (only to list out archive files)
Archive:  target_A.zip
  Length     Date   Time    Name
 --------    ----   ----    ----
        0  12-24-16 08:10   A_dir/
 --------                   -------
        0                   1 file
$unzip -l target_R.zip
Archive:  target_R.zip
  Length     Date   Time    Name
 --------    ----   ----    ----
        0  12-24-16 08:10   A_dir/
        0  05-15-15 00:00   A_dir/myfile2.txt
        3  12-24-16 02:52   A_dir/file1.txt
        0  12-24-16 02:48   A_dir/A1/
        0  12-24-16 02:48   A_dir/A1/A2/
        3  12-24-16 02:53   A_dir/file2.txt
 --------                   -------
        6                   6 files
$

$zip -@ target_stdin.zip
B_dir
G_dir  
(press ctrl d)
  adding: B_dir/ (stored 0%)
  adding: G_dir/ (stored 0%)
$

$unzip -v target_stdin.zip(To view only in detail without unzip)
Archive:  target_stdin.zip
 Length   Method    Size  Ratio   Date   Time   CRC-32    Name
--------  ------  ------- -----   ----   ----   ------    ----
       0  Stored        0   0%  12-24-16 08:12  00000000  B_dir/
       0  Stored        0   0%  12-24-16 08:53  00000000  G_dir/
--------          -------  ---                            -------
       0                0   0%                            2 files
$

$zip -u target_A.zip B_dir
  adding: B_dir/ (stored 0%)
$
$unzip -l target_A.zip
Archive:  target_A.zip
  Length     Date   Time    Name
 --------    ----   ----    ----
        0  12-24-16 08:10   A_dir/
        0  12-24-16 08:12   B_dir/
 --------                   -------
        0                   2 files
$

$zip -d target_A.zip B_dir
deleting: B_dir/
$

$unzip -l target_A.zip
Archive:  target_A.zip
  Length     Date   Time    Name
 --------    ----   ----    ----
        0  12-24-16 08:10   A_dir/
 --------                   -------
        0                   1 file
$

$zip -r target_A.zip . -i *.txt
  adding: file1.txt (deflated 12%)
  adding: file2.txt (stored 0%)
  adding: file3.txt (deflated 22%)
  adding: file4.txt (deflated 40%)
  adding: vikas.txt (stored 0%)
  adding: ShortStory.txt (deflated 59%)
  adding: story.txt (deflated 55%)
  adding: ls_out_onecolumn.txt (deflated 59%)
  adding: language_copy.txt (deflated 3%)
$

$zip -e target_encrypted.zip file1.txt file2.txt
Enter password:
Verify password:
  adding: file1.txt (deflated 12%)
  adding: file2.txt (stored 0%)
$

$unzip -n target_encrypted.zip
Archive:  target_encrypted.zip
unzip: ZIP decompression failed (-3)
$

$zip -r target_new.zip A_dir -x \*.txt
  adding: A_dir/ (stored 0%)
  adding: A_dir/A1/ (stored 0%)
  adding: A_dir/A1/A2/ (stored 0%)
$unzip -l target_new.zip
Archive:  target_new.zip
  Length     Date   Time    Name
 --------    ----   ----    ----
        0  12-24-16 08:10   A_dir/
        0  12-24-16 02:48   A_dir/A1/
        0  12-24-16 02:48   A_dir/A1/A2/
 --------                   -------
        0                   3 files
$ls -lR A_dir
total 12
drwxr-xr-x  3 pinku  guest  512 Dec 24 02:48 A1
-rw-r--r--  1 pinku  guest    3 Dec 24 02:52 file1.txt
-rw-r--r--  1 pinku  guest    3 Dec 24 02:53 file2.txt
-rw-r--r--  1 pinku  guest    0 May 15  2015 myfile2.txt

A_dir/A1:
total 4
drwxr-xr-x  2 pinku  guest  512 Dec 24 02:48 A2

A_dir/A1/A2:
total 0
$


NEXT->

Cracking IT Interview