What is the difference between Tar and Gzip? With Examples

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, if your files are located in the Documents directory, you can navigate there using the command cd Documents.
  3. To compress a single file, use the following command:
gzip filename

For example, if you want to compress a file named, you would enter the command gzip example.txt. This will create a compressed file named example.txt.gz.

  1. To compress multiple files into a single archive, use the following command:

tar -czvf archive-name.tar.gz file1 file2 file3

Replace archive-name with the name, you want to give your compressed archive. For example, if you want to compress files example1.txt, example2.txt, and example3.txt into an archive called examples.tar.gz, you would enter the command tar -czvf examples.tar.gz example1.txt example2.txt example3.txt.

  1. Once the compression process is complete, you can verify that the files have been compressed by using the ls command. Compressed files will have the .gz or .tar.gz extension.

To decompress a file or archive that has been compressed using Gzip, use the following command:

  • 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?

To create an archive using the tar command, follow these steps:

  1. Open the terminal or command prompt.
  2. Navigate to the directory where the files and directories that you want to include in the archive are located.
  3. Use the tar command with the cvf options followed by the archive name and the file or directory names that you want to include in the archive. For example, if you want to create an archive named myarchive.tar that includes the files file1.txt and file2.txt, you can use the following command:
    tar cvf myarchive.tar file1.txt file2.txt

    If you want to include a directory instead of specific files, you can use the directory name instead of the file names. For example, to include all the files and directories in the mydirectory directory, you can use the following command:

    tar cvf myarchive.tar mydirectory

    The c option tells tar to create a new archive, the v option tells tar to show verbose output, and the f option tells tar to use the specified file name for the archive.

  4. Press Enter to run the command.
  5. Wait for the tar command to complete. Once it’s done, you should see the archive file in the current directory.

That’s it! You’ve successfully created an archive using tar.