Answered by the Webhosting Experts

How to Archive and Compress Files in Linux (Using Tar)

The tar command (short for Tape Archive) bundles multiple files and directories into a single, easily transportable archive file known as a tarball. It serves as the standard Linux utility for creating and managing these archives. Essential for server administration, it allows you to back up critical data, preserve strict file permissions, and seamlessly integrate with compression tools like gzip (.tar.gz) to save disk space. Mastering its basic flags is a fundamental skill for efficiently managing, transferring, and restoring data on any Linux server.

The Anatomy of a Tar Command

The tar command relies on short, case-sensitive flags to tell it exactly what to do. A standard command looks like this:

tar [options] [archive_name] [target_files_or_directories]

Here are the primary operational flags you need to know:

  • -c (Create): Tells tar to create a new archive file.
  • -x (Extract): Tells tar to unpack an existing archive.
  • -t (List): Tells tar to view or list the contents of an archive without extracting it.

Here are the most common modifier flags:

  • -v (Verbose): Visually displays the progress in the terminal by listing every file being processed.
  • -f (File): Specifies the filename of the archive. This must always be the last flag before the archive name.
  • -z (Gzip): Compresses the archive using gzip (creates .tar.gz).
  • -j (Bzip2): Compresses the archive using bzip2 (creates .tar.bz2).

How to Create an Archive

To bundle a directory or a group of files into an uncompressed tarball, use the -cvf flag combination.

  1. Open your terminal and navigate to the directory containing the items you want to archive.
  2. Run the command tar -cvf backup.tar website_data/ to archive a folder named website_data

Creating the tar File
Creating the tar File
Newly Created tar File
Newly Created tar File

How to Extract an Archive

When you download a backup or software package, you need to unpack it. To extract an archive into your current working directory, use the -xvf flags.

  1. Navigate to the directory where you want the extracted files to land.
  2. Run the extraction command tar -xvf backup.tar if you wish to extract the file in the same directory that it is in or add -C /path/to/destination to it to extract it elsewhere. Ensure that the target directory exists before doing so.
Extracting the Archive
Extracting the Archive

Compressing Your Tarballs: .tar vs .tar.gz vs .tar.bz2

On a production server, uncompressed tarballs waste valuable storage and bandwidth. Adding a single compression flag to your tar command fixes this instantly.

Gzip offers fast performance and standard for most web server backups however Bzip2 is available as well and offers maximum compression size reduction, though it takes longer to process.

 

Gzip Operations

  • Create a compressed gzip archive using tar -czvf backup.tar.gz /path/to/directory
  • Extract the compressed gzip archive using tar -xzvf backup.tar.gz -C /path/to/target_directory
Difference in Size Between a tarball and a Compressed tarball
Difference in Size Between a tarball and a Compressed tarball

Bzip2 Operations

  • Create a compressed bzip2 archive using tar -cjvf backup.tar.bz2 /path/to/directory
  • Extract the compressed gzip archive using tar -xjvf backup.tar.bz2 -C /path/to/target_directory

How to View the Contents of a Tar File Without Extracting It

Extracting a massive archive just to see what is inside can clutter your server’s disk space. You can safely peak inside any tarball using the -tvf flags.

Using tar -tvf backup.tar.gz the compressed tarball can be viewed without extraction as shown below.

Viewing Content of Compressed tarball  Without Extraction
Viewing Content of Compressed tarball Without Extraction

Need More Personalized Help?

If you have any further issues, questions, or would like some assistance checking on this or anything else, please reach out to us from your my.hivelocity.net account and provide your server credentials within the encrypted field for the best possible security and support.

If you are unable to reach your my.hivelocity.net account or if you are on the go, please reach out from your valid my.hivelocity.net account email to us here at: support@hivelocity.net. We are also available to you through our phone and live chat system 24/7/365.