Commonly used Tar and Gzip Commands in Linux With Examples

Commonly used tar and gzip commands

In this article, we will review commonly used tar and gzip commands in Linux with some examples. Linux users and IT professionals alike, ought to be familiar with most if not all these commands.

Table of Contents

Introduction

In Linux, Tar and Gzip commands are used to compress and archive files and directories. These commands are widely used in the Linux environment due to their ability to save disk space and reduce transfer time when transferring files over networks. This article will explore the commonly used Tar and Gzip commands in Linux, their usage, examples, and comparisons of the different types of compression.

commonly used tar and gzip commands

Image by Pixabay from Pexels

The Tar Command

Tar (short for Tape Archive) is a command-line tool used for archiving files and directories into a single file. It can be used to create, extract, and view the contents of an archive file. The following is the syntax of the Tar command:

tar [options] [archive-file] [files or directories]

The following are some of the commonly used options for the Tar command:

-c: Creates a new archive file
-x: Extracts files from an archive
-t: Displays the contents of an archive
-v: Verbose mode. Shows the progress of the operation
-f: Specifies the filename of the archive
-z: Compress the archive with Gzip
-j: Compress the archive with Bzip2

Tar Command Examples

To create a tar archive file (.tar), run the following command at your terminal.

				
					$ tar -cvf archive.tar file1 file2 directory1
				
			

To extract the archive.tar file, run the following command:

				
					$ tar -xvf archive.tar
				
			

Run this command (below) to create a Gzip compressed archive.tar.gz file.

				
					$ tar -czvf archive.tar.gz file1 file2 directory1
				
			

This command (below) will extract the contents of a Gzip compressed file or archive.tar.gz.

				
					$ tar -xzvf archive.tar.gz
				
			

To create a Bzip2 compressed file, run this command (below):

				
					$ tar -cjvf archive.tar.bz2 file1 file2 directory1
				
			

To extract a Bzip2 compressed file or archive.tar.bz2.

				
					$ tar -xjvf archive.tar.bz2
				
			

The Gzip Command

Gzip is a file compression utility in Linux that compresses files using the Lempel-Ziv coding algorithm. It is used to reduce the size of files and directories, making them easier to transfer over a network or save disk space. The following is the syntax of the Gzip command:

gzip [options] [files]

The following are some of the commonly used options for the Gzip command:

-c: Write output to stdout
-d: Decompress the file
-l: List compressed files
-v: Verbose mode. Shows the progress of the operation
-9: Maximum compression level
-k: Keep the original file
-f: Force compression

Gzip Command Examples

Compress a file with Gzip (below). Notice the new file has the .gz extension.

				
					$ gzip file.txt
				
			

Decompress a Gzip file:

				
					$ gzip -d file.txt.gz
				
			

Compress a Gzip file with the maximum compression level:

				
					$ gzip -9 file.txt
				
			

Keep the original file after compression:

				
					$ gzip -k file.txt
				
			

Comparisons of the Different Types of Compression

Gzip, Bzip2, and LZMA are the most commonly used compression algorithms in Linux. Here are some comparisons of the different types of compression:

Gzip

  • Fast compression and decompression
  • Good compression ratio
  • Uses less memory than Bzip2 and LZMA
  • Compatible with most operating systems

Bzip2

  • Slower compression and decompression
  • Better compression ratio than Gzip
  • Uses more memory than Gzip
  • Compatible with most operating systems

LZMA

  • Slower compression and decompression
  • Best compression ratio
  • Uses a lot of memory
  • Not compatible with all operating systems

Conclusion

In conclusion, the Tar and Gzip commands are essential utilities in Linux used for archiving and compressing files and directories. The Tar command is used to create archive files, while the Gzip command is used to compress files using different algorithms. The Tar command can also be used to compress archive files with Gzip, Bzip2, or LZMA algorithms. Each compression algorithm has its advantages and disadvantages, with Gzip being the most commonly used due to its fast compression and decompression speeds, good compression ratio, and compatibility with most operating systems. Bzip2 and LZMA are slower but offer better compression ratios. Overall, understanding and mastering these commands are essential skills for any Linux user, especially for system administrators dealing with large amounts of data.

Was this article helpful to you? If so, leave us a comment below. We’d love to hear from you!

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *