Generally we can Zip/compress the entire folder / Whole Linux or UNIX Directory

  • Compressing a whole linux/UNIX directory is easy, certainly in terms of technical basis it is called as a compressed archive.
  • GNU tar command is best for this work.
  • It can be used on remote linux or UNIX server.
  • It performs two things:
    1. Create the archive
    2. Compress the archive

Syntax of tar command :

bash code
tar -zcvf archive-name.tar.gz directory-name

Where,

  • -z : Compress archive using gzip program
  • -c : Create archive
  • -v : Verbose i.e. display progress while creating archive
  • -f : Archive File name

Syntax of tar command :

bash code
tar -zcvf archive-name.tar.gz directory-name

Considering, we have a directory called /home/wikitechy/prog and we would wish to compress this directory then we need to type tar command as follows:

bash code
$ tar -zcvf wiki.tar.gz /home/wikitechy/prog

[ad type=”banner”]

  • Above command will create an archive file called wiki.tar.gz in current directory.
  • To restore the archive we need to use the following command (it will extract all files in current directory):
bash code
$ tar -zxvf wiki.tar.gz

Where-x : Extract files

  • To extract files in particular directory, for example in /tmpwiki then we need to use the following command
bash code
$ tar -zxvf wiki.tar.gz -C /tmpwiki
$ cd /tmpwiki
$ ls -

Trying to zip a folder in unix can be done using the gzip command

  • We can use gzip -r myfolder which will gzip files recursively.
  • The same way we can unzip files using gunzip -r myfolder which will unzip files recursively.
  • At some cases we can Use tar; it supports options for compression.
  • gzip command is designed as a complement to tar, not as a replacement.
  • Zipping an entire folder using gzip is not possible , because Unlike zip, gzip functions as a compression algorithm only.
  • Unix uses a program named tar to archive data, which can then be compressed with a compression program like gzip, bzip2, 7zip, etc.
  • In order to “zip” a directory, the correct command would be mentioned bellow :
bash code
tar -zcvf archive.tar.gz directory/ 
  • This will tell tar to c (create) an archive from the files in directory (tar is recursive by default), compress it using the z (gzip) algorithm, store the output as a f (file) named archive.tar.gz, and v (verbosely) list (on /dev/stderr so it doesn’t affect piped commands) all the files it adds to the archive
  • The tar command offers gzip support (via the -z flag).
  • The gzip command/lib is completely separate. The command above is effectively the same as
bash code
tar -cv directory | gzip > archive.tar.gz
  • To decompress and unpack the archive into the current directory we would use
bash code
tar -zxvf archive.tar.gz
  • That command is effectively the same as
bash code
gunzip < archive.tar.gz | tar -xv

Using the -r switch , the gzip command will not recursively compress a directory into a single zip file, rather it will go into that directory structure and zip each file that it finds into a separate file.

Example:

Before running the gzip command

bash code
$ tree dir1/
dir1/
|-- dir11
| |-- file11
| |-- file12
| `-- file13
|-- file1
|-- file2
`-- file3

Example:

Now on executing the gzip command

bash code
$ gzip -r dir1
after

After running the gzip command

bash code
$ tree dir1/
dir1/
|-- dir11
| |-- file11.gz
| |-- file12.gz
| `-- file13.gz
|-- file1.gz
|-- file2.gz
`-- file3.gz

[ad type=”banner”]

  • To zip up the directory structure , use the tar command, and then compress the resulting .tar file
bash code
$ tar zcvf dir1.tar.gz dir1/

Example:

bash code
$ tar zcvf dir1.tar.gz dir1/
dir1/
dir1/file1
dir1/file2
dir1/dir11/
dir1/dir11/file11.gz
dir1/dir11/file12.gz
dir1/dir11/file13.gz
dir1/file3

Which results in the following single file :

bash code
$ ls -l | grep tar
-rw-rw-r-- 1 saml saml 271 Nov 1 08:07 dir1.tar.gz

Finally we can confirm its contents:

bash code
$ tar ztvf dir1.tar.gz 
drwxrwxr-x saml/saml 0 2017-10-01 08:05 dir1/
-rw-rw-r-- saml/saml 0 2017-10-01 07:45 dir1/file1
-rw-rw-r-- saml/saml 0 2017-10-01 07:45 dir1/file2
drwxrwxr-x saml/saml 0 2017-10-01 08:04 dir1/dir11/
-rw-rw-r-- saml/saml 27 2017-10-01 07:45 dir1/dir11/file11.gz
-rw-rw-r-- saml/saml 27 2017-10-01 07:45 dir1/dir11/file12.gz
-rw-rw-r-- saml/saml 27 2017-10-01 07:45 dir1/dir11/file13.gz
-rw-rw-r-- saml/saml 0 2017-10-01 07:45 dir1/file3

How to use:

bash code
cd /home/; gzipdir MyDirectory    
  • It will create /home/MyDirectory.tgz and remove MyDirectory
bash code
gunzipdir /home/MyDirectory.tgz
  • It will create /home/MyDirectory and remove /home/MyDirectory.tgz
  • In Linux, using gzip its unable to compress a folder, since its being used to compress a single file only.
  • To compress a folder, we should use tar + gzip, which is tar -z

Note:

bash code
$ tar --help
-z, -j, -J, --lzma Compress archive with gzip/bzip2/xz/lzma

Here is an example:

bash code
tar -zcvf outputFileName folderToCompress

How to gzip a folder in Linux:

1. Tar + Gzip a folder:

Lets see the example to gzip a folder named “wikitechy”, and also all the files within that folder, into a single compressed file wikitechy.tar.gz

bash code
$ tar -zcvf wikitechy.tar.gz wikitechy/

a wikitechy
a wikitechy/.DS_Store
a wikitechy/CHANGELOG
a wikitechy/extension-example.xml
a wikitechy/extension.xsd
a wikitechy/LICENSE
a wikitechy/wikitechy-api-sources.jar
a wikitechy/wikitechy-api.jar
a wikitechy/wikitechy.jar
a wikitechy/wikitechy.yml
a wikitechy/nrcerts
a wikitechy/README.txt

$ ls -lsa

0 drwxr-xr-x 13 mkyong staff 442 Mar 24 21:40 wikitechy
11072 -rw-r--r-- 1 mkyong staff 5667938 Sep 23 15:44 wikitechy.tar.gz

[ad type=”banner”]

2.List the contents of wikitechy.tar.gz:

bash code
$ tar -tf wikitechy.tar.gz
wikitechy/
wikitechy/._.DS_Store
wikitechy/.DS_Store
wikitechy/._CHANGELOG
wikitechy/CHANGELOG
wikitechy/._extension-example.xml
wikitechy/extension-example.xml
wikitechy/._extension.xsd
wikitechy/extension.xsd
wikitechy/._LICENSE
wikitechy/LICENSE
wikitechy/._wikitechy-api-sources.jar
wikitechy/wikitechy-api-sources.jar
wikitechy/._wikitechy-api.jar
wikitechy/wikitechy-api.jar
wikitechy/._wikitechy.jar
wikitechy/wikitechy.jar
wikitechy/._wikitechy.yml
wikitechy/wikitechy.yml
wikitechy/._nrcerts
wikitechy/nrcerts
wikitechy/._README.txt
wikitechy/README.txt

Categorized in: