Skip to main content
In this lesson, we continue our journey into the Linux shell by exploring file compression and archival techniques. You’ll learn about several tools for compressing and archiving files, methods for searching files and directories, and basic pattern matching in Linux. In addition, there is an interactive lab that allows you to experiment with the VI Editor.
The image lists Linux Core Concepts, including file compression, data extraction, and text editors, with associated labs for practical learning.
Before diving into file compression and archival commands, let’s start by checking the size of a file in Linux. The du command (short for disk usage) is widely used to determine file sizes. :::note Viewing File Sizes To display a file size in kilobytes, use the following command: :::
For a more user-friendly, human-readable format:
Alternatively, you can use the ls -lh command to view file sizes in a human-readable format:

Using tar for Archival

The tar command is one of the most common utilities used to group multiple files or directories into a single archive file (tarball). This is particularly useful for data backup, transfer, and archiving. To create a tar archive, use the -c option to create the archive and the -f option to specify the file name:
You can list the contents of a tarball using:
To extract files from a tar archive, use the -x option:
Additionally, to compress the tarball and reduce its file size, include the -z option:

Compression Commands in Linux

Linux offers several commands for compressing files. The choice of command depends on the type of data and required compression level. Below are examples using bzip2, gzip, and xz: Each command appends a specific file extension indicating the compression format: .bz2 for bzip2, .gz for gzip, and .xz for xz. :::note Decompressing Files To uncompress these files, use the following commands:
  • For bzip2: bunzip2
  • For gzip: gunzip
  • For xz: unxz :::
For example, here is how you decompress each file type:
It is important to note that you do not always need to fully uncompress a file to view its content. Tools like zcat, bzcat, and xzcat allow you to read compressed files without manual decompression.

Conclusion

This lesson has provided an overview of file compression and archival in Linux. Mastering these techniques will enhance your file management and storage efficiency. Additionally, exploring interactive labs with the VI Editor serves as a practical complement to these technical skills. For further learning, consider exploring additional documentation on Linux file management and archival strategies.

Watch Video