Skip to main content

Understanding the `tar` Command in Linux: Archiving and Compression Essentials

In the realm of Linux file management, tar stands out as a multifaceted tool used for archiving multiple files into a single archive file. This command is frequently coupled with compression to reduce the archive's size. Understanding the difference between archiving and compression is fundamental to using tar effectively.

Archiving vs. Compression: What's the Difference?

Archiving is the process of combining multiple files and directories into a single file, known as an archive. This process makes it easier to handle and transfer a collection of files as one unit but does not necessarily reduce the size of the files.

Compression, on the other hand, is the process of reducing the size of files by encoding information more efficiently. This is often used after archiving to save storage space or to speed up file transfers.

What is tar

The tar command in Linux stands for "Tape Archive" and is used to create and manipulate tar archives. The name is derived from its historical use of archiving data on magnetic tape.

The tar command can collect many files into one larger file, while maintaining the directory structure and metadata such as permissions and timestamps. Although originally designed for tape backups, tar is widely used for creating archives in file storage and is a standard method for distributing sets of files on Unix-based systems.

Does tar Reduce the Size of Files in an Archive?

When discussing tar, it's crucial to address a common misconception: does the tar command itself reduce the size of the files it archives? The answer is no—tar alone does not compress files. It simply joins them together into a single archive, preserving the original file size and format. The resulting archive file size will roughly equal the total size of the files being archived.

Purpose of tar Without Compression

The primary purpose of tar without additional compression is to combine multiple files into one container. This can be particularly useful for:

  • Organization: Keeping related files bundled together.
  • Transport: Making it easier to transfer a collection of files.
  • Preservation: Maintaining file permissions and attributes.

Adding Compression to tar Archives

While tar itself does not compress files, it is commonly used in conjunction with compression tools to reduce the size of the archived files. By using options like -z for gzip, -j for bzip2, or -J for xz, tar can create a compressed archive file. These tools apply compression algorithms to the tar archive to significantly reduce the file size, making it suitable for storage and faster transfer over networks.

Syntax of tar

The basic syntax of the tar command is as follows:


The tar command can create new archives, extract files from an existing archive, list the contents of an archive, and even update or append files to an existing archive.

Preparing Sample Files for an Archive

Before diving into examples, let's create some sample files and directories to work with:

# Create sample files and directories
mkdir archive_contents
echo "Hello, World!" > archive_contents/file1.txt
echo "This is a sample for tar archiving." > archive_contents/file2.txt

With these sample files, we can explore various tar operations.

Examples of Using tar

Creating an Archive

tar -cvf archive.tar archive_contents/
  • -c creates a new archive.
  • -v stands for "verbose," which lists the files processed.
  • -f specifies the filename of the archive.

This creates an archive named archive.tar containing the directory archive_contents.

Extracting Files from an Archive

tar -xvf archive.tar
  • -x extracts files from an archive.

This extracts the contents of archive.tar into the current directory.

Viewing the Contents of an Archive

tar -tvf archive.tar
  • -t lists the contents of an archive without extracting them.

Compressing an Archive with gzip

tar -czvf archive.tar.gz archive_contents/
  • -z filters the archive through gzip for compression.

Compressing an Archive with bzip2

tar -cjvf archive.tar.bz2 archive_contents/
  • -j filters the archive through bzip2 for compression.

Extracting a gzip-Compressed Archive

tar -xzvf archive.tar.gz

Extracting a bzip2-Compressed Archive

tar -xjvf archive.tar.bz2

Options Table for tar

--create-cCreate a new archive.
--extract-xExtract files from an archive.
--file-fUse archive file or device ARCHIVE.
--verbose-vVerbosely list files processed.
--list-tList the contents of an archive.
--gzip-zCompress the archive with gzip.
--bzip2-jCompress the archive with bzip2.
--xz-JCompress the archive with xz.
--append-rAppend files to the end of an archive.
--update-uAppend files newer than copy in archive.
--delete--deleteDelete from the archive (not on all platforms).
--exclude--excludeExclude files, given as a pattern.
--incremental-GHandle new GNU-format incremental backup.
--help--helpDisplay help message.
--version--versionDisplay version information.

When to Use tar

  • Backup: When creating a backup of files and directories, tar allows for easy packaging into one archive file.
  • Software Distribution: Many software projects are distributed as .tar.gz or .tar.bz2 archives to include binary, source files

What Can You Do Next 🙏😊

If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.

YouTube @cloudaffle