Tar command Examples in Linux
Tar is one of the most widely used commands for creating and managing achieve files on Linux. This tutorial explains how to create, list, update, and manage archive files using it.
Basic concepts of the tar command
The tar command provides a straightforward method to manage multiple files as a single archived file for various reasons, such as backup and file transfer. Let us understand it through an example. You want to move several thousand files and directories from your computer to a remote computer. If you transfer all these files separately, it would be a tedious and time-consuming task. However, if you pack all these files in a single archived file, move the archived file to the remote server, and extract the archived file, it would be an easy task. The tar command creates a single archived file from several files or directories. A single archived file is easier to manage than many files, especially when moving from one location to another or backing up files.
The tar command examples
- Creating and listing archived file
- Adding new files to the existing archive file
- Deleting files from an archive file
- Updating files of an existing archive
- Listing an archive
- Extracting files and directories from an archive file
Access the shell prompt and create a directory. Switch to the created directory. Create some files and subdirectories for testing. You can also make some files in subdirectories.

Creating and listing archived file
The tar command uses the following syntax to create an archive and add multiple files and directories.
#tar -cvf [archived file name] [source files or directories name]
The following command creates an archive file data.tar and adds files a and b.
#tar -cvf data.tar a b
After creating the archive file, use the following syntax to list its contents.
#tar -tvf [archived file name]
For example, to list the contents of the archived file data.tar, use the following command.
#tar -tvf data.tar

This tutorial is part of the tutorial "The tar Command and its Options (c,v, f) Explained with Examples.". Other parts of this tutorial are as follows:
Chapter 1 Tar command options and syntax explained
Chapter 2 Tar command examples in Linux
Chapter 3 Tar command Compress and Extract Archives
Adding new files to the existing archive file
Use the options r (recursive), v (verbose), f(archived file) and specify the new file name as an argument to add a new file to an existing archive. For example, the following command adds the file c to the archive data.tar.
#tar -rvf data.tar c
The tar command adds the specified file at the end of the archive.

Removing or deleting files from an archive
To delete a file from the archive, use the option --delete. For example, the following command deletes the b file from the data.tar archive.
#tar --delete b -vf data.tar

Updating files of an existing archive
Use the option u to add only the files that have been modified or created since the creation of the archive. This option compares the supplied files and directories (as arguments) with those stored in the provided archive. If it detects any changes to any file or finds a new file or directory, it adds that file or directory to the end of the archive. This option does not replace the archive's existing file with the updated one. It adds the updated file at the end of the archive as a separate file. The original version of the file also remains in the archive.
This feature allows you to store multiple copies of the same file, where the first copy contains the original data, and all subsequent incremental copies contain the original data plus added or modified data since the last incremental copy. The t option lets you view how many incremental copies of a file the archive has. It lists the names of files and directories stored in the archive.
When extracting archives, the tar extracts files and directories in the same order in which they were added to the archive. By default, it extracts archives in overwrite mode. In this mode, if incremental copies of a file are available, the tar first extracts the original copy and then all the incremental copies in their respective order. This method overwrites the original file with the last available incremental copy of the file.
Let us take an example.
- Add a directory (that includes sub-directories and files) to an archive.
- After creating the archive, rerun the tar command with the u option to update the same archive. Since nothing has changed since the creation of the archive, the update process does not add anything to the archive.
- Change a file and rerun the update process. This time, since a file has changed, the update process adds the modified file at the end of the archive.
- List the archive's contents to verify the tar placed the updated file at the archive's end.
- Extract the archive and list the directory that contains the modified file.
- It should only contain the updated version of the file.

Listing the contents of an archive file or tarball
The listing enables you to see what files and directories an archive includes. For example, the following command lists the files and directories stored in the archive data.tar.
#tar -tvf data.tar

Extracting files and directories from a tarball or an archive file
The x option extracts the specified archive file. Use the f option to specify the file. You can also use the v option to view the real-time extraction process.
#tar -xvf test.tar
As mentioned earlier, the tar extracts an archive in the overwrite mode. If the destination directory already contains a file with the same name, the tar will overwrite the existing file if it has a file with the same name in the archive.
Let us take an example.
You added a file to an archive on Sunday. On Monday, you made some changes to the original file. On Tuesday, you extract the archive created on Sunday in the same directory.
What will happen in this case?
In this case, all changes made on Monday will lost. The tar will overwrite it with the file added to the archive on Sunday.

When extracting the archive, the tar command uses the same directory structure in which the file and directory were added to the archive. It first creates the directory and then extracts the files in the respective directories.

By default, tar extracts the archive in the current directory. It creates the same directory structure in the current directory and extracts files in that structure.

You have two options for extracting an archive in a specific directory.
- Switch to the destination directory before executing the tar command.
- Use the -C option to specify the destination directory path.

Conclusion
Whether you are a system administrator, developer, or casual user, mastering the tar command will enhance your ability to handle files and backups effectively. This tutorial explained the usage of the tar command through examples.
By ComputerNetworkingNotes Updated on 2025-11-25