How to tar a Folder in Linux/Ubuntu?


In Linux/Ubuntu, the Tar (tape archive or tarball) is a popular archiving utility widely used to compress files and directories into compressed archives. Software is typically available on the internet in the form of compressed folders, for example, tar.gz, zip, .deb, etc. There are many types of tar archive formats, for example, tar, tar.gz, tar.xz, tar.bz2, tar.tbz, etc.

This article will explain the following methods of dealing with Tar Folders in Linux/Ubuntu.

 

How to Tar a Folder in Linux/Ubuntu Via CLI?

The tar command is the primary utility to tar a folder in Linux/Ubuntu. It’s various use cases are discussed here:

Use Case 1: How to Tar a Folder in Linux/Ubuntu?

The tar command is used to organize folders/files in an archive and manipulate archives. A folder is compressed to a tar archive via the tar command. The tar command line utility comes pre-installed in Linux/Ubuntu systems. The general syntax of creating a Tar folder is as follows:

tar cf [File_name.tar] [Directory/Folder to Archive]

Where:

  • c: Creates a new archive.
  • f: Write to the archive.

In the following sections, the creation of a tar folder and a tar.gz folder will be demonstrated.

How to Create a .tar Archive in Linux/Ubuntu?

To create a Tar archive “TarArchive.tar” from a “Screenshots” folder via CLI, execute the following command:

tar cf TarArchive.tar Screenshots

The folder “Screenshots” is compressed to the TarArchive.tar archive.

How to Tar Multiple Folders?

To create a tar folder “TarArchive.tar” from multiple folders: Folder1, Folder2, Folder3, execute the following command:

tar cf TarArchive.tar Folder1 Folder2 Folder3

The folders “Folder1”, “Folder2”, and “Folder3” are compressed to the TarArchive.tar archive.

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

The “tar.gz” is a popular compressed archive file format that utilizes the “gzip” compression algorithm to compress data. A folder is compressed to a tar.gz archive via the tar command.

For example, use the “z” flag with the tar command to compress the folder “Screenshots” to a tar.gz archive “TargzArchive.tar.gz” by executing the following command:

​​tar czf TargzArchive.tar.gz Screenshots

Where ‘z’ creates the archive using “gzip”.

The folder “Screenshots” is compressed to the TargzArchive.tar.gz archive.

Use Case 2: How to View a Tar Folder?

To view the contents of the Archive “TarArchive.tar”, run the following command:


The above output lists the contents of the TarArchive.tar folder.

Use Case 3: How to Append a Tar Folder?

To append three files: Screenshot011.png, Screenshot012.png, and Screenshot013.png to a tar archive “TargzArchive.tar.gz”, and execute the following command:

tar -rvf TarArchive.tar Screenshot011.png Screenshot012.png Screenshot013.png

Where ‘r’ appends the files to a tar archive.

Alternatively, multiple files can be appended by using the wildcard character ‘*’ as demonstrated below:

tar -rvf TarArchive.tar *.png

Where *.png indicates all the .png files in the current directory.

The files: Screenshot011.png, Screenshot012.png, and Screenshot013.png are appended in the TarArchive.tar folder.

Use Case 4: How to Extract a Tar Folder in Linux/Ubuntu?

Folders are compressed to conserve space and optimize transmission bandwidth. When the contents of the compressed folder are required, the compressed folder is extracted.

In the following sections, the extraction of tar and tar.gz archives will be demonstrated.

How to Extract/Unzip a .tar Archive in Linux/Ubuntu?

To extract a tar archive “TarArchive.tar”, execute the following command:


Where “x” extracts files from the tar Archive.

The above output verifies that the archive TarArchive.tar is extracted.

How to Extract/Unzip a tar.gz Archive in Linux/Ubuntu?

To extract a tar.gz archive “TargzArchive.tar.gz”, run the following command:

tar xzf TargzArchive.tar.gz

Where “z” constructs the extracted archive using “gzip”.

The archive TargzArchive.tar.gz is extracted to Screenshots Directory.

Use Case 5: How to Extract a Specific Folder/File From a Tar Folder?

There are cases when a user requires only a specific file or a folder and not the entire archive. In the following sections, the extraction of tar and tar.gz archives will be demonstrated.

How to Extract a Specific Folder/File from a .tar Archive?

To extract a specific folder “Folder3” from a tar archive “TarArchive.tar”, execute the following command:

tar xvf TarArchive.tar Folder1/Folder3

The above output indicates that only Folder3 is extracted.

Similarly, to extract a file Screenshots012.png and a file “Screenshot001.png” inside the Screenshots Folder, execute the following command:

tar xvf TarArchive.tar Screenshots/Screenshot001.png Screenshot012.png

The above output indicates that two files are extracted, i.e., Screenshots012.png and Screenshot001.png.

How to Extract a Specific Folder/File from a tar.gz Archive?

To extract specific files from a tar.gz archive, run the following command:

tar xzvf TargzArchive.tar.gz Screenshots/Screenshot005.png Screenshots/Screenshot007.png

Where the flag “z” extracts the archive TargzArchive.tar.gz using gzip.

The above output indicates that only the files “screenshot005.png” and “screenshot007png2 are placed inside the “Screenshots” folder are extracted.

 

How to Tar a Folder in Linux/Ubuntu Via GUI?

Users who are new to Linux are hesitant to use the Command Line. In such scenarios, users can utilize the GUI (Graphic User Interface) with ease to work with Tar Folders.

 

How to Tar a Folder in Ubuntu 22.04?

In Ubuntu 22.04, there are only four (4) options to compress a Folder, i.e., zip, password-protected zip, tar.xz, and 7z. Among these options, tar.xz is the only tar-based compressed archive file format.

To compress a folder to a tar.xz archive, first navigate to the path of the folder to be compressed. Next, select the folder and right-click via mouse or keyboard. Next, choose “Compress” from the drop-down list:

A “Create Archive” pop-up window will be opened. Type the name in the “Tar Archive” tab, then, select the tar.xz from the drop-down and press the “Create” button:

This will create a Tar archive “TarArchive.tar.xz” as shown below:

 

How to View a Tar Folder in Linux/Ubuntu?

To view the contents of a Tar Folder, double-click TarArchive.tar.xz to access the contents as shown below:

 

How to Extract/Unzip a Tar Folder in Linux/Ubuntu?

To extract a tar folder TarArchive.tar.xz, first navigate to the path of the folder to be extracted. Next, select the Tar folder and right-click via mouse or keyboard. Then, select “Extract Here” from the drop-down menu:

This will create a folder “TarArchive” as shown below:

Alternatively, a Tar folder can also be extracted by double-clicking and pressing the extract button:

Conclusion

To compress the data to tar and tar.gz archives, run the “tar cf <archive.tar> <Directory>” and “tar czf <archive.tar.gz> <Directory>” commands respectively. Additionally, a user can view, append, and extract the contents of a tar archive via CLI (Command Line Utility) and GUI (Graphic User Interface). This article explained different ways of dealing with a tar archive in Linux/Ubuntu.

Print Friendly, PDF & Email