We'll now look at how to archive files in Linux.
Now imagine you have a website with thousands of files and directories.
You don't want to lose anything. So you decide to back up all this data.
Now, if you do that manually, you'll usually go through steps like this,
you pack all those files and directories into a single file like backup dot Tar.
This action is called archiving.
You compress backup dot tar.
So you can store 10 gig of data in a smaller compressed file,
which might only be seven or eight gigs of storage space, maybe less.
You'll end up with a file like backup dot Tar dot GZ.
And then finally you copy the compressed file to a remote location.
So you have a second copy of your data in another place and that's your backup.
Let's learn how to perform these actions. In
this lecture,
we will explore archiving and later we'll look at
compressing and backing up data to a remote location.
The name Tar comes from tape archive.
There was a time when keeping backups on magnetic tapes was very popular.
Tar was used to prepare data that would be sent out to these tapes.
But even if this backup format on tapes is used less often these days if
at all tar is still a popular tool because it does its job well.
So what does it do?
Tar is basically a packer and unpacker.
It can take any number of separate files and directories
and pack them all to a single tar file,
which is also called a Tar Ball.
Since we now have all our files in a single package,
this makes it easier to move it around, upload it to a website.
Let people download the archive and so on.
Now imagine we have an archive dot tar file on our system.
Tar lots has specified command-line options in three different ways.
For example, to display the contents of that archive,
the correct command-line option is either dash dash list or dash T
or T
the shortest option.
So we can use one of the following forms here.
Now, after you memorize what each letter like T or F does,
you'll prefer the last method as its fastest to write.
But it might be hard to remember in the beginning what letter is used for listing,
which is T
whereas dash dash list is much easier to memorize the most important thing though,
when you point the tar utility to the dot Tar archive,
it's good to have it to add the dash dash file or dash F or F option
at the end of your list of options
because Tar expects the path of a tar file immediately after the file option.
If you put that option in another spot, for example, at the beginning,
you might add a second option after it
and Tar would get confused thinking your second option is actually a path to a file.
Let's look at some tar commands
to take file one and pack it into a file called archive dot tar.
We would type in Tar dash dash create dash dash file, archive dot tar file one.
Now dash dash create tells Tar that we want to create a new tar archive
with the name specified after dash dash file to add another file to our archive.
We would type in tar dash dash, append dash dash file,
the name of the Tar ball and the file name. And
now append is just a fancy word that means add to an existing archive
to add an entire directory and its contents to a tar archive.
We would type in something like this.
If you use a relative path here like pictures front slash,
the path stored in the archive will look like this.
But if you use an absolute path,
the content will look like this
before extracting from a tar archive, you should always use this command
and check out these paths.
Why? To get an idea of where your extracted files will end up.
For example, we could extract with this command
and our files will end up in the current directory,
plus the paths we saw stored in the archive.
If we were in slash home slash Aaron slash work,
these files would be extracted at slash home slash
slash work slash pictures.
Now, by default, tar extracts these in our current directory.
But what if we want to extract this to another directory?
Let's say we're in slash home slash,
we have archive dot tar here, but we want to extract its contents to slash T MP.
We can tell Tar, hey, don't extract to my current directory.
Extract to this other directory here.