What’s tar? Tar is the abbreviation for Ribbon Archive and refers to the practice on previous calculation days where data was copied to ribbons. Despite the nostalgic origin of the name, the resin is very powerful and uses modern technology to archive and compress files.

Basis of the update: Archives against compression

The tar command is important for understanding Linux users. Before we go deeper into this subject, let’s start with a small clarification.

  • Check-in – The action of saving multiple files as a single file.
  • Compression – the action of compressing one or more larger files.

The tar is an archiving tool. It creates a file from different files. This saves network bandwidth, time and processing power when transferring files. A single 100 MB file takes much less time than a 100 MB file transfer due to overhead costs.

That’s why you will often find software available in Tarball. Tarball is a common term for a tar file.

Although the Tar itself cannot compress files, you can use one of the most common compression algorithms to compress files when creating the Tarball. I’ll show you how to do it later in this basic Harz manual.

Plain tarred files

Here are some of the common tar files you can find:

The tar: It’s a tarball file. It’s just an archive and there’s no compression.

.tar.Gz or .tgz: This is an extension of the Gzip compressed archive.

.tar.Bz2 or .tbz: This is an extension of the archive compressed with Bz2. This is a relatively new technology. It has a higher compression ratio, but due to the increased shrinkage capacity it takes a little longer to finish.

…tar.xz or .txz, etc… Tar is also compatible with xz, lzip and other functions. These tools mainly use the same compression algorithm, LZMA. The popular 7z, which has become very common in Windows environments, also uses this algorithm. Other differences in the files are due to the structure and metadata. I won’t go into detail in our examples, but I will mention them.

It is important to remember that extensions are not necessary on Linux and other Unix systems. Your Unix system can usually identify files by their headers, regardless of their extension, but using a common naming scheme can help avoid confusion. You can use the command File under Linux to find out the file type.

Examples of tar content commands

How to Create and Extract Tarballs from Linux Command Line

In this article I would like to introduce you to some of the common methods of tarballing and file compression.

I have collected several files of different types in the Documents folder. There are backup images and some text files. We will look at several examples of how compression changes the size of files.

Here you will find a complete list of documents to be used.

[email protected] manual:~/Documents$
total 7404
drwxr-xr-xr-xx 2 Christopher 4096 May 5 10:55 ./
drwxr-xr-x 17 Christopher 4096 May 5 10:59 …
-rw-r–r—– 1 Christopher 5094932 Apr 30 23:54 Aerial view of the bush on the sand field -3876435.jpg
-rw-r–r— 1 Christopher 13268 Apr 30 23:57 Lorem1.txt
-rw-r–r—- 1 Christopher 13268 Apr 30 23:57 lorem2.txt
-rw-r–r–r— 1 Christopher 13268 apr 30 23:57 lorem3.txt
-rw-r–r— 1 Christopher 13268 apr 30 23:56 lorem.txt
-rw-r–r–r— 1 Christopher Christopher 2411919 apr 30 23:54 mountain-range-2397645.jpg

I’ve already mentioned the possibilities of the resin. It is a powerful tool that offers many possibilities. Different options and different file types can make the command more complex than it actually is. As always, my goal is to demystify the command line. So let’s split things into different parts before we combine some options in the examples.

Here is a table with the most commonly used options. Remember, this is just the beginning. I recommend that you consult the reference material yourself to find even more options as soon as you feel comfortable.

Switch Advanced options Description
-c, -create create a new archive
-d, -diff, -appear distinguish between archives and the file system
-r, -Offer Attach files at the end of the archive
-t, -list slightly shifted
-u, -new website only to add newer files than to copy them to the archive.
-x, -excerpt, -collection file recovery
-j, -Flash2 filter the archive via bzip2
-z, -gunzip, -gunzip, -unzip. gzip

1) Make a tarball

I mentioned earlier the usual file types associated with the tar command. It’s probably the easiest thing to do.

tar cvf output_tarball.tar source_directory

Compression is not applied, so the file takes up at least as much space as the files in the Documents folder.

[email protected] manual: ~$ tar cvf doc.tar ~/Documents

tar cvf (create, verbose, file archive): creates a new tar file with the name doc.tar of all files in ~/documents

[email protected] handbook: ~$ will doc.tar
-rw-r–r—– 1 christopher 7567360 may 5 11:00 doc.tar

Please note that the tarball will have the same folder structure as the original tarball folder.

2) Make a gzip tarball

Let’s try the gzip compression while we make the tarball.

[email protected] manual: ~$ tar cvzf doc.tar.gz ~/Documents

tar cvzf (create, verbose, g-zip, file archive): creates a new tar file named doc.tar.gz of all files in ~/documents. When making a tarball, gzip compression is used (with the z option).

[email protected] manual: ~$ ll doc.tar.gz
-rw-r–r—- 1 christopher 7512498 may 5 11:01 doc.tar.gz

As you can see, the size of the zippered tar balls, 54862 bytes (53 MB), is smaller than a normal uncompressed resin ball.

Please note that when using the hyphen – with the tar options

When using options with the Linux command, you usually add a hyphen (-) in front of the options.
It is not mandatory to abort the options and it is best to avoid this. That’s why I didn’t use it in the examples.
If you use a hyphen for the options, always leave f at the end of the options. If you use tar -cvfz, then z becomes an argument for the z option. And then you see such a mistake:

doc.tar.gz: I can’t right now: Such a file or folder does not exist.

Therefore, it makes sense to use option f at the end of all other options, so that even if you use a hyphen for the habit, it won’t be a problem.

3) Make a bz2 tarball

Say you want to make a bz2 tarball. The steps are the same as the last one. Simply change the z (gzip) option to j (bz2). See the table of options I mentioned earlier.

[email protected] manual: ~$ tar cvjf doc.tar.bz2 ~/Documents

tar cvfj (create, detailed, type bz2, archive file).

[email protected] manual: ~$ ll doc.tar.bz2
-rw-r–r—- 1 christopher 7479782 5. 11 May 11:04 doc.tar.bz2

Did you notice the size? It’s even less than Tarball.

4) Indicate the contents of the tarball

You can use the option -t (instead of -c) to display the content of an archive file. It works the same whether the file is compressed or not. The actual file size is shown, not the compressed size.

[email protected] manual: ~$ tar tvf doc.tardrwxr-xr-xristopher/christopher 0 2020-05-05 10:55 home/christopher/Documents/rw-r–r– christopher/christopher 5094932 2020-04-30 23:54 home/christopher/Documents/aerial-view-of-bushes-on-sand-field-3876435.jpg-rw-r–r— christopher/christopher 13268 2020-04-30 23:57 home/christopher/Documents/lorem1.txt-r–r– christopher/christopher 13268 2020-04-30 23:56 home/christopher/Documents/lorem.txt-rw-r–r— christopher/christopher 2411919 2020-04-30 23:54 house/christopher/Documents/lorem.txt-rw-r— christopher/christopher 2411919 2020-04-30 23:54 house/christopher/Documents/mountain-range-2397645.jpg-rw-r–r— christopher/christopher 13268 2020-04-30 23:57 home/christopher/Documents/lorem3.txt-r–r– christopher/christopher 13268 2020-04-30 23:57 home/christopher/Documents/lorem2.txt.

5) Add extra files to the tarball

You can add files to the tarball archive with -r. You cannot add files to a compressed archive without first decompressing them with the tar command.

You can also add and update with the -u option. This option is only meant to add new folders based on the reference documents, but in my practice it worked just like the application by adding new copies of all folders.

[email protected] manual: ~$ tar rvf doc.tar ~/Documents/

6) Remove tarball

Now that you have seen how the different types of compression affect the overall size of the files, let’s take a look at the extraction of these files.

[email protected]:~$ cd docs
[email protected]:~/docs$ tar xvf ~/doc.tar.gz

I created a new folder called docs. Next I used tar xvf (extract, multiword, file archive) to extract the content here.

[email protected] manual: ~/docs/home/christopher/Documents$ ls
air-view-of-bushes-on-sand-field-3876435.jpg lorem1.txt lorem2.txt lorem3.txt mountain-range-2397645.jpg

It is important to note that the tar maintains the structure of the files. So when I unpack files, they’re in /home/christopher/Documents. To avoid this, you can switch to the desired directory (~/Documents) and copy all files with a wildcard * instead of the directory structure.

7) Remove the tar ball from special catalogue

By default, the contents of the archive are extracted from the current directory. It’s not always desirable.

You can unpack a traboline to a specific folder as follows

tar xvf tar_file -C Target_directory

The target directory must exist, so make sure you use the mkdir command to create it.

You should keep this in mind when using tar under Linux :
Almost always used in tar cf or tar xvf format. Remember this:

c means creation: You use it to make a Tarball
x: You use it to extract the tarball
f that the : You use it for the name of the tar file (both for creation and extraction). Try using them at the end of the options.
v is an abbreviation of Multiword : It’s not necessary, but it shows what’s happening to the crew.

Of course you can’t use the options c and x in the same tar command.


Did you like our team guide? I hope you’ve learned something new from all this advice.

If you like this guide, make it available on social networking sites. If you have any comments or questions, please leave them below.

If you have suggestions for topics you would like to see highlighted, feel free to leave them behind. Thank you for reading it.

How to Create and Extract Tarballs from Linux Command Line

Christopher Murray

Christopher is a software developer in Orlando, Florida. He likes open source, Taco Bell and Chi-weeney named Max. Visit his website for more information or contact him via social media.

Did you like the article? Share with us and help us grow ?how to extract tar gz file in windows command line,how to unzip tar.gz file in linux,gzip: stdin: not in gzip format,how to unzip txt.gz file in linux,how to unzip gz file in linux,extract tar.gz windows,how to extract tar.z file in linux,unzip tar.gz mac