How to Create and Extract tar.gz and tar.bz2 files on Ubuntu / Linux

By | May 1, 2023

The most common compressed archive file format in Linux is the tar.gz format. Tar file is an archive file format. Tar.gz is a compressed tar file.

Create tar.gz archives

The tar command can be used to create tar.gz archives.

tar czf new-tar-file-name.tar.gz file-or-folder-to-archive

Here is the command explanation:

  • c - create new archive.
  • z - compress the archive using gzip.
  • f - use archive file.
  • new-tar-file-name.tar.gz - the name of the tar.gz to create.
  • file-or-folder-to-archive - the name of the folder we want to archive.

Adding multiple directories

tar -czf new-tar-file-name.tar.gz file1 file2 folder1 folder2

Extract tar archives

gz archives

tar -xzf tar-file-name.tar.gz

Here is the command explanation:

  • x - extract the archive.
  • z - uncompress the archive using gzip.
  • f - use archive file.
  • tar-file-name.tar.gz - the name of the tar.gz to create.

The tar command will extract all the files/folders in the archive to the current directory. To extract to specific location modify the command as follows

tar -xzf tar-file-name.tar.gz /path/to/location

bz2 archives

Extracting tar.bz2 (bzip2 file) is very similar to the way you extract tar.gz file. Instead of using the -z flag you need to use the -j flag for the bzip2 format

tar -xvjf tar-file-name.tar.gz

Here is the command explanation:

  • x - extract the archive.
  • v - verbose. show information as files are being extracted.
  • j - filter the archive through bzip2
  • f - use archive file.
  • tar-file-name.tar.gz - the name of the tar.gz to create.

The tar command will extract all the files/folders in the archive to the current directory.

Check out the following tutorial to learn more:

10 "tar" Command Examples in Linux to Create and Extract Archives
About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].

2 Comments

How to Create and Extract tar.gz and tar.bz2 files on Ubuntu / Linux

Leave a Reply

Your email address will not be published. Required fields are marked *