Home Linux Commandline Tips How to Create and Manage a .tar.bz2 File in Linux

How to Create and Manage a .tar.bz2 File in Linux

The Linux operating system has transformed file archiving into a free, reputable, reliable, and secure file management process. By archiving your file(s) under a Linux operating system environment, you guarantee its integrity and security. Your files become free of compromise from both users and applications active in the system.

To create tar files on a Linux OS environment, we need to reference the usage of the tar command. In summary, the tar command can achieve the following objectives as a file archiving utility:

  • Create tar files.
  • Extract tar archives from existing/created tar files.
  • List the content of an existing/created tar archive(s).
  • Update existing/created tar archive with more files.
  • Removing files from existing/created tar archive.

When you create a tar (Tape Archive) file from the Linux command line environment, you will either result in a tar file with the extension .tar.gz or .tar.bz2. When compressing a tar file, the tool you use for the compression process determines the final tar file name.

For instance, Using GZIP compression tool results in a tar file name with the extension .tar.gz , and using the BZIP2 compression tool results in .tar.bz2 file name extension.

[ You might also like: How to Tar Specific File Types (Extensions) in a Directory ]

GZIP is faster than BZIP2 in its compression and decompression routines but BZIP2 leads to smaller compression (15% decrease in compression).

This article guide will walk us through creating and managing .tar.bz2 file archives on a Linux operating system environment.

Creating a tar.bz2 File in Linux

For this tutorial, we will need access to some reference files for archiving into a .tar.bz2 file archive.

List Files in Linux
List Files in Linux

The tar command general syntax is as follows:

$ tar [OPERATION_AND_OPTIONS] [ARCHIVE_NAME] [FILE_NAME(s)]

From the general tar syntax reference above, to create a .tar.bz2 file archive, an ideal syntax will look like the following:

$ tar -cjf ARCHIVE_NAME.tar.bz2 [FILE_NAMES] 

The command option -c initiates the archive file creation process, -j specifies that the file archive should have a .tar.bz2b extension, and -f instructs the whole tar operation to use the device archive.

For instance, if we reference the above screen capture, the LinuxShellTips_Files directory hosts several files and folders. To archive the images directory together with the file_1.txt and file_2.txt files, our tar command will be as follows:

$ tar -cjf images_and_files.tar.bz2 /home/dnyce/LinuxShellTips_Files/images /home/dnyce/LinuxShellTips_Files/file_1.txt /home/dnyce/LinuxShellTips_Files/file_2.txt
Create Tar File in Linux
Create Tar File in Linux

To list the content present in this tar file, we will use the -t option and run:

$ tar -tf images_and_files.tar.bz2
List Tar Files in Linux
List Tar Files in Linux

For more details on the above output like file owner and file size, use -v (–verbose) option:

$ tar -tvf images_and_files.tar.bz2
List Tar File Sizes and Ownership
List Tar File Sizes and Ownership

To extract the archive file, include the -x option.

$ tar -xf images_and_files.tar.bz2

To extract the archive file to a specific directory, include the -C option and path to that directory.

$ tar -xf images_and_files.tar.bz2 -C /home/dnyce/linuxshelltips/decompressed 

To add a file to the existing tar archive, use the -r option for --append operations.

$ tar -rvf images_and_files.tar.bz2 /home/dnyce/LinuxShellTips_Files/file_3.txt
Add Files to Tar Archive
Add Files to Tar Archive

We have successfully learned how to create and manage .tar.bz2 archive files in a Linux OS environment.

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.