The process of compressing files reduces the amount of size of files and helps deliver email attachments or transfer these files efficiently. We can also use the compressed files and folders for backup, and use it at the time of need. Ubuntu 22.04 provides the tar command as a utility for compressing files and folders. 

In this article, we’ll explain how you can compress a folder with tar.

How to Compress a Folder With Tar? 

Folder compression is very important when it has different types of files. It is good practice to compress all the necessary folders for backup. If you are using Ubuntu 22.04 then you can use the tar command to compress your folders. 

Follow one of these methods to compress the folder:

  • Using tar -zcvf gzip
  • Using tar -jcvf bzip2
  • Using tar –zstd Command

Method 1: Compressing a Folder by Using tar -zcvf gzip

Tar is an archive management system that works under GNU. The -zcvf flag is a compression algorithm used along with a tar command to compress folders. If you want to compress a folder or directory you can use the tar command with -zcvf and follow the steps described below:

Step 1: Compress the Folder

Open the terminal by pressing “Ctrl + Alt + t” and type the following command in the terminal:

$ tar -zcvf  "<your archive-name.tar.gz>" "<your directory name>"

The execution of the command is shown in the diagram, in this command “MyCompressedFiles.tar.gz” is my archive name, and “MyFile” is the target folder which I want to compress:

The command will compress the folder with the tar.gz format. A compressed folder will be created in your selected location (in our case, it’s home directory):

Step 2: Extract the Folder

If you want to extract the files from the compressed folder, use the “tar” command with the “-zxvf” flags, as shown below:

$ tar -zxvf "<name.tar.gz>" -C "<Your target folder>"

Here in this command:

  • “-zxvf” is used to extract files.
  • Next, we specify the name of the folder that needs to be extracted. 
  • “-C” is used to copy the extracted files to the specified directory:

Step 3: Compress Multiple Folders

There is also a command available for compressing multiple folders. Type the following command and provide a space between the names of the folders: 

$ tar -zcvf "<your archive_name.tar.gz>" "<folder_1 folder_2 folder_3>"
  • You’ll write a tar command with the -zcvf flag which compresses the folder
  • Write the archive name, in our case, it is “mulitpleDir.tar.gz
  • Then you’ll write the folder names and provide blank space between each folder name

Method 2: Compressing a Folder by Using tar -jcvf bzip2

In this method, the tar command uses the -jcvf flag to compress the folder. You can also compress a folder by using tar bzip2 compression instead of gzip.

Step 1: Compress the Folder

Execute the following command in the terminal to compress the folder:

$ tar -jcvf "<archive_name.tar.bz2>" "<target folder>"
  • You’ll write the tar command with the -jcvf flag
  • Next, write the archive name, in our case, it is”linways.tar.bz2
  • Then write the name of the folder you want to compress, in our case, it is “myFiles03

Step 2: Extract the Folder 

Now if you want to extract the folder, type the following command in the terminal:

$ tar -jxvf "<name of the archive>" -C "<name of the folder for extraction>"

The command will extract the folder:

Method 3: Compressing a Folder Using tar –zstd Command

You can also use the –zstd command with sudo permissions if you want to compress the folder and files. It is a compression method developed by Facebook for better speed and compression ratio. 

Step 1: Compress the Folder

Use the following command for compression:

$ sudo tar -zstd -cf "<archive_name>" "<target folder>"

The command will compress the folder with the tar zst extension:

Step 2: Extract the Folder

The extraction method is slightly different than other ones. In this method, we use two steps:

  • First, we decompress the zst to tar
  • Then we extract the tar folder 

Use the following command to decompress zst to tar format:

$ sudo unzstd "<archive name.tar.zst>"

The command will create a file in tar format as shown in the diagram:

Now we will extract the tar format by using the following command:

$ tar -xvf "<archive name>" -C "<name of folder for extracted files>"

The command will extract the files in the folder:

By using, these methods you can compress the folders with the tar command.

Conclusion

To compress the folder with tar, you can use -zcvf with gzip compression, -jcvf bzip2 compression, and -zstd zst compression. The process of compression is widely used when the folder is being transferred having different files. It reduces the sizes of folders as well as it reduces the chance of losing files from the folders. In this article, we’ve discussed, how you can compress a folder using different methods in Ubuntu 22.04.

Frequently Asked Questions

What is the importance of compressing a folder with tar?

Compressing a folder with tar reduces file size, making it easier to transfer files efficiently and use them for backup.

How can I compress a folder in Ubuntu 22.04 using the tar command?

In Ubuntu 22.04, you can compress a folder using the tar command with options like -zcvf for gzip compression, -jcvf for bzip2 compression, or --zstd for zstd compression.

What is the significance of using the tar -zcvf command for folder compression?

The tar -zcvf command uses gzip compression to compress folders efficiently, making it a suitable choice for creating compressed archives.

How do I compress a folder using tar -zcvf in Ubuntu?

To compress a folder in Ubuntu using tar -zcvf, open the terminal, run the command 'tar -zcvf <archive-name.tar.gz> <directory-name>', and the compressed folder will be created in the specified location.

Can I extract files from a compressed folder created with tar -zcvf?

Yes, you can extract files from a compressed folder created using tar -zcvf by using appropriate extraction commands in the terminal.

What are the steps to compress a folder with tar in Ubuntu?

To compress a folder with tar in Ubuntu, you need to open the terminal, use the tar command with the desired options, and specify the archive name and folder to be compressed.

How does the tar command help in compressing folders?

The tar command in Ubuntu allows users to compress folders by utilizing different compression algorithms like gzip, bzip2, or zstd to reduce file size and create compressed archives.

Where can I find the compressed folder after using tar -zcvf in Ubuntu?

After using tar -zcvf to compress a folder in Ubuntu, the compressed folder will be created in the specified location, such as the home directory or the location you designated in the command.