Knowing how to use the command line of Linux shell to extract and compress
files is very important. On this tutorial we will guide you on doing just that.
1) tar (tape archive files)
TAR stands for Tape ARchive. It was originally designed for tape backups.
Hence, the word tape in the name. It can now be used to create a tar file
anywhere on the filesystem. TAR creates one "tar file" out of several files and
directories keeping the absolute paths if wanted. It is important to know that
it does not compress the files in any way. Thus, the TAR file will take up the
same amount of space as all the individual files combined. A TAR file can be
compressed by using gzip or bzip2.
?
| Basic TAR Syntax |
? ? The syntax for TAR is as follows: tar -switches
tarfile.tar where tarfile.tar is the name of the tar file.
Commonly Used TAR Switches
| Switch |
Explanation |
| x |
Extract the contents of the TAR file |
| c |
Create a TAR file |
| z |
Gunzip (uncompress) it before extracting, used on file ending in .tar.gz
or .tgz |
| v |
Verbose - display contents as it is tarring or extracting |
| f |
Filename to follow |
| t |
List contents of TAR file |
|
Some samples of using tar :
| Example |
Explanation |
tar -xvf filename.tar |
Extract the contents of filename.tar and
display the files as they are extracted |
tar -cf filename.tar /home/user |
Create a TAR file named filename.tar from the
contents of the directory /home/user |
tar -zxvf filename.tgz |
Gunzip(uncompress) filename.tgz and then extract
the contents displaying the files as they are extracted |
tar -tvf filename.tar |
List contents of filename.tar to the screen |
?
2)? gzip & Gunzip
gzip and gunzip are GNU file compression and
decompression utilities. Usually, files that have been compressed by gzip
will have a .gz extension. However, sometimes you may see a file
that has a .tgz extension. This is a TAR file that has been
compressed by gzip. The .tgz extension is a shorthand
version for the .tar.gz extension. This type of file must be
uncompressed with gunzip before it can be untarred. However, there
is a way to use the tar command to uncompress the file and untar it
at the same time.
Uncompressing a gzip File Using gunzip
To uncompress a gzip file, execute the following command:
gunzip filename.txt.gz ? ? (where filename.txt.gz is
the name of the file you wish to uncompress)
The result of this operation is a file called filename.txt. By
default, gunzip will delete the filename.txt.gz file.
?
Compressing a gzip File
To compress a file using gzip, execute the following command:
gzip filename.txt ? ? (where filename.txt is the name
of the file you wish to compress)
The result of this operation is a file called filename.txt.gz. By
default, gzip will delete the filename.txt file.
?
3) bzip2 and bunzip: Files With .bz2 Extensions
? ? bzip2 and bunzip2 are
file compression and decompression utilities. The bzip2 and
bunzip2 utilities are newer than gzip and
gunzip and are not as common yet, but they are rapidly gaining
popularity. The bzip2 utility is capable of greater compression
ratios than gzip. Therefore, a bzip2 file can be
10-20% smaller than a gzip version of the same file. Usually,
files that have been compressed by bzip2 will have a .bz2
extension.
? |
| Uncompressing a bzip2 File Using
bunzip2 |
? ? To uncompress a bzip2 file, execute
the following command:
bunzip2 filename.txt.bz2 ? ? (where filename.txt.bz2
is the name of the file you wish to uncompress)
The result of this operation is a file called filename.txt. By
default, bunzip2 will delete the filename.txt.bz2
file.
? |
| Compressing a File Using bzip2
|
? ? To compress a file using bzip2,
execute the following command:
bzip2 filename.txt ? ? (where filename.txt is the
name of the file you wish to compress)
The result of this operation is a file called filename.txt.bz2.
By default, bzip2 will delete the filename.txt
file.
? |
?
That's about it, have fun with your linux experience.