Tar Command in Linux: A Step-by-step Guide to Mastery

tar command
tar command in linux

At Unixmen, we have been explaining each and every important Linux command in detail that would help the Linux community abundantly and one such crucial command is the tar command in Linux. Tar is short for “tape archive”. This command is used to create, maintain, modify, and extract all files in a Linux device that are archived in the tar format. In this guide, we will take you through important aspects like:

  • What is the tar command
  • Options in tar command
  • Practical use cases.

What is the Tar Command?

As with any Unixmen article, we are going to start with the foundation: what is the use of tar? The tar command is used to combine multiple files into a tarball. A tarball in the Linux community means a single archive file. Tarballs are helpful when you are creating backups, distributing files across a huge organization, and compressing. Here is the basic syntax of the tar command:

tar [options] [archive-name] [file or directory to be archived]

Common Options in Tar

To Create an Archive

Use “cvf” to create an archive file or a tarball containing multiple files. For example, if you would like to create an archive named “sample.tar” containing the files “hello” and “hola”, execute the following command:

tar -cvf sample.tar hello hola

To Extract Files from an Archive

Use “xvf” to extract files contained in an archive or a tarball. For example, to extract the files archived using the previous example, execute the following command:

tar -xvf sample.tar

To View the Files in an Archive

To just view the files contained in an archive or a tarball without extracting them, use “tvf”. In our example scenarios used above, execute the following command to view the files in the sample.tar archive:

tar -tvf sample.tar

To Compress Files Using Tar

There are three ways with which you can compress an archive using tar. They are:

  • tar -czvf sample.tar.gz hello: Creates a gzip compressed archive.
  • tar -cjvf sample.tar.bz2 hello: Creates a bzip2 compressed archive.
  • tar -cJvf sample.tar.xz hello: Creates an xz compressed archive.

To Add a File to an Archive

If you would like to add a file to an archive, use the syntax below:

tar -rvf sample.tar newfile

This command appends a file named newfile to an archive named “sample.tar”.

To Extract a Specific File from an Archive

Use the following command to extract a specific file from an archive containing multiple files:

tar -xvf sample.tar file1

To Add Multiple Files to an Archive

To add all files of “.txt” format to an archive, use the asterisk (*) wildcard. For example, the command that follows adds all .txt files to the sample.tar archive.

tar -rvf sample.tar *.txt

To Exclude Files from Archiving

Use the “–exclude” option to exclude specific files from rolling them into the archive. Here is the basic syntax to create an archive excluding a few files.

tar -cvf sample.tar sample --exclude='*.txt'

This command excludes all .txt files and archives other files in the folder into sample.tar tarball.

Create Archives Over SSH

For creating an archive of a remote directory over SSH, utilize the syntax that follows:

ssh user@RemoteHost "tar -czvf - /path/to/dir" > sample.tar.gz

Extract Archives Over SSH

To extract an archive to a remote server over SSH, utilize the syntax that follows:

cat sample.tar.gz | ssh user@Remote_Host "tar -xzvf - -C /path/to/destination"

Wrapping Up

The tar command is a helpful tool to anyone working with multiple files, logs, and directories in Linux. It can create, extract, and manage archives easily. This leaves very less for manual work, saving so much time for the users. By mastering this command in Linux, you can manage your files easily, ensure data is properly organized, and maintain a healthy storage capacity.

Happy Tar-balling!

Related Link

Tar manual from Linux.org

Some More Articles that would be of Interest to You