What is the difference between Tar and Gzip? With Examples

Tar vs Gzip Explained

Tar and Gzip are two different utilities that are commonly used in Linux and other Unix-like operating systems.

Tar, short for “tape archive”, is a file archiving utility that is used to bundle multiple files and directories into a single file. It does not provide any compression but simply creates an uncompressed archive. Tar is often used in conjunction with other compression utilities, such as Gzip, to create compressed archives.

Gzip, on the other hand, is a compression utility that is used to compress individual files. It uses the gzip algorithm to compress files, which typically results in significantly smaller file sizes. Gzip is often used in conjunction with Tar to create compressed archives.

In summary, Tar is used to bundle multiple files into a single file, while Gzip is used to compress individual files. They are often used together to create compressed archives.

How to Compress Files Using Gzip

Gzip is a widely used file compression utility on Unix-like operating systems. It can compress a single file or a group of files into a compressed archive with the extension .gz. Here’s how to use Gzip to compress files:

  1. Open a terminal on your Unix-like system.
  2. Navigate to the directory containing the file or files you want to compress using the cd command. For example:
Terminal

cd Documents

  1. To compress a single file:
[…]

gzip example.txt

  1. To compress multiple files into a single archive:
[…]

tar -czvf examples.tar.gz example1.txt example2.txt example3.txt

  1. Verify compression with ls. Compressed files will have .gz or .tar.gz extensions.

How to Decompress Files

To decompress a file or archive that has been compressed using Gzip:

To decompress a single file:

[…]

gzip -d filename.gz

To decompress a tar archive:

[…]

tar -xvzf archive-name.tar.gz

How to Create an Archive Using Tar

  1. Open the terminal.
  2. Navigate to the directory with files you want to archive.
  3. Use the tar command with the cvf options. For example:
[…]

tar cvf myarchive.tar file1.txt file2.txt

To archive a directory:

[…]

tar cvf myarchive.tar mydirectory

  1. Press Enter and wait for completion.
  2. The archive .tar file will appear in your current directory.

That’s it! You’ve successfully created and extracted files using tar and gzip.