Lesson 02
Using the TAR Command to Create, Inspect, and Extract an Archive File in Linux / macOS
Video Transcript
Let's learn some Linux commands. This lesson I'll teach you how to use the
tar command. We are going to learn how to make an archive in a Unix-based system
such as Linux or Mac OS. An archive is when you take several files and you make
them into a single file. Usually you make an archive and you want to send someone
a lot of files and you don't want to do them one by one. Instead what you do is
you create an archive and take all these files and put them in a single file and
then you send that file over. Typically after archiving you also do what's
called compression so you can reduce the file size to save some bandwidth.
This lesson I'll just talk about the archiving part with tar command. So let's
do it. I'll just essentially teach you how to do three things. One of them is how
to create an archive. The second how to list the contents of an existing archive
without extracting. And the third is how to extract an existing archive. So let's
get started here. I have in my directory I did a ls-l and I have two files one
called notes.txt and another called sample.jpg. One is a text file the other is
an image. Text file as you can see here from the number 69 right after my
user my group name that's 69 bytes in size for the sample.jpg the size is
14,928 bytes. So let's say you have several files in my case it's a very
small size by simple size two files but we can generalize this for as many
files as you want so imagine you have a lot of files. So we want to create an
archive and you want to combine all these files into a single file you're
gonna create a single file that includes all these files. Let's do it that's the
tar command Tar and to create one you need an option for create. I'm gonna
start with the typically people use the shortcuts as they get more familiar with
the commands and I'm gonna start with the long option name okay so you can learn
first what it means and then you can later use the shortcut so I'm gonna say
dash dash create because I want to create an archive that's the option and then
the next thing I'm going to do I have to tell Tar what's the resulting file name
right the archive file name I'm just gonna say archive but to do that I have
to use the option dash dash file and I have to give it the file name I'm gonna
say archive dot tar that's the resulting file name and immediately after at the
end of the line I'm gonna specify all the files that I want to be included in
this archive so I'm gonna say notes dot txt space sample dot jpg and that's it
so we have called to the tar command to create an archive whose resulting file
with dash dash file archive dot tar has that name and then at the end we have the
listing of all the files that we want to place in that archive let's do it so with
that I can do another listing a less dash L and you can see there's a new file
here called archive tar you can see the file size is 20,480 did you notice that
if I add up the file sizes from notes dot txt which is 69 and sample dot jpg
which is 14,928 we would get what 14,900 and what 97 right and compared to 20,480
there's a difference there so the archive actually has more in size than
the individual files themselves if their sizes that are up that's because the
archive creates some overhead as it's made but typically if you have a lot a lot
lot lot more files I think the overhead will be less significant in any case
that's how we create an archive now that I told you about the long options
right but typically people like to first use the shortcut and then later like to
combine all the options together to a single single string of options so
instead of dash dash create you can just say dash c that's the shortcut how
people would traditionally use it right and instead of dash dash file we can use
dash f okay that's the same thing and you're gonna get the same it's just gonna
overwrite now people also like to combine the options so you can have dash c followed
by f so you can combine this two into one and it's gonna do the same thing so
tar dash c f meaning create and here's the resulting file archive that are
did you notice that every time I type my tar command and a press enter that it
seems that nothing happened but something actually happened so people also
like to add the dash v option for verbose so you could add that too if you
would like right in between the C and B or before the C whatever CVF so the V is
an option to be verbose so it will tell you in each line the file that was added
to the archive so you you probably gonna see these options used very often as
someone asked you to tar something or maybe extract later on as you will see the
V option stands for verbose so it tells you the stuff it did in this case tells
you the file names that were archived so now let's learn how to list the
contents without extracting it so I'm gonna say tar already have an archive
right archive tar that's the file we want to check to see what's inside and
I'm gonna say dash t okay t is gonna be the option though let us check the
contents and if you want to be verbose you can add the V up to you and you can
also and then you have to say F so you have to tell it what archive you want
to check archive tar so we can see tar space dash T V F space archive tar and
immediately after it lists each file that's in the archive and that matches
what we had right notes dot txt and sample dot J PG okay if you want to do
this long listing you can always check the manual I suggest you do man space
tar and depending on your system in my case I am using a Linux distribution
Ubuntu and here is how my tar command the options are like so and I was I just
thought you how to use dash T but there's the long option name here and I
would use dash dash list in this case I know the first letters not T but that's
what I would use in a long listing so the T is for the listing of the contents
okay next we are going to learn how to extract you can see here if you look at
the menu tar dash dash extract is what we want the shortcut will be just X as
you can see on the first line of this my screen my window so let's get started
let me make a director here dear to and I'm going to CD to dear to so I can
extract here instead of the same directory that I was because I already
had the files there so what I'm gonna say is gonna say tar so I want to extract
so I can either say dash dash extract and so on or I can use the shortcuts
which is dash X and it can already combine all the other options or if
you'd like you can separate them with dash space dash other option what I'm
gonna do altogether so you can do verbose dash V if you want and you can add
the you have to add the F to specify the file name in this case would be one
directory up right so I'm gonna say dot dot slash dot dot stands for the
directory one level up from where I am in this case tape archive and slash the
name of your archive in my case archive tar and you can see it's verbose and it
told us each file that was extracted I can do a LS dash L here in the current
directory and I can see the notes is here and sample JPG is here you can see
the file names 69 bytes and 14,920 bytes is the same as the ones that were
present in the directory one level up
okay so in this lesson we learned to use the tar command tar stands for a tape
archive they used to use tapes to record file data back in the days and we learned
three operations in the first one how to create an archive with dash C second
one how to check the contents of an archive dash T third one how to extract
the contents of an archive dash X if you want to be verbose and have the command
spit out some stuff while it's working you can use the dash V option and
remember to always use the dash F option to specify the file name for the
operation in question in the case of creating a archive if you tell it what's
the name of the archive in the case of checking an archive you have to tell it
the file name of the archive you want to check with the dash F option and lastly
when you want to extract an archive you have to use the dash F to specify which
file you want to extract which archive note that I showed you how to extract
everything from an archive but that you might not always want to extract
everything but specific files you can always do that as you do the extractor
man you can specify the files you want to extract for example if I go back here
and I create another directory 3 I'm gonna see the directory 3 I want to tar dash
X VF dot slash archive dot tar but I only want to take notes dot txt how could I do that
so let's try this notes dot txt at the end so let's do a listing now you can see
now when I specify the specific file name at the end of the command it's a
it only extracted that specific file so that's how you would do it if you want
specific ones of course I can do it again and extract just the sample so I can
have all of them now okay and that's the last point I wanted you to know of course
the tar command has a lot more options you can do man tar and check them out if
you need something that's more than the basic basics that I just taught you you
can have a look and there's a lot in there that's the key to quit the menu page
thank you so much for watching this video please leave your like and subscribe
and I'll see you in the next one
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: