HOW TO COMPRESS AND DECOMPRESS FILES IN LINUX: tar

By | July 20, 2016

The next tool in line of compression tools is the tape archiver (tar). Though the command somewhat has some complex (though capable) set of command line options and syntax, like we have been doing with others we’ve talked about (gzip and bzip) you can quickly learn to use tar by remembering a few simple invocations on the command line that would be required to do most of what you will do with the utility.

To create an archive from files:

tar –cf name_of_archive file1 file2 file3

For example;

tar –cf gallery.tar pic1 pic2 pic3

But if the files to be archived are already in a directory, the directory can be archived directly.

tar -cvf archive.tar directory_name

To list all the files in an archive:

tar –tvf archive.tar

To extract all files from an archive:

tar –xf gallery.tar

OPTIONS:

-t : lists the content of an archive

-x : extract files from an archive

-c : create a new archive

-f : use archive file or device archive

-v : verbosely list files processed (with this you know the command is working)

If several files are to be compressed in a single archive, tar can be used in combination with either gzip or bzip2 this can be done using the –z and –j options to filter the archive through gzip and bzip2 respectively. For example to create a gzip compressed tar archive of a directory, just add ‘z’ to the tar –cf option like this:

tar –czf directory_name.tgz directory_name

The result would be a compressed archive (a file ending with .tgz) of the specified directory and all the files and directories under it.

To view the list of files being added during compression and archiving, use:

tar –cvzf directory_name.tgz directory_name

To list the content of the compressed archive use:

tar -tzf archive.tgz

To expand the content of a compressed archive use:

tar –xzf archive.tgz

Using the same principle try combining tar with bzip2 using the ‘j’ and see the results.

To compress;

tar –cjf archive.tbz2 directory_name

To list all the file sin the archive use;

tar –tjf archive.tbz2

To expand the content of a compressed archive use:

tar –xjvf archive.tbz2

Happy Linux’NG!


NEVER MISS AN UPDATE




I agree to have my personal information transfered to MailChimp ( more information )

Join over 10,000 visitors to receive Open Source tips, trick, news, tutorials, programming and more.

We hate spam. Your email address will not be sold or shared with anyone else.

ALEXANDER OMOROKUNWA
MUST READ  Why I have come to love Ubuntu Linux.

Tell us what you think

This site uses Akismet to reduce spam. Learn how your comment data is processed.