How to Compress a Directory Full of Files Using Tar and Gzip on the Terminal in Ubuntu
This is an article about how to compress directories full of files using tar and gzip commands from the terminal in Ubuntu Linux. In this article, you will find information about utilizing the tar command for archiving and then applying gzip compression to produce highly compressed archive files suitable for storage or transfer.
Introduction to Tar and Gzip
When working with large volumes of data on an Ubuntu system, managing disk space efficiently becomes critical. The Unix/Linux shell provides powerful tools like tar (Tape ARchive) and gzip to handle the task of archiving and compressing files efficiently. In this article, we’ll explore how to use these commands together to create compressed archives from directories.
Why Use Tar?
Before diving into how tar works with gzip, it’s important to understand what tar is designed to do:
- Archiving: tar creates a single file containing the contents of multiple files and/or directories.
- Preservation of File Permissions: It maintains file permissions within the archive, which can be crucial when restoring archives on servers.
Why Use Gzip?
Gzip (GNU Zip) is an efficient lossless data compression utility. When used with tar, it significantly reduces the size of archived files while preserving all contained data intact.
- Compression Ratio: Typically achieves a better compression ratio compared to other utilities like zip.
- Compatibility: Widely supported and compatible across different Unix-like systems including Linux, macOS, and BSD.
Preparing Your Environment
Before starting, ensure that your Ubuntu system has the necessary tools installed:
- Open Terminal: You can do this by pressing Ctrl+Alt+T.
- Verify tar Installation:which tar
- Verify gzip Installation:which gzip
If either command does not return a path, you might need to install them using the package manager:
Basic Tar Usage
To create an uncompressed archive of a directory named documents, use the following command:
- c: Create a new archive.
- v: Verbosely show progress while creating or extracting.
- f: Specify the filename to write/read.
Adding Gzip Compression
To compress files with gzip, you can use the -z option with tar:
Here:
- c: Create an archive.
- z: Filter the archive through gzip.
- v: Verbosely show progress.
- f: Specify the filename.
This command creates a gzip-compressed .tar.gz file named documents.tar.gz.
Using Bzip2 for Better Compression
For even better compression, consider using bzip2 with tar. The -j option compresses files through bzip2, which often results in smaller archives than gzip:
Here:
- c: Create an archive.
- j: Filter the archive through bzip2.
- v: Verbosely show progress.
- f: Specify the filename.
This creates a .tar.bz2 file, which typically offers better compression but at the expense of slower processing time compared to gzip.
Extracting Tar Files
To extract an existing tarball back into its original directory structure:
- x: Extract files from an archive.
- z: Filter through gzip for .gz archives.
- v: Verbosely show progress.
For bzip2-compressed files, use:
Advanced Options and Tips
Creating Sparse Files
If your directory contains sparse files (files with large blocks of zeros), you can create a sparse archive to save space:
The --sparse option tells tar to only store non-zero data in the compressed file, which is beneficial for very large and sparsely populated files.
Compressing Files Outside of Directories
Sometimes you might want to compress individual files instead of directories. You can specify each file directly:
This creates a tarball named files.tar.gz containing the specified files.
Specifying Compression Level
By default, gzip uses -6, but you can adjust it if needed. For higher compression ratios (and slower speeds), use:
The --options parameter allows specifying additional options directly to the compression utility.
Listing Contents Without Extracting
To list the contents of a tarball without extracting it:
- t: List the contents.
- z: Handle .gz files.
This is useful for checking if a file exists in an archive or viewing directory structure before extraction.
Conclusion
In this article, you learned how to use tar and gzip together to create compressed archives from directories on Ubuntu Linux. Whether it’s creating basic tarballs, applying gzip compression, or exploring advanced options like sparse files and custom compression levels, the combination of these tools provides a powerful way to manage large data sets efficiently.
With practice, you’ll find that mastering tar and gzip can save significant amounts of disk space and bandwidth when transferring archives. For more detailed information about these commands, refer to their manual pages by running:
Happy archiving!
Last Modified: 23/03/2018 - 03:26:37