Sunday, September 6, 2015

zip , gzip, unzip, gunzip, tar and family Linux

Archiving files is a very important and useful task in real-world day to day use, tools like Win-zip or Win-Rar are probably the most known and well known archive tools for Windows system, Linux on uses a separate set of tools for this purpose, lets look at some of those.

Lets us consider we want to archive a set of files ( in *nix almost everything is a file ) as below.
bash-$ ls
directory1  directory2 directory3  directory4 file1  file2  file3
bash-$ 
we can use tar to create an archive of it using below command.
bash-$
bash-$ ls
directory1  directory2 directory3  directory4 file1  file2  file3
bash-$
bash-$ tar -cf archive.tar *
bash-$
bash-$
bash-$ ls
archive.tar.gz directory1  directory2 directory3  directory4 file1  file2  file3
bash-$
bash-$ 
Where -c indicates creation of archive identified by option -f with name archive.tar .
To see contents of archive file archive.tar

bash-$
bash-$ tar -tf archive.tar
directory1/
directory2/
directory3/
directory4/
file1/
file2/
file3/
bash-$ 


you can get more information on this using file command.
bash-$ file archive.tar
archive.tar: POSIX tar archive (GNU)
bash-$ 

To extract archive file and retrieve contents -x option can be issues along with -f and archive file.
bash-$
bash-$ ls
archive.tar
bash-$ tar -xf archive.tar
bash-$ ls
archive.tar  directory1  directory2  directory3  directory4  file1  file2  file3
bash-$
Existing tar files can be compressed further using gzip command.
bash-$ gzip archive.tar
bash-$ ls
archive.tar.gz directory1  directory2 directory3  directory4 file1  file2  file3
bash-$ 
gzip produces a more compressed archive file in terms of size.

extracting gzipped file to its original tar can be done by using gunzip, followed by tar with -x to get original contents.

bash-$
bash-$ ls
archive.tar.gz
bash-$
bash-$ gunzip archive.tar.gz
bash-$
bash-$ ls
archive.tar
bash-$
bash-$ tar -xf archive.tar
bash-$
bash-$ ls
archive.tar  directory1  directory2  directory3  directory4  file1  file2  file3
bash-$
bash-$ 

in fact,   tar can be used to generate/extract zipped archive directly using -z option
bash-$ tar -czf archive.tar.gz *
bash-$ ls
archive.tar.gz directory1  directory2 directory3  directory4 file1  file2  file3
bash-$ rm directory* file* -rf
bash-$
bash-$ ls
archive.tar.gz
bash-$
bash-$ tar -xzf archive.tar.gz
bash-$ ls
archive.tar.gz directory1  directory2 directory3  directory4 file1  file2  file3
bash-$ 
similar operation can be performed using zip and unzip commands

Zip files to create an archive ( note -r option for recursive zipping )
bash-$
bash-$ ls
directory1  directory2 directory3  directory4 file1  file2  file3
bash-$
bash-$
bash-$ zip -r archive.zip *
  adding: directory1/ (stored 0%)
  adding: directory2/ (stored 0%)
  adding: directory3/ (stored 0%)
  adding: directory4/ (stored 0%)
  adding: file1/ (stored 0%)
  adding: file2/ (stored 0%)
  adding: file3/ (stored 0%)
bash-$
bash-$ ls
archive.zip  directory1  directory2  directory3  directory4  file1  file2  file3
bash-$ 

unzip to extract zipped archives
bash-$
bash-$ rm directory* file* -rf
bash-$
bash-$
bash-$ ls
archive.zip
bash-$
bash-$
bash-$ unzip archive.zip
Archive:  archive.zip
   creating: directory1/
   creating: directory2/
   creating: directory3/
   creating: directory4/
   creating: file1/
   creating: file2/
   creating: file3/
bash-$
bash-$ ls
archive.zip  directory1  directory2  directory3  directory4  file1  file2  file3
bash-$
bash-$ 



No comments:

Post a Comment