Linux Commands Ubuntu

How to Create a tar File in Linux/Ubuntu?

How to Create a tar File in Linux Ubuntu

Data compression is done to optimize data storage on storage mediums like SSD, Cloud, etc. Data compression results in larger storage availability, cost savings due to larger space availability, and faster data transfer rates.

Tar is a command line tool that handles a variety of compression formats like “.gz”, “.xz”, “.tbz”, “.bz2” etc. This article will demonstrate the creation of three tar compression formats on Linux/Ubuntu 22.04. LTS systems.

How to Create a tar.gz File in Linux/Ubuntu?

The “tar.gz” is a compressed archive file format for Linux systems that uses the “gzip” compression algorithm.

Consider that we have a set of five text files: “File1.txt, File2.txt, File3.txt, File4.txt, and File5.txt” in a directory that needs to be compressed as shown below:

$ ls -l

We will run the following command to compress these five files to create a tar.gz compressed archive:

$ tar czf archive.tar.gz *.txt

The above command comprises of:

  • tar: tar command line tool
  • c: Creates a new archive.
  • z: Constructs the compressed archive using “gzip”.
  • f: file
  • archive.tar.gz: Name of the archive file.
  • *.txt: Select all the .txt files in the present working directory.

We can verify the created archive.tar.gz archive by using the following ls command:

$ ls -l

From the below image, we can see that a .tar.gz compressed archive “archieve.tar.gz” is created.

How to Create a tar.xz File in Linux/Ubuntu?

The “tar.xz” is a compressed archive file format for Linux systems that uses LZMA/LZMA2 compression algorithms.

Consider that we have a set of five text files: “File1.txt, File2.txt, File3.txt, File4.txt, and File5.txt” in a directory that needs to be compressed as shown below:

We will run the following command to compress these five files into a tar.xz compressed archive:

$ tar cfJ archive2.tar.xz *.txt

The above command comprises of:

  • tar: tar command line tool.
  • c: Create a new archive.
  • f: file
  • J: Create the compressed files using “.xz”.
  • archive2.tar.xz: Name of the archive file.
  • *.txt: Select all the .txt files in the present working directory.

We will verify the created “archive2.tar.xz” archive via the “ls -l” command:

$ ls -l

From the below image, it can be verified that a .tar.xz compressed archive “archieve2.tar.gz” is created.

How to Create a tar.bz2 File in Linux/Ubuntu?

The “.tar.bz2” is a compressed archive file format for Linux systems that is highly optimized for compression with the BZ2 compression algorithm.

Consider that we have a set of five text files: “File1.txt, File2.txt, File3.txt, File4.txt, and File5.txt” in a directory that needs to be compressed as shown below:

We will run the following command to compress these five files into a tar.bz2 compressed archive:

$ tar -cvjf archive1.tar.bz2 *.txt

The above command comprises of:

  • tar: tar command line tool.
  • c: Create/Make a new archive.
  • v: verbose
  • j: Creates the compressed files using “bz2”.
  • f: File
  • archive1.tar.gz: Name of the archive file.
  • *.txt: Select all the .txt files in the present working directory.

We can verify the created archive1.tar.bz2 archive by using the following command:

$ ls -l

From the below image, it can be verified that a .tar.bz2 compressed archive named “archieve1.tar.gz” is created:

Conclusion

Various compressed archives are created by using the Tar command line tool, such as tar.gz, tar.xz, and tar.bz2. Compressed archives are created using “tar czf <archive_name.tar.gz> <file.txt>”, “tar cfJ <archive_name.tar.xz> <file.txt>”, and “tar -cvjf <archive_name.tar.bz2> <file.txt>” commands respectively. This article has demonstrated the creation of three tar compression formats on Linux/Ubuntu 22.04. LTS systems.

 

Similar Posts