How to Compress a Directory Full of Files Using Tar and Bzip2 on the Terminal in Ubuntu
This is an article about compressing directories using two powerful command-line tools: tar (Tape Archive) and bzip2. In this article, you will find information about how to create compressed archives of your files and directories, making it easier for backups, transferring large amounts of data, or simply keeping your hard drive organized.
In this tutorial, we’ll focus on two key components:
- Tar: A utility that gathers multiple files into a single archive file.
- Bzip2: A powerful compression tool used to reduce the size of tar-created archives even further.
Read this article to find out about how these tools work together and what benefits they offer when managing your Ubuntu system’s data storage needs.
What is Tar?
Tar stands for Tape ARchive, a command-line utility that was originally developed to create backups on magnetic tape drives. Today, tar can be used in any environment where you need to manage multiple files as one single archive file. It has numerous options and switches that allow users to extract or manipulate archives from the terminal.
What is Bzip2?
Bzip2 is a data compression program using the Burrows-Wheeler algorithm, combined with Huffman coding for encoding. It compresses files significantly more efficiently than gzip, which makes it ideal when working with tar files since you can create highly compact archives of large directories full of multiple files.
Why Use Tar and Bzip2 Together?
The combination of tar and bzip2 is one of the most efficient ways to archive and compress your files on a Linux-based system like Ubuntu. While tar can gather numerous files and directories into a single file, using bzip2 allows you to further reduce the size of this archive by applying advanced compression techniques. This makes it easier to store backups or share large datasets over network connections.
Prerequisites
Before diving in, ensure that your Ubuntu machine has both tar and bzip2 installed. These tools are usually pre-installed on most Ubuntu systems. However, if you don’t have them, you can install them using the following command:
Step-by-Step Guide
Let’s go through a step-by-step guide to compressing a directory with tar and bzip2.
1. Navigate to Your Directory
First, navigate to the directory that contains all of your files or folders which you wish to compress. You can use the cd command to change directories.
2. Create a Tar Archive
Once inside the directory containing the files and folders to be compressed, we’ll create a tar archive of these items. By default, tar just creates an uncompressed file, but you can use other options for various purposes.
- -c: This option tells tar to create an archive.
- -v: The verbose mode which displays files being processed.
- -f: Specifies the name of the output file (in this case backup.tar).
The period (.) at the end indicates that tar should include all contents of the current directory.
3. Compress with Bzip2
After creating an uncompressed tar archive, we’ll compress it using bzip2 to save space and improve transfer efficiency.
This will result in a new file called backup.tar.bz2. The .bz2 extension signifies that the original tar file has been compressed with bzip2.
4. Verify Your Compressed Archive
To verify your compressed archive, you can use both tar and bzip2 commands to check for integrity and content details.
This command will display detailed information about the file size of the newly created archive.
5. Extracting Your Archive
When it’s time to decompress your tar-bzipped files, you need to reverse both operations:
Firstly, extract the bzip2-compressed tarball with bunzip2, and then use tar to unpack the contents.
This will leave you with a plain tar file called backup.tar.
Next, extract it using the x option (extract):
- -x: Extracts files from an archive.
- -v: As before, this makes the command output what’s happening on-screen.
Advanced Options and Techniques
Compressing Directly with Bzip2
You can compress while creating a tarball directly in one step using tar:
This command uses -c, -v, and -f options for tar to create an archive with standard output (-) and pipe it into bzip2.
Specifying Compression Level
Bzip2 offers different compression levels. The default level is 6, but you can adjust this if desired:
Here -9 specifies the highest compression level (slowest), whereas -1 would be the lowest.
Compressing Without Temporary Files
To avoid creating temporary tar files before compressing them, you can use:
This command pipes the tar creation directly into the compression process without writing to disk first.
Common Errors and Solutions
Incorrect Permissions or Ownership
If you encounter permission issues when trying to compress or decompress files, make sure that you have the necessary permissions. You may need to use sudo for administrative tasks:
Missing Files in Extraction
If your extracted tarball does not contain all expected files, it could be because of file permissions or an incomplete archive. Verify the contents before extraction:
Conclusion
In this article, you’ve learned how to create compressed backups using tar and bzip2 on Ubuntu. This powerful combination offers a flexible way of managing large volumes of data efficiently.
Whether you’re backing up important files or preparing for transfer over limited bandwidth connections, mastering these commands will significantly enhance your Linux administration capabilities.
By following the steps outlined above, users can effectively create compressed archives, ensuring that their file backups are both compact and secure. This knowledge is invaluable when dealing with large datasets or when implementing efficient storage solutions on your Ubuntu system.
Last Modified: 23/03/2018 - 06:18:51