Post

How to Compress and Extract Files in Linux (tar, gzip, zip)

How to Compress and Extract Files in Linux (tar, gzip, zip)

Compressing and extracting files is a common task in Linux, whether you’re backing up data, transferring files, or saving disk space. In this post, we will explore how to use tar, gzip, and zip to manage files efficiently. We’ll cover the basics for beginners and dive into advanced options for more complex use cases.


Why Compress and Archive Files?

Compressing and archiving files has several benefits:

  • Save Disk Space: Reduce the size of files to save storage.
  • Easier File Transfers: Combine multiple files into a single archive for easier sharing.
  • Backup and Restore: Create archives for backups that can be restored later.

Linux provides powerful tools like tar, gzip, and zip to handle these tasks. Let’s explore each tool in detail.


Using tar for Archiving

The tar command is one of the most commonly used tools for creating archives. It doesn’t compress files by itself but is often used in combination with compression tools like gzip or bzip2.

1. Create a tar Archive

To create an archive:

1
tar -cvf archive.tar file1 file2 directory/

Explanation:

  • -c: Create a new archive.
  • -v: Verbose output (shows the files being added to the archive).
  • -f: Specify the name of the archive file.

Example:

1
tar -cvf my_archive.tar file1.txt file2.txt my_folder/

This creates an archive named my_archive.tar containing file1.txt, file2.txt, and the contents of my_folder/.


2. Extract a tar Archive

To extract files from an archive:

1
tar -xvf archive.tar

Explanation:

  • -x: Extract files from the archive.
  • -v: Verbose output.
  • -f: Specify the archive file name.

Example:

1
tar -xvf my_archive.tar

This extracts all files and directories from my_archive.tar into the current directory.


3. View the Contents of a tar Archive

To list the files in an archive without extracting them:

1
tar -tvf archive.tar

Example:

1
tar -tvf my_archive.tar

This displays the contents of my_archive.tar in a detailed format.


4. Create a Compressed tar Archive

To create a compressed archive using gzip:

1
tar -czvf archive.tar.gz file1 file2 directory/

Explanation:

  • -z: Compress the archive using gzip.

Example:

1
tar -czvf my_archive.tar.gz file1.txt file2.txt my_folder/

This creates a compressed archive named my_archive.tar.gz.


5. Extract a Compressed tar Archive

To extract files from a .tar.gz archive:

1
tar -xzvf archive.tar.gz

Example:

1
tar -xzvf my_archive.tar.gz

This extracts all files and directories from my_archive.tar.gz.


Compressing with gzip

The gzip command compresses individual files to reduce their size. It is often used in combination with tar for archiving and compression.

1. Compress a File

To compress a file:

1
gzip file.txt

Example:

1
gzip my_file.txt

This creates a compressed file named my_file.txt.gz.


2. Decompress a File

To decompress a .gz file:

1
gzip -d file.txt.gz

Example:

1
gzip -d my_file.txt.gz

This restores the original file my_file.txt.


3. Check Compression Ratio

To see how much space was saved during compression:

1
gzip -l file.txt.gz

Example:

1
gzip -l my_file.txt.gz

This displays the original size, compressed size, and compression ratio.


Using zip for Compression and Archiving

The zip command combines compression and archiving into a single step, making it a convenient tool for managing files.

1. Create a zip Archive

To create a zip archive:

1
zip archive.zip file1 file2 directory/

Example:

1
zip my_archive.zip file1.txt file2.txt my_folder/

This creates a compressed archive named my_archive.zip.


2. Extract a zip Archive

To extract files from a zip archive:

1
unzip archive.zip

Example:

1
unzip my_archive.zip

This extracts all files and directories from my_archive.zip.


3. List the Contents of a zip Archive

To view the contents of a zip archive without extracting:

1
unzip -l archive.zip

Example:

1
unzip -l my_archive.zip

This displays the files and directories inside my_archive.zip.


4. Add Files to an Existing zip Archive

To add files to an existing zip archive:

1
zip archive.zip new_file

Example:

1
zip my_archive.zip new_file.txt

This adds new_file.txt to my_archive.zip.


Comparison of Tools

Here’s a quick comparison of tar, gzip, and zip to help you choose the right tool for your needs:

ToolPurposeCompressionArchivingCommon Use Case
tarArchiving files and directoriesNoYesCreating backups or combining files
gzipCompressing individual filesYesNoReducing the size of large files
zipCompression and archivingYesYesSharing files as a single compressed file

Advanced Tips and Tricks

1. Exclude Files from a tar Archive

To exclude specific files or directories:

1
tar --exclude="file_to_exclude" -cvf archive.tar directory/

Example:

1
tar --exclude="*.log" -cvf my_archive.tar my_folder/

This excludes all .log files from the archive.


2. Compress Files with Maximum Efficiency

Use bzip2 or xz for higher compression ratios:

1
2
tar -cjvf archive.tar.bz2 file1 file2
tar -cJvf archive.tar.xz file1 file2

3. Split Large Archives into Smaller Parts

To split a large archive into smaller chunks:

1
tar -cvf - large_folder/ | split -b 100M - archive_part_

This splits the archive into 100MB chunks.


Conclusion

By mastering tar, gzip, and zip, you can efficiently manage files in Linux. These tools are essential for system administrators, developers, and anyone working with large amounts of data. Whether you’re creating backups, saving disk space, or sharing files, these commands will make your life easier.

Happy compressing and extracting!

This post is licensed under CC BY 4.0 by the author.