In this video, you'll learn about TAR.
TAR is the tape archiver.
And as that suggests, it was created a long time
ago to archive files to tape.
So it's a backup utility.
It was used to stream files to a backup tape.
By default, it doesn't compress data.
And the basic use of TAR is to archive, to extract,
or to list the contents of an archive.
So you can use a command as in tar-cvf,
followed by the name of the archive
that you want to create, followed by what
you want to put in there.
So slash home and slash etc will be inside the myarchive.tar
in this example.
Or tar-tvf, which will show the contents of an archive.
That's always smart if you want to extract an archive
to figure out what is going on.
And you can also use tar-xvf to extract
the content of the archive.
By default, it will extract to the current path.
And you can use minus c to switch the output path
to extract it to somewhere else.
And also, you can add compression
using either minus z or minus j or minus uppercase J for gzip,
bzip2, or xz compression.
Let's go check this out.
To start this, I'm going to the user root home directory.
That's nicer.
So tar-cvf, myarchive of slash home and slash etc.
Hey, in case you are wondering, didn't I forget anything?
Do you see that command?
Where is the minus in front of the c and the v and the f?
A little bit of Unix history.
In ancient history of Unix, there
were two main dialects, BSD Unix and system 5 Unix.
In BSD Unix, it was common to use command line options
without a hyphen in front of the option.
In system 5, it was common to use hyphens
in front of the options.
Nowadays, in Linux, system 5 is 1.
And that means that using a hyphen in front of the option
is the normal way of doing it.
But some commands like tar that originate from BSD Unix
still allow you to work without a hyphen in front
of your command line options.
Like here for the tar utility.
Now, if I'm using ls minus l on my star there,
we can see the archive.
And oh my goodness, it's quite big.
Do you see that?
It's 109 megabytes, more or less.
Now, I want to do the same.
And this time, I want to use compression.
Minus c, I'm using the minus this time
to show you that really it doesn't matter.
It works with and without the minus.
And the behavior is the same.
z for compression, v for verbose, f for file name.
I want to create my archive dot gz.
It is common if you use gzip to create a compressed archive
to use the extension dot gz.
Common, but not mandatory.
Because extensions in Linux are not
as important as they are in other operating systems.
So I'm doing the exact same thing.
And what do we see?
So here it is creating the compressed archive this time.
And when we use ls minus l on my a,
you can see that there's quite a difference.
Now that we are talking about compression,
let's use the other compression utilities as well.
So this time, I'm going to use bzip2.
So I'm using the extension bz2.
And in order to compress bzip2, I need j.
Do you notice that it's getting a little bit slower?
And there's a reason for it.
And that is because bzip2 is a bit more efficient.
And the xz command is even more efficient.
I need uppercase J to use xz.
And hey, now that we are talking about slower,
I'm going to put time in front of it,
which is showing us how long it actually takes
for this command to complete.
And if you would compare that to the performance
of the bzip2 utility,
you can see that really there's a big performance difference,
16 seconds of user time in total,
which is quite a lot.
But there's a reason for it.
And the last one compresses a lot more efficient.
Oh, I made a mistake, by the way.
Do you see that my archive.bz2
doesn't contain any bz2 compressed files.
It's an xz compressed file.
But as I told you, Linux doesn't care about extensions.
Linux can figure it out itself.
And you can prove that by using the file command.
And there you can see that the file command
clearly recognizes that this is xz compressed data.
Okay, so I created all of this stuff.
And now I want to extract it.
Let me create a directory extract.
And I want to use tar-tvf first
so that we can verify what is in the archive.
And I'm going to do it on the myarchive.bz2.
And there we can see the content.
And in the content, you can see that all of these
are relative file names.
Now, if I would use tar-cvf,
then it would extract it in the current directory.
But I don't want to extract it in the current directory.
And that's why I'm going to use minus uppercase C
to put it in the extract directory.
And oh my goodness, what do we have?
We have what we call a typo.
Do you see that?
I have tar-vf.
And the tar command is complaining about it.
Hey, you are forgetting one of the options.
Yeah, I'm forgetting the x option.
And now we can see that it has extracted
not in the current directory,
but it does exist in the extract directory, as you can see.
And that's how you can use the powerful tar utility.