Introduction to the Linux/UNIX tar command
The name "tar" stands for "tape archive.
" originally designed as a program that would let people back up their files to magnetic tape.
Here's the idea of an archive: Let's say you have some shirts, and youwant to store them away.
You get a bag and put the shirts in the bag.
Here's the command to "tar" the shirts: tar -cvf shirts.
Tar orange green blue purple Let's examine the options to the tar command.
The "c" means you are creating an archive.
The "v" means "verbose.
" If you use this option, tar will give you output about what it's doing.
The "f" means "I want a file as output," and it's followed by the file name; in this case, shirts.
And finally, the files you want to go into the archive: orange, green, blue, purple.
That makes the entire command: tar -cvf shirts.
Tar orange green blue purple When you need to get the shirts out of the bag, Just use the "x" option, which stands for "extract.
" Again, "v" stands for "verbose," and "f" specifies the tar file: shirts.
That extracts all the files from the "bag.
" If you had specified any file names after the tar file name, only those files would have been extracted.
tar is very handy for backing up your files, but there's one problem: The size of the tar file is greater than the size of all the original files.
That's because there's some overhead involved in keeping track of the file positions in the archive, as well as their names and other such things.
The solution is to find some way to shrink the tar file so it takes up less space.
So let's talk about how you compress files on Linux.
Consider a file named "car.
If you say "gzip car.
Txt" The gzip program compresses the file, and renames the result as car.
Gz You don't have to add the extra.
Gz at the end; gzip does that for you.
gzip isn't the only game in town.
There's also a program named bzip2.
It adds the ".
So if I said bzip2 car.
Txt, I would get car.
Bz2 as my output file.
bzip2 works better on large files; compressed files become smaller than with gzip but it takes more time to do the job.
To uncompress a file, you use gunzip or bunzip2.
The program will automatically remove the.
Bz2 extension on the uncompressed file.
So your commands would be gunzip car.
Gz or bunzip2 car.
Bz2 Now, let's see how this helps us with tar files.
Remember how you created shirts.
Tar with the command tar -cvf shirts.
Tar orange green blue purple You can now use gzip on the shirts.
Tar file by typing gzip shirts.
Tar and you'll get a smaller archive file named shirts.
Gz Because making a tar file and compressing it is such a common operation, compression has been built into tar.
If you add the "z" option to the tar command, so your options now read c v z f, tar will use the gzip method to compress the resulting tar file Unlike the independent gzip command, you have to specify the.
Gz in your file name.
It doesn't get added on automatically.
So in this case, after the "f", you would put the file name "shirts.
Gz" Becuase the tar plus gzip operation is so common, you will often see people abbreviate the resulting filename extension as ".
Tgz" instead of ".
Gz" If you add the "j" option to the tar comand, so your options now read -cvjf, tar will use the bzip2 method to compress the resulting tar file.
Again, you must specify the.
Bz2 in the file name.
It's not automatic.
So your command would now read: tar -cvjf shirts.
Bz2 orange green blue purple To extract the files in a tar-gzip file, you use the "x" option for "extract," just as you did before.
and you add the "z" option to stand for the gunzip compression method.
So your command would be: tar -xvzf shirts.
Gz To extract the files in a tar-bzip2 file, you use the "x" option to extract, and you use the "j" option instead of "z" to use the bunzip2 uncompression method, and your command would read tar -xvjf shirts.