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:
- Create the archive
- Compress the archive
Syntax of tar command :
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 :
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:
[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):
Where-x : Extract files
- To extract files in particular directory, for example in /tmpwiki then we need to use the following command
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 :
- 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
- To decompress and unpack the archive into the current directory we would use
- That command is effectively the same as
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
Example:
Now on executing the gzip command
After running the gzip command
[ad type=”banner”]
- To zip up the directory structure , use the tar command, and then compress the resulting .tar file
Example:
Which results in the following single file :
Finally we can confirm its contents:
How to use:
- It will create /home/MyDirectory.tgz and remove MyDirectory
- 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:
Here is an example:
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
[ad type=”banner”]
2.List the contents of wikitechy.tar.gz: