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.

TecMint Weekly Newsletter
Get the Learn Linux 7 Days Crash Course free when you join 34,000+ Linux professionals reading every Thursday.
Check your email for a magic link to get started.
Something went wrong. Please try again.

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.

If this article helped, with someone on your team.

TecMint Weekly Newsletter
Get the Learn Linux 7 Days Crash Course free when you join 34,000+ Linux professionals reading every Thursday.
Check your email for a magic link to get started.
Something went wrong. Please try again.
TecMint has been free for 14 years. Help keep it that way.
Google AI Overviews and tools like ChatGPT have cut into search traffic for independent tech sites like TecMint. Running this site costs over $2,000 every month for hosting, infrastructure, and paying authors to keep the content accurate and tested.

If this article helped you solve a problem, consider buying a coffee. It helps keep TecMint free, supports the authors, and keeps the project going.
☕ Buy Me a Coffee
Ravi Saive
I'm Ravi Saive, an award-winning entrepreneur and founder of several successful 5-figure online businesses, including TecMint.com, GeeksMint.com, UbuntuMint.com, and the premium learning hub Pro.Tecmint.com.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.

Free Course
Get a free Linux course before you go.
Subscribe to TecMint Weekly and get the Learn Linux 7 Days Crash Course free. Read by 34,000+ Linux professionals every Thursday.
Something went wrong. Please try again.
Check your email for a magic link to get started.