Commands.page Logo

How to Convert Tar.gz to Tar.bz2 in Ubuntu Linux

This article provides a quick guide on changing archive compression formats within the Ubuntu operating system. It explains that there is no single direct command for this task, but rather a process involving extraction and recompression using the tar utility. You will learn the specific flags needed to handle gzip and bzip2 formats safely and efficiently via the terminal.

There is no single command that directly converts a .tar.gz file to a .tar.bz2 file without intermediate steps. The process requires you to extract the contents of the original archive and then compress those contents into the new format. You can achieve this using the tar command built into Ubuntu.

To begin, open your terminal and navigate to the directory containing your file. First, extract the .tar.gz archive using the following command:

tar -xzf filename.tar.gz

The -x flag extracts files, -z handles the gzip compression, and -f specifies the filename. This will create a folder or set of files in your current directory.

Next, compress the extracted content into the bzip2 format. Use this command:

tar -cjf filename.tar.bz2 extracted_folder_name

In this command, -c creates a new archive, -j handles bzip2 compression, and -f defines the new filename. Replace extracted_folder_name with the actual name of the directory created during extraction.

Once you have verified the new .tar.bz2 file exists, you can clean up the original files to save space. Remove the extracted folder and the original archive with these commands:

rm -rf extracted_folder_name
rm filename.tar.gz

For advanced users who wish to avoid creating intermediate files on the disk, you can pipe the output directly. However, the two-step method above is recommended for ensuring data integrity and preserving directory structures correctly.