Everything about Tar
Intro
.tar
,.tar.gz
,.tar.bz2
. Do you know the difference between all of those extensions?
Tar is an archive file which means it is a container for many files. Tar is used to collect many files as a single file.
gz is an abbreviation for Gzip, it is a lossless compression algorithm used to compress a file.
bz2 is an abbreviation for Bzip2, it is also a lossless compression algorithm. The main difference between gzip & bzip2 is the algorithm itself. Gzip can compress a file faster but the size of the compressed file is bigger than Bzip2.
So .tar
extensions mean it is an archived file, whether .tar.bz2
& .tar.gz
means it is a compressed archive file.
Tar Terminal Command
tar command on the terminal has many flags, it intimidated me when I first learn about it. I want to share some of the information regarding the most used flag.

Those flags can be combined when creating a command on the terminal.

c
indicating that we are going to create a tar file from the specified path, f
means give the archived filename ofarchivedFileName.tar
. v
indicating verbose, it will print out the process on the terminal when archiving process going on.

use z
when using Gzip algorithm, use j
when using Bzip2 algorithm.

x
indicate that you are going to untar the files. you can use the same command for both archived and compressed archived files. f
in this case, indicates what file you are going to untar. -C
indicates the path you want your files to be collected after untar process is done. By default untar process will collect your data in the current directory.

t
indicates that you are going to see the data inside the archived files. tvf
inspect the data recursively, which means if there is a folder inside the archived file, it will inspect all the data inside of it. ls -l
inspect the data inside the archived file only. It won’t look recursively inside the folder (if there are any).

you can also extract only certain files (not all of them). The flags are slightly different based on the type of extension.

You can also unarchive multiple files using double-quote syntax.

you can also extract data with certain pattern names using --wildcard
flags.

You can also add data inside the archived files using r
flag. One thing to remember here is that we CAN NOT add data to the compressed archived file. So make sure that you use r
flag only on .tar
files.
Split Feature
There is another feature that works well with the tar command. It is Split
command. It helped us split big data into small pieces chunks. It helps when we are going to send the data over the internet.

Assume we want to send the data inside /app/data
. Supposed that the data is too big so we want to divide it into some pieces of small chunks. So after we compressed the archived file using Bzip2 algorithm. We split it by 10 Megabytes using split -b 10M
command. Data that has been separated will be named archivedFile.tar.bz2.part**
, you can specify the pattern that you wish using double-quote syntax in the last command.


You can merge all chunks files before into one file using cat
command. Using split data can help a lot when sending data over the internet, especially when the bandwidth is slow.
Donation
If you find this article helpful, and you want to support me to upload more content like that, you can buy me a coffee by clicking here.