TAR Command Options in Linux - Explained with Examples

TAR is a very powerful Linux command that is generally used for compressing the multiple files into a single TAR file. However, apart from that, it can also be used for printing the contents of a TAR file, extracting the contents of a TAR file, etc.

Here at LinuxAPT, we shall look into the options that can be used with this command as well as a few examples in which this command can prove to be extremely helpful.


TAR Command Options

TAR Command Options can be seen once you run the below command:

$ tar --help

This command will display all the options available with the TAR command.


Examples of Using the TAR Command

1. Compress Multiple Files into a TAR File

Here, we compress the multiple text files into a single TAR file by running the following command:

$ tar –czvf files.tar.gz file1.txt file2.txt file3.txt

This command creates a TAR file named "files.tar.gz" and adds the text files named "file1.txt", "file2.txt", and "file3.txt" to this TAR file.


2. View the Contents of a TAR File

You can also display the contents of a TAR file by running the following command:

$ tar –tf files.tar.gz

The contents of the TAR file will be displayed on the terminal.


3. Add Another File to a TAR File

Here, we add a text file to an already existing TAR file by running the following command:

$ tar –rvf files.tar file4.txt

If the name of the file to be added appears in the output, it means that the specified file has been successfully added to the TAR file.


4. Remove a File from a TAR File

Here, we will remove a file from a TAR file by running the following command:

$ tar --delete –f files.tar file4.txt

This command removes the file named "file4.txt" from our TAR file.


[Need help in fixing Linux system issues ? We can help you. ]

This article covers the usage of the TAR command in Linux. In fact, the GNU tar (short for Tape ARchiver) command is the most widely used archiving utility in Linux systems. Available directly in the terminal, the tar command helps create, extract, and list archive contents.


How to tar a file in Linux using command line ?

  • Open the terminal app in Linux.
  • Compress an entire directory by running tar -zcvf file.tar.gz /path/to/dir/ command in Linux.
  • To compress a single file by running tar -zcvf file.tar.gz /path/to/filename command on Linux.
  • Tar and compress multiple directories file by running tar -zcvf file.tar.gz dir1 dir2 dir3 command in Linux.

Related Posts