How to Compress and Extract Files in Linux Using tar and gzip Commands

Posted on

How to Compress and Extract Files in Linux Using tar and gzip Commands

File compression is a valuable technique for transferring files between computers and servers. Linux administrators frequently handle archiving, compression, and extraction tasks. An archive combines multiple files or directories into a single file. Tar files, often recognized by extensions like .tar, .tar.gz, .xz, or .bz2, are usually created using the .tar utility.

These archives reduce disk space usage and are ideal for convenient internet downloads. After downloading an archive, users can extract the files using unzipping utilities. Linux offers several command-line tools, particularly tar and gzip, for efficient compression and extraction of large archives.

This guide shows you how to use the tar and gzip commands to archive, compress, and extract files on Linux systems. We’ll use Ubuntu 22.04 to demonstrate the commands.

What Is tar?

tar, short for “tape archive,” is a common command-line utility for creating and extracting archives.

Compressed archives, also known as tarballs, can include multiple files and directories. They typically have extensions like .tar, .tar.gz, .xz, or .bz2. Linux developers often use tar archives to distribute source code and other important files. You can combine tar with gzip and bzip2 for different compression formats.

What Is gzip?

gzip is a single-file stream, lossless compression tool for Linux used to compress and decompress files. It offers both command-line and graphical interfaces for versatility.

gzip’s key features include the ability to compress multiple files simultaneously, command-line operation, LZ77 and Lempel–Ziv–Welch algorithms, shortening of long filenames, and automatic addition of the .gz extension. Gzip is a strong, user-friendly compression tool suitable for Linux environments.

tar and gzip (File Compression Tools)

The key difference between tar and gzip lies in their main functions:

tar creates archives from multiple files, while gzip compresses single files. However, you can use them together. The “z” switch with the tar command integrates gzip, allowing you to create compressed archives seamlessly.

How to Compress a File or Directory Using tar Command

The basic syntax to compress a file using tar is:

$ tar -czvf <tar compress file name> </destination path for files/or/directory>

For example, if you have a file/directory named “/home/samreena” in the current directory, compress it to an archive named testfile.tar.gz with:

$ tar -czvf testfile.tar.gz /home/samreena

In the above command:

-c: Creates a tar archive file

-z: Compresses the archive using gzip

-v: “Verbose” mode displays the progress details in the terminal.

-f: Specifies the archive file name.

how to compress and extract files in linux using tar and gzip commands

Verify the new archive with “ls”:

how to compress and extract files in linux using tar and gzip commands

How to Compress Multiple Files or Directories to an Archive File

To compress multiple files or directories into a single archive: Navigate to the directory containing the files/subdirectories using the ‘cd’ command. Then, use the tar command with flags like “tar czvf” to create the compressed archive. For example:

$ tar -czvf <archive_name.tar.gz> <file1 file2 file3 …>

If you have “index.html”, “testfile.tar.gz”, and “testfolder2” in the current Documents directory, compress them into “test_archive.tar.gz” using:

$ tar -czf test_archive.tar.gz index.html testfile.tar.gz testfolder2

how to compress and extract files in linux using tar and gzip commands

This creates a compressed archive named test_archive.tar.gz containing the specified files.

How to Exclude Files or Directories during Archiving File

To exclude specific files from the archive, use the –exclude

Share this:

Leave a Reply

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