Deprecated: sanitize_url is deprecated since version 2.8.0! Use esc_url_raw() instead. in /var/www/linuxwebdevelopment.com/wp-includes/functions.php on line 5211

How To Compress A File or Folder With Tar In Linux

To compress a file or folder with the tar command is easy in Linux. Read this article to learn about various ways to compress files or folders with tar.

tar is a ubiquitous command in Linux. Most modern Linux distros should come with tar by default.

Now, I will discuss how to compress a file or folder with tar in Linux.

Step 1: Decide what type of tar compression that you want. The compression type will determine which file extension you should use. Do you want a .tar.gz, .tar.bz2, or .tar.xz file?

Each type of tar file is made with a different compression algorithm. .tar.gz files tend to be able to be compressed quickly, but the compression might be worse than the others. That is to say, the compressed tar file would likely be larger.

.tar.xz files in my experience have among the best compression of any tar algorithm. So if saving space is your #1 priority, then .tar.xz would be good. But one downside of .tar.xz is that it tends to be much slower than other compression algorithms.

.tar.bz2 files may be made slightly slower than .tar.gz files, but the compression may be better.

With tar, compressing 1 file, or 1 folder can be done with basically identical tar commands.

How To Compress A Folder or File With Tar

First let’s assume we want to use Gzip compression which would go with .tar.gz files.

If you have a folder named ‘my_folder’ and you wanted to compress it and all of its files you could do something like:

tar cfvz my_folder.tar.gz my_folder

Now, let’s break down the above command. The cfvz are ‘flags’ or special parameters passed to the tar command telling tar exactly what you want to do.

In the ‘cfvz’ part of the command, c stands for compress, f stands for force (we will force the compression), v stands for verbose which will give you information about the folder while it is being compressed, and finally z is the specific flag which specifies that we want to use gzip compression (making a .tar.gz file)

I generally will use cfv every time. If I want to compress a folder and its contents quickly, I’ll use cfvz, and if I want the best possible compression, I’ll use cfvJ

How To Compress A Folder Or File With Tar Using Gzip Compression

The above example is how you would compress a folder with Gzip. Again, the ‘z’ part of ‘cfvz’ is the part that tells the tar command that you want to use Gzip compression.

tar cfvz my_folder.tar.gz my_folder

Compressing a file is basically done the same way as compressing a folder in tar.

To compress a file called my_file.txt with Tar Using gzip compression, you would do:
tar cfvz my_file.tar.gz my_file.txt

How To Compress A Folder Or FIle With Tar Using bzip2 Compression

The command to compress a folder with bzip2 instead of gzip is almost identical. You only need to change the final letter for the flags to a j, and change the file extension of the compressed file you are creating from .tar.gz to .tar.bz2

Instead of using the following

tar cfvz my_folder.tar.gz my_folder

To compress a folder called my_folder with Tar Using bzip2 compression, you would do:

tar cfvj my_folder.tar.bz2 my_folder

To compress a file called my_file.txt with Tar Using bzip2 compression, you would do:

tar cfvj my_file.tar.bz2 my_file.txt

How To Compress A Folder Or FIle With Tar Using xz Compression

Creating a Tar file using xz compression is quite similar to making a tar file using gzip or bzip2 compression.

The flag that indicates that you want xz compression is ‘J’. Note that it is important that you use uppercase J. Lowercase j would indicate bzip2 compression. Additionally for xz, you would name your compressed file ending in .tar.xz.

To compress a folder called my_folder with Tar Using xz compression, you would do:

tar cfvJ my_folder.tar.xz my_folder

To compress a file called my_file.txt with tar using xz compression, you would do:

tar cfvJ my_file.tar.xz my_file.txt

Quick Notes

When I gave examples, such as tar cfvJ my_file.tar.xz my_file.txt
You can name your .tar.xz file whatever you want, and could even create it in a different directory than the current one you may be using.

Permissions

If you are trying to tar a folder or file that needs higher permissions, you might need to use sudo with tar.
For example, if you were compressing a website in /var/www/
You might need to do something like

sudo tar cfvz ~/my_website.tar.gz my_website

Want to learn how to extract a tar file? Then read our article titled How To Extract A Tar File In Linux

What did you think of this article. Do you have anything to add? Let’s discuss it in the comments below.

Posted on Categories LinuxTags

Leave a Reply

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