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 :

[pastacode lang=”bash” manual=”tar%20-zcvf%20archive-name.tar.gz%20directory-name%0A” message=”bash code” highlight=”” provider=”manual”/]

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 :

[pastacode lang=”bash” manual=”tar%20-zcvf%20archive-name.tar.gz%20directory-name%0A” message=”bash code” highlight=”” provider=”manual”/]

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:

[pastacode lang=”bash” manual=”%24%20tar%20-zcvf%20wiki.tar.gz%20%2Fhome%2Fwikitechy%2Fprog%0A” message=”bash code” highlight=”” provider=”manual”/]

[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):
[pastacode lang=”bash” manual=”%24%20tar%20-zxvf%20wiki.tar.gz%0A” message=”bash code” highlight=”” provider=”manual”/]

Where-x : Extract files

  • To extract files in particular directory, for example in /tmpwiki then we need to use the following command
[pastacode lang=”bash” manual=”%24%20tar%20-zxvf%20wiki.tar.gz%20-C%20%2Ftmpwiki%0A%24%20cd%20%2Ftmpwiki%0A%24%20ls%20-%0A” message=”bash code” highlight=”” provider=”manual”/]

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 :
[pastacode lang=”bash” manual=”tar%20-zcvf%20archive.tar.gz%20directory%2F%20%0A” message=”bash code” highlight=”” provider=”manual”/]
  • 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
[pastacode lang=”bash” manual=”tar%20-cv%20directory%20%7C%20gzip%20%3E%20archive.tar.gz%0A” message=”bash code” highlight=”” provider=”manual”/]
  • To decompress and unpack the archive into the current directory we would use
[pastacode lang=”bash” manual=”tar%20-zxvf%20archive.tar.gz%0A” message=”bash code” highlight=”” provider=”manual”/]
  • That command is effectively the same as
[pastacode lang=”bash” manual=”gunzip%20%3C%20archive.tar.gz%20%7C%20tar%20-xv%0A” message=”bash code” highlight=”” provider=”manual”/]

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

[pastacode lang=”bash” manual=”%24%20tree%20dir1%2F%0Adir1%2F%0A%7C–%20dir11%0A%7C%20%20%20%7C–%20file11%0A%7C%20%20%20%7C–%20file12%0A%7C%20%20%20%60–%20file13%0A%7C–%20file1%0A%7C–%20file2%0A%60–%20file3%0A” message=”bash code” highlight=”” provider=”manual”/]

Example:

Now on executing the gzip command

[pastacode lang=”bash” manual=”%24%20gzip%20-r%20dir1%0Aafter%0A” message=”bash code” highlight=”” provider=”manual”/]

After running the gzip command

[pastacode lang=”bash” manual=”%24%20tree%20dir1%2F%0Adir1%2F%0A%7C–%20dir11%0A%7C%20%20%20%7C–%20file11.gz%0A%7C%20%20%20%7C–%20file12.gz%0A%7C%20%20%20%60–%20file13.gz%0A%7C–%20file1.gz%0A%7C–%20file2.gz%0A%60–%20file3.gz%0A” message=”bash code” highlight=”” provider=”manual”/]

[ad type=”banner”]

  • To zip up the directory structure , use the tar command, and then compress the resulting .tar file
[pastacode lang=”bash” manual=”%24%20tar%20zcvf%20dir1.tar.gz%20dir1%2F%0A” message=”bash code” highlight=”” provider=”manual”/]

Example:

[pastacode lang=”bash” manual=”%24%20tar%20zcvf%20dir1.tar.gz%20dir1%2F%0Adir1%2F%0Adir1%2Ffile1%0Adir1%2Ffile2%0Adir1%2Fdir11%2F%0Adir1%2Fdir11%2Ffile11.gz%0Adir1%2Fdir11%2Ffile12.gz%0Adir1%2Fdir11%2Ffile13.gz%0Adir1%2Ffile3″ message=”bash code” highlight=”” provider=”manual”/]

Which results in the following single file :

[pastacode lang=”bash” manual=”%24%20ls%20-l%20%7C%20grep%20tar%0A-rw-rw-r–%201%20saml%20saml%20%20271%20Nov%20%201%2008%3A07%20dir1.tar.gz%0A” message=”bash code” highlight=”” provider=”manual”/]

Finally we can confirm its contents:

[pastacode lang=”bash” manual=”%24%20tar%20ztvf%20dir1.tar.gz%20%0Adrwxrwxr-x%20saml%2Fsaml%20%20%20%20%20%20%20%20%200%202017-10-01%2008%3A05%20dir1%2F%0A-rw-rw-r–%20saml%2Fsaml%20%20%20%20%20%20%20%20%200%202017-10-01%2007%3A45%20dir1%2Ffile1%0A-rw-rw-r–%20saml%2Fsaml%20%20%20%20%20%20%20%20%200%202017-10-01%2007%3A45%20dir1%2Ffile2%0Adrwxrwxr-x%20saml%2Fsaml%20%20%20%20%20%20%20%20%200%202017-10-01%2008%3A04%20dir1%2Fdir11%2F%0A-rw-rw-r–%20saml%2Fsaml%20%20%20%20%20%20%20%2027%202017-10-01%2007%3A45%20dir1%2Fdir11%2Ffile11.gz%0A-rw-rw-r–%20saml%2Fsaml%20%20%20%20%20%20%20%2027%202017-10-01%2007%3A45%20dir1%2Fdir11%2Ffile12.gz%0A-rw-rw-r–%20saml%2Fsaml%20%20%20%20%20%20%20%2027%202017-10-01%2007%3A45%20dir1%2Fdir11%2Ffile13.gz%0A-rw-rw-r–%20saml%2Fsaml%20%20%20%20%20%20%20%20%200%202017-10-01%2007%3A45%20dir1%2Ffile3%0A” message=”bash code” highlight=”” provider=”manual”/]

How to use:

[pastacode lang=”bash” manual=”cd%20%2Fhome%2F%3B%20gzipdir%20MyDirectory%20%20%20%20″ message=”bash code” highlight=”” provider=”manual”/]
  • It will create /home/MyDirectory.tgz and remove MyDirectory
[pastacode lang=”bash” manual=”gunzipdir%20%2Fhome%2FMyDirectory.tgz%0A” message=”bash code” highlight=”” provider=”manual”/]
  • 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:

[pastacode lang=”bash” manual=”%24%20tar%20–help%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20-z%2C%20-j%2C%20-J%2C%20–lzma%20%20Compress%20archive%20with%20gzip%2Fbzip2%2Fxz%2Flzma%0A” message=”bash code” highlight=”” provider=”manual”/]

Here is an example:

[pastacode lang=”bash” manual=”tar%20-zcvf%20outputFileName%20folderToCompress%0A” message=”bash code” highlight=”” provider=”manual”/]

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

[pastacode lang=”bash” manual=”%24%20tar%20-zcvf%20wikitechy.tar.gz%20wikitechy%2F%0A%0Aa%20wikitechy%0Aa%20wikitechy%2F.DS_Store%0Aa%20wikitechy%2FCHANGELOG%0Aa%20wikitechy%2Fextension-example.xml%0Aa%20wikitechy%2Fextension.xsd%0Aa%20wikitechy%2FLICENSE%0Aa%20wikitechy%2Fwikitechy-api-sources.jar%0Aa%20wikitechy%2Fwikitechy-api.jar%0Aa%20wikitechy%2Fwikitechy.jar%0Aa%20wikitechy%2Fwikitechy.yml%0Aa%20wikitechy%2Fnrcerts%0Aa%20wikitechy%2FREADME.txt%0A%0A%24%20ls%20-lsa%0A%0A%20%20%20%20%200%20drwxr-xr-x%20%2013%20mkyong%20%20staff%20%20%20%20%20%20%20%20442%20Mar%2024%2021%3A40%20wikitechy%0A%2011072%20-rw-r–r–%20%20%201%20mkyong%20%20staff%20%20%20%205667938%20Sep%2023%2015%3A44%20wikitechy.tar.gz%0A” message=”bash code” highlight=”” provider=”manual”/]

[ad type=”banner”]

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

[pastacode lang=”bash” manual=”%24%20tar%20-tf%20wikitechy.tar.gz%0Awikitechy%2F%0Awikitechy%2F._.DS_Store%0Awikitechy%2F.DS_Store%0Awikitechy%2F._CHANGELOG%0Awikitechy%2FCHANGELOG%0Awikitechy%2F._extension-example.xml%0Awikitechy%2Fextension-example.xml%0Awikitechy%2F._extension.xsd%0Awikitechy%2Fextension.xsd%0Awikitechy%2F._LICENSE%0Awikitechy%2FLICENSE%0Awikitechy%2F._wikitechy-api-sources.jar%0Awikitechy%2Fwikitechy-api-sources.jar%0Awikitechy%2F._wikitechy-api.jar%0Awikitechy%2Fwikitechy-api.jar%0Awikitechy%2F._wikitechy.jar%0Awikitechy%2Fwikitechy.jar%0Awikitechy%2F._wikitechy.yml%0Awikitechy%2Fwikitechy.yml%0Awikitechy%2F._nrcerts%0Awikitechy%2Fnrcerts%0Awikitechy%2F._README.txt%0Awikitechy%2FREADME.txt%0A” message=”bash code” highlight=”” provider=”manual”/]

Categorized in: