In this chapter, we’ll explore how to archive and compress files in Linux. Archiving combines multiple files into a single file, while compression reduces the size of files to save disk space and speed up file transfers. By the end of this chapter, you’ll be able to create archives, compress files, and extract them using various tools like tar, gzip, bzip2, xz, zip, and modern tools like zstd.
File archiving and compression are essential skills for:
- Saving Disk Space: Compressed files take up less space.
- Organizing Files: Archives combine multiple files into one for easier management. - Faster Transfers: Compressed files transfer faster over networks.
- Backups: Archiving is often used to create backups of important data.
tarThe tar command is used to create and extract archive files. It’s one of the most commonly used tools for archiving in Linux.
To create an archive (also called a tarball), use the -c(create) option:
$ tar -cvf archive_name.tar file1 file2 directory/-c: Create an archive.-v: Verbose mode (shows progress).-f: Specify the archive file name.To extract files from a tarball, use the -x (extract) option:
$ tar -xvf archive_name.tarTo list the contents of an archive without extracting it, use the -t (list) option:
$ tar -tvf archive_name.targzip, bzip2, and xzCompression tools reduce the size of files or archives. Here’s how to use the most common ones:
gzip is a fast and widely used compression tool.
Compress a File: $ gzip file.txt This creates file.txt.gz.
Decompress a File:$ gunzip file.txt.gz
Compress a Tar Archive:$ tar -cvzf archive_name.tar.gz directory/
-z: Use gzip for compression.bzip2 provides better compression than gzip but is slower.
Compress a File: $ bzip2 file.txt This creates file.txt.bz2.
Decompress a File:$ bunzip2 file.txt.bz2
Compress a Tar Archive:$ tar -cvjf archive_name.tar.bz2 directory/
-j: Use bzip2 for compression.xz offers even better compression than bzip2 but is slower.
Compress a File: $ xz file.txt This creates file.txt.xz.
Decompress a File: $ unxz file.txt.xz
Compress a Tar Archive:$ tar -cvJf archive_name.tar.xz directory/
-J: Use xz for compression.zip and unzipThe zip and unzip commands are commonly used for creating and extracting .zip files, which are widely supported across operating systems.
To create a .zip file:
$ zip archive_name.zip file1 file2 directory/To extract a .zip file:
$ unzip archive_name.zipTo list the contents of a .zip file without extracting it:
$ unzip -l archive_name.zipzstdzstd (Zstandard) is a modern compression tool that offers high compression ratios and fast speeds.
To compress a file with zstd:
$ zstd file.txtThis creates file.txt.zst.
To decompress a .zst file:
$ unzstd file.txt.zstTo create a .tar.zst archive:
$ tar -cvf - directory/ | zstd > archive_name.tar.zstTo create a compressed backup of a directory:
$ tar -cvzf backup.tar.gz /path/to/directory/To extract a .tar.gz archive:
$ tar -xvzf backup.tar.gzTo compress all .log files in a directory:
$ gzip *.logTo create a .zip archive for sharing with Windows users:
$ zip -r archive_name.zip directory/zstd for Fast CompressionTo compress a large file quickly with zstd:
$ zstd -3 large_file.txtModern compression tools like zstd and brotli offer better compression ratios and speed compared to traditional formats. Zstd is particularly useful for quickly compressing large files, such as logs or backups.
For added security, encrypting archives with gpg ensures data protection during storage or transfer. These modern approaches allow for efficient data management, especially in scenarios with limited bandwidth or storage.
$ zstd file.txt
$ gpg -c archive.tar.gzLet’s put your new skills to the test:
1. Create a .tar.gzarchive of a directory and extract it.
2. Compress a file using bzip2 and then decompress it.
3. Create a .ziparchive and share it with a friend.
4. Use zstd to compress a large file and compare the compression speed and ratio with gzip.
File archiving and compression are essential skills for managing files efficiently in Linux. Whether you’re creating backups, saving disk space, or sharing files, tools like tar, gzip, bzip2, xz, zip, and zstdmake these tasks easy and effective. By mastering these tools, you’ll be well-equipped to handle file management like a pro.