Tar commands in Linux

In this article, we will go with a useful utility tool in Linux for reducing the size of files before transferring over the internet. Tar is the most widely used utility for archiving and compressing files, and it has options for extracting and decompressing the archive files.

How to use tar

We will go to discuss three different extensions archiving file which is created by tar command and the size will be optimize orderly.

Type of extension Command
.tar tar -cvf myfolder.tar /home/techsavvy/myfolder/
.tar.gz tar -cvzf myfolder.tar.gz /home/techsavvy/myfolder/
.tar.bz2 tar cvfj myfolder.tar.bz2 /home/techsavvy/myfolder/

Options:

  • c – Creates a new .tar archive file.
  • v – Verbosely show the .tar file progress.
  • f – File name type of the archive file.
  • z – Filter the archive through gzip.
  • j – Filter the archive through bzip2.

How to extract data from the archive file

$ tar -xvf your-archived-file -C /path/you/want/extracted

Options:

  • x – Extract files from an archive.
  • v – Verbosely show the .tar file progress.
  • f – File name type of the archive file.
  • C – Change to DIR before performing any operations

List content of the archive file

$ tar -tvf your-archived-file

Options:

  • t – List the contents of an archive.
  • v – Verbosely show the .tar file progress.
  • f – File name type of the archive file.

Check the size of the archive file

$ tar -czf your-archived-file | wc -c

The above command will display the size of archive file in Kilobytes (KB).

Well, it is basic use-cases for tar command. You can check more by using man tar.
Thank you for reading!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s