Let's say we want to archive or store a user's home directory just in case we need it.
One way to do that would be with a Taar command.
The tar command originally stood for tape archive.
But you can also use it to archive files to any storage device not just tapes.
So we'll use type against TAR shows that it's a file on file system which is executable here in the
user bin directory.
And let's look at the man page for this command.
I'm just going to move down here a little bit and look at these examples here.
So these three examples are the three main uses of the tar command.
Here you can see how to create an archive how to view the contents of an archive and how to extract
the contents of an archive.
So the dashi option is used to create an archive the dash f option allows you to specify the file name
of the archive file.
The Dashti option is to list the contents of an archive.
The dash via option is for verbose and the dash x option is for extract.
So let's try these commands out by the way if you want to get details on those of course.
They are listed here and all the options in the man page.
First let me create a directory in some files in there so we have something to play with and something
to create an archive with.
So let's say we have some cat videos Let's do this cat videos Admiral cat bar
Let's call this one Tarth Paul.
When I ask can we come up with here and let's do Luke Clocker.
What or two more here.
Obi Wan cat novae
about had me.
I'm a kitty cat him before here.
So if we look at our cat videos director here we have some empty files that were created with the touch
command.
Now let's go ahead and create an archive of these cat videos Ashesi to create after specify a file will
say cat videos dot Teej are since that is the convention.
You don't have to do this but this is a good practice and they will give it a path to archive.
So we're just going to archive cat videos that directory and all of its contents.
So if I do ls here we have cat videos dot Taar.
So the archive was created.
Now let's look at the contents of that archive.
Will the tar Dashti to list the contents of the archive and we have to give it a f. a file which is
cat videos.
They are.
So that shows us the contents within that tar file.
If you wanted to see the files as they were being added to the archive you can use the verbose option.
So let's go ahead and just delete this tar file here now and do this.
See verbose and then do cat videos TR and the cat videos directory.
By the way you can leave off or leave on the directory separator.
Either one is fine.
I'm just going to leave it on for this time.
So this time instead of silently creating our tar file the dash option allowed us to see what was being
added to our archive as it was happening.
Let's go ahead and restore the files from this archive somewhere.
Let's go ahead just create a restore directory change into that directory and then what we're going
to do is do an x for extract and then we got to give it a file.
And of course dot dot is a parent directory of where we were just at and then Ford slashes a directory
separator and then cat videos dot TAR is the name of the file.
So again here Dot Dot forward slash I'm not doing anything that's necessarily related to tar.
This is just a Linux convention of relative pathing how to get to a file that's in the parent directory
of your current directory.
By the way I could have used the full path I could use home vagrant cat video star but I'm not going
to do that.
OK so we have a command here dash X for extract dash for the file that we're going to use as our archive
file which is not for cat video store and going to hit enter.
So now if we look in this new directory here we have the cat videos directory created.
And if we list the files in that cat videos directory we get all those files that were inside that archive.
I'm just going to go ahead and remove all this right here.
So this time let's use the verbose option to see exactly what we're extracting and this is typically
what I like to do.
OK this shows that it's created the directory and then put those several files within that directory.
OK I don't need that directory right now so I'm just going to delete it and I'm going to type CD dot
dot to move back up to the parent directory and I'm back in my home directory.
One of the common things people do with archive's is actually compress them to save some disk space.
Now this can be a two step process like so.
Let me go ahead just remove the tar file that was there.
Let's create a cat video star file.
And now let's zip it.
By the way the JE's up command compresses the original file and appends a dot g the to the end of the
file name.
So if you see a file that ends in Tarar GZ It's a compressed archive.
Now to decompress or uncompress a zip file.
Use the unzip.
Now we have our uncompressed original tar file back.
Let's go ahead and delete that.
You can perform the compression on the archive when you create it.
If you use the dash the option to TAR.
So what we can do here is to tar Dasch Z for compress and see we're going to create an archive.
Is going to be verbose EFIS we're going to give a file name
and then a path to what we're going to put in our archive here we can see we have our cat videos at
Target gz file.
And if we want to view the contents of a compressed archive we also need the dash CSY option.
So we'll do our dash z t to list verbose Ephor file.
You know the drill.
We'll give it the path and then we can see the contents of that archive without actually extracting
it.
So let me go ahead and just delete that right now.
Sometimes people like to use the File Extension of dot P GZ to represent a compressed archive so we
can do this Taar the CVF cat videos Dot T G Z and then give it a path.
So it's just something to keep in mind that target Z and Dot T G Z are really the same thing.
Let's go ahead and restore this compressed archive we'll go into our restored directory here.
And there's nothing in our folder.
So let's use the dash z option cause we're working with a compressed file and then provide the path
to that file.
Actually this time I'm going to use the full path
OK.
It displays what was extracted and if we look sure enough we see the contents of that archive has been
extracted on disk.
I want to point out something that's kind of important here.
The tar command really like most Linux commands treat you like a knowledgeable sane responsible adult.
So it will happily override anything in its way.
It assumes you know what the heck you're doing.
So let's add some text to a file just to change it up a little bit.
So let's just put some text in here and to our cat videos file of Darth paw.
And if we kept the contents of that file we'll see it says what we piped into it by the way exclamation
mark dollar sign takes the very last argument on the previous command line and places it on the current
command.
So again we're using what's called event designators in bash and you can see that exclamation mark dollar
sign expanded to the last thing on our previous command line here.
Cat videos Darth paw.
And then it displays the entire command it's going to be executed and then it displays the result of
that command.
Just another quick tip for you there.
Anyway back to the original point.
Let's go ahead and extract this archive again.
Execute the most recent command that starts with C which is cat and if we can't get videos for it unless
Darth paw nothing is returned.
And if we look at a long listing of that file it now has a file length of 0 and so nothing is in it
which is the original file or the father was in the archive.
So again Taar there just overwrote the file.
It didn't ask for a confirmation or anything like that it didn't give you a warning.
It just went ahead and over of the file.
So be very careful when you're extracting archives and just be conscious of what you're doing.
This also goes without saying but Taar relies on file permissions like any other command does.
So if you try to create an archive of files that you don't have permissions to.
Well that's just not going to work.
Also if you try to extract the contents of an archive to somewhere where you don't have permissions
to that's not going to work either.
So if you want to be sure you have permissions you suit you or become the user first.
For example let's say you want to backup the contents of the forward slash ETSI directory that contains
pretty much all of your configuration files on the system you'll need root privileges to do that because
there are some files in NC that a normal user just does not have access to.
So let's get back to our home directory and do suruh tar dashes Z Moli velt the verbose option see Alf
recreate the see the T GZ file and will archive the contents of forward slash ETSI here like I just
said I didn't use the verbose option mainly because I wanted you to see this particular message.
TAR says I'm removing the leading forward slash from member names.
If we look at the contents of the archive we'll see that there is no leading slashy there so do this
tar ZDF at c TDC so we can see the last file there at CNN RC is not forward slash and C forward slash
nano RC.
It's just ETSI word slash nano RC.
This allows you to extract the contents of the archive into a different location without overriding
all the existing files.
So if it preserves the leading slash forward slash and C for slice or C if you try to extract this archive
then it would overwrite that file.
And that may be not what you want to do.
For example maybe you want to extract this into a different subdirectory and then compare the contents
of that archive or that backup to what is currently on disk instead of just blowing right over and over
writing it right away.
So more or less here tar is going to remove the absolute pathing indicated by the leading forward slash.
And it's just going to leave you with a relative path within your archive.
There is one more thing I'd like to talk about with the tar command.
Now you may have seen people leave off the leading hyphen and do things like this.
Tarzi see CVF cat videos.
TDC and then give a path.
So this is an older type of syntax that is still supported to this very day.
So I just want you to be aware of it in some documentation you might actually see mention of quote old
options unquote.
Well this is what they're talking about.
If you use this old option syntax all the options must be written together as a single clomped set without
spaces separating them or dashes preceding them.
Also the set of letters must be the first to appear on the command line after tart's cell and then some
whitespace.
Old options cannot appear anywhere else.
They must immediately followed tar with some whitespace.
The letter of an old option is exactly the same letter as the corresponding short option.
For example the old option t is the same as the short option Dashti when options require an argument.
Those arguments have to be presented in the same order.
This can get pretty confusing if you use multiple options and have an argument.
So instead of worrying about how to do it correctly.
Honestly my advice is to simply use a hyphen before your options.
That way you can do things like this tar space Desch F and then provide whatever option you want to
you after that command and then all your other options they can be Dessie dash V and so on and here's
a path.
And we do this and sure enough we get our archive created if we wanted to do it with the quote old style
syntax we'd have to do this tar.
See V.F. cat videos tar and then the path.
So that works.
So he couldn't do something like this for example tar Affe cat videos and then do see in the out here
because one is going to try to do is interpret C and V as files that are to be added to the archive
and not as options.
So again it gets kind of confusing when this old syntax will just really treat tar like any other command
or using dashes if some old grumpy Unix guy with suspenders and a beard yells at you for doing this
we'll just tell him that I told you to do it and put the blame on me.