In this video I will tell you about tar, the tape archiver. In case you are wondering. Tape archiver, that's a
backup tape that in the past you would connect to a parallel port on your computer to stream a backup.
Pretty slow, pretty primitive, but hey, that's the way how it was.
TAR was the utility to create files on that backup tape.
TAR is still there even if backup tapes are not used that often anymore. And
the basic use of TAR is to archive, to extract or to list files.
The basic commands are TAR CVF where C is for create, V is verbose and F is
followed by the name of the file that you want to create. Like myarchive TAR home.
What will that do? Well, it will archive the home directory into the
archive file archive tarot and then you can use XVF to extract it.
If you are going to extract it, you should notice that extraction by default happens to the current path.
If that's not what you want, you can use C to switch to the output path.
Or you can also go into the output path beforehand because then
it will extract to the current directory and nothing is happening.
When you have a backup file and you are wondering what is inside, it might also
be useful to use T VF because that will show the contents of an archive.
And if ever you are wondering if a file is an archive or not, you can use the file utility to get more details about a file.
And in case you are wondering why would I want to do that? Because an archive
has TAR as the extension, isn't it? No, it's not. The extension is just a convenience.
On the Linux operating system. Linux extensions file have a so called magic code at the start of
a file that helps the Linux operating system to identify what type of file it is. And you
can do so manually using file. I have seen situations where I downloaded something from the Internet.
It didn't have an extension, so I didn't know what this is. An
archive, an executable. Then the file utility is really a very convenient utility.
Now back to tar. TAR doesn't compress and at all. If you want to add compression,
you use z or j or uppercase j respectively for GZIP, BZIP2 or XZ compression.
Before I show you more, we need to talk a bit about Unix styles.
TAR supports two ways to provide arguments.
You can use the options without a minus in front of them like TAR cvf.
That's what we call the BSD Unix style.
And there is the option to use the arguments with a minus in front of them. And that's System 5 Unix style.
Little bit of background.
In the 1980s, Unix was the leading operating system and there were two main
dialects in Unix and one dialect was BSD. The other dialect was System 5.
In the end, System 5 became the dominant dialect.
And that is why some of the common practices from System 5 still exist nowadays.
But utilities like TAR, but also PS that have a BSD origin still honor the
BSD origin by allowing you to put arguments without a minus in front of them.
Let me show you how to work with tar.
So I'm going to start using TAR cvf, that's BSD style, you see etc tar
and I'm going to put etc in there and oh boy, I'm getting exiting with
failure status due to previous errors. You know what I. I know what the errors
are. The etc directory is not readable completely for non root users.
So I need to do this with sudo privileges and then LS
l on etc TAR is showing that the archive was created.
Now as I mentioned, we can also apply compression. So let's do the same
command, but this time we put a Z in there for for gzip compression
and I'm calling the resulting file etc star gz same etc directory.
And before we are going to compare, I'm also going to use J for bzip2 compression.
And let's also do that with XZ compression using the uppercase j option.
Do you notice that it is really slowing down?
Well, there's a reason for that. Let's do an LS l on
etc and we can see the etc archives that have been created.
So originally the etc tar was quite big.
The gzip file reduces it to about 25% and
then bzip2 and xz reduce it even more.
Now if we want to see what is inside tar t PVF on etc tar, whichever,
no need to specify the uncompression utility when you want to show what is inside TAR
recognizes that it is a compressed file and it will show you automatically the files within.
Of course you are allowed to add the uncompression utility, but it's really not necessary.
Now I'm going to use xvf etc tar exe and you
know what, I want to extract it in the tmp directory.
So there we go. And when I'm listing the TMP directory,
as you can see, the etc directory has been created.
And if you don't believe that it worked out all right, well the tree utility is
always a nice way to verify the contents. And that really looks like an ATC directory.
One more thing, the file utility.
So if I would use mvetc tar to just etc I can do that.
And then there is nothing anymore that on the etc file indicates what it is.
It's marked as just black and we really don't see it anymore.
But if you use the file utility, the file utility is telling that this is a POSIX STAR archive.
And if you want to know what POSIX is, POSIX is an old Unix
standard to guarantee interoperability between the different flavors of the UNIX operating system.