How to Add, Delete and Update Files in Tar Archive in Linux

In the realm of file archiving and compression, the tar command stands as a versatile tool for creating, managing, and manipulating archives in Linux systems.

In this article, we will delve into the process of adding, deleting, and updating files within a tar archive, exploring the steps and commands needed to perform these tasks.

Adding Files to a Tar Archive

Adding files to an existing tar archive can be extremely useful when you want to update or expand the contents of an archive without creating a new one from scratch.

To achieve this, you can use the -r or --append option followed by the name of the tar archive and the list of files you want to add.

$ tar -rvf tecmint.tar ravi1.txt ravi2.txt ravi3.txt

In the above command, -r specifies that you want to append files to the archive, -v enables verbose output for tracking progress, and -f specifies the name of the tar archive.

To view the contents of a tar file called tecmint.tar, you would use the following command:

$ tar -tvf tecmint.tar 
Add Files to Tar Archive
Add Files to Tar Archive

Deleting Files from a Tar Archive

Sometimes, you might want to remove specific files from an existing archive to keep it up to date or reduce its size. This is where the --delete option is used to delete a file from an archive as shown.

$ tar --delete -f tecmint.tar ravi1.txt ravi3.txt
$ tar -tvf tecmint.tar 

In this example, the tar command will extract the archive, remove the specified file, and then re-create the archive without the deleted file.

Delete Files From Tar Archive
Delete Files From Tar Archive

Updating Files in a Tar Archive

Updating files within a tar archive involves a two-step procedure: extracting the archive, modifying the desired files, and then re-creating the archive. This method empowers you to fine-tune the contents without altering the entire archive.

$ tar -xf tecmint.tar ravi2.txt [extracting archive]
$ tar -uvf tecmint.tar ravi2.txt [updating archive]
Update File in Tar Archive
Update File in Tar Archive
Conclusion

The tar command’s ability to add, delete and update files within tar archives offers a dynamic way to keep your archives current and relevant.

Whether you’re updating files, removing obsolete data, or optimizing archive sizes, understanding how to utilize the tar command’s features can significantly enhance your data management skills.

By following the steps outlined in this article, you can confidently add, delete and update files within tar archives, thereby enhancing the efficiency and effectiveness of your digital storage endeavors.

Similar Posts