Tar Command in Linux


File compression is an important task in Linux that helps in data handling and moving it across various folders. The tar command or Tape Archive is used in Linux to archive and compress files. An archive file is a group of compressed files that make it easier to store and move them. A tar archive includes file system details like the structure of directories, user ownership, and file names.

In this tutorial, different ways and examples to use the tar command for file compression will be covered. We’ll also show you how one can use the tar command to make changes, and view and open these archive files.

Table of Contents:

tar Command in Linux

In Linux, we can use the tar command to back up and restore files or move a website to a different server. The tar command can compact archive files that are easy to transfer between different disks or computers. Tar commands have different options that can be used with it.

Syntax

The basic structure of the tar command is:

tar [options] [archive-file] [file/directory]

Here’s what each part of this command means:

  • options: These are the additional settings you can use to change how the tar command works.
  • archive-file: This is the archive file name you’re making or changing.
  • file/directory: This is the file or folder you want to add to the archive.

tar Command Options

There are several options available with the tar command to archive and extract files. You can also add new files to an existing archive, and leave out specific files. Some main options for the tar command include:

Options Description
tar -c Create and combine files and folders into a single archive.
tar -x It will extract files from a tar archive file.
tar -v Shows detailed information for each step of the archiving or extraction process on the terminal
tar -f Specifies the file name that you are going to archive
tar -A Combine several archive files into one
tar -z When creating a tar file, it compresses the file using gzip compression
tar -t This option lists the files and folders inside an archive
tar -u Adds new files or folders in an existing archive
tar -r Adds or updates files and folders in an existing archive
tar -W Verifies the integrity of an archive file.
tar -j This option constructs an archive file using bzip2 compression
–exclude Ignores files that are equivalent to the specified pattern
–delete Delete the file and directory from the compressed file
–wildcards This option specifies the patterns to select files for the UNIX tar command

To get further details of the tar command, use the help option with it:

tar --help

Creating a New tar Archive File

The command tar with the -c option creates a new archive file. For instance, we can create an archive file named file.tar in the current directory using this command:

tar -cvf file.tar *.txt

Note: Before running this command, make sure to create a test file or replace the name of your file in this command. I have created a new text file using the touch command.

Along with the -c option, we have also used two additional options here. The -v option will show the detailed output of the archiving process on the terminal, while the -f option is to set the name of the archive.

Creating a New Tar Archive File Using Gzip Compression

You can also compress the tar archives using Gzip compression. You can do this by naming the file with a suffix that shows the compression type. If you compress the files with gzip (using the -z option), then the file name should end in the tar.gz extension.

Let’s create a tar archive using gzip compression:

tar -cvzf file.tar.gz *.txt

Similarly, when you compress the file with bzip2 (using the -j option) compression, you need to add the tar.bz2 extension along with the file name:

tar -cvjf file.tar.bz2 *.txt

Extracting a tar Archive File

To extract the tar archive file, use the -x option along with the tar command. For instance, to extract the file.tar, you can use this command:

tar -xvf file.tar

Similarly, you can follow the same format for extracting the other tar file types, like the tar.gz and tar.bz2 files.

tar -xvf file.tar.gz

tar -xvf file.tar.bz2

Furthermore, you can also extract your tar file to a specified destination using the -C option followed by the path of the file. Let’s take this as an example:

tar -xvf file.tar -C /home/linux/Downloads

The above command will unarchive the contents of the file.tar into the specified directory, which is inside the documents.

We can also extract particular files from tar archive files by specifying the names of the files that we need to extract from an archive file. For example, for extracting file names file1.txt and file3.txt from the file.tar archive, we can run this command:

tar -xvf file.tar file1.txt file3.txt

Note: Before running the above command, I have created three text files using the touch command and created a new tar file that contains all those three text files. The command for tar file creation is:

tar -cvf file.tar file1.txt file2.txt file3.txt

Updating Existing tar Archive File

To update the already created tar archive file, you can use the -r option with the tar command. For instance, if you have a new file4.txt file, and you want to add this to your existing tar archive file, you can update your file by running this command:

tar -rvf file.tar file4.txt

After adding the text file to an existing tar file, I have extracted it, and you can see the fourth text file is successfully added to it.

Listing Contents of Tar File

To list or view what’s inside the file.tar archive file, simply use tar with the -t option. This command will show all your archive file content on the terminal:

tar -tvf file.tar

Deleting Files From a tar Archive File

To delete a specific file or folder from the existing tar file, use the –delete flag with the tar. Consider the below command to delete the file2.txt from the main archive file:

tar --delete -f file.tar file2.txt

After deleting, list the file.tar archive file contents to check if the file2.txt was deleted or not:

tar -tvf file.tar

Conclusion

The tar command is a command line utility in Linux that is used for archive management. Using this you can perform operations like creating, viewing, and extracting contents from archive files. It has support for multiple formats like tar.gz and tar.bz2 files. Several options are also available to perform custom operations like deleting specific files, listing tar file content, or adding a new file to an existing tar archive file. All these operations of tar command are covered in this article.

Print Friendly, PDF & Email
Categories