How to remove a single file from tarball?

Yeah, it would be helpful to remove some unwanted files from the tarball which you already created. We already discussed the basic usage of Linux “tar” command to create archives from server CLI (command line interface). This is one of the most commonly using Linux command. This is very similar to the ZIP concept in Windows platform, what we are using commonly to save multiple file together.

You can refer to this topic for more details on this >> 15+ tar command usages with examples <<

The ‘tar’ saves many files together into a single tape or disk archive, and can restore individual files from the archive. It is very useful in such conditions like when we want to send a lot of files via email, transfer files from one machine to another etc.

Here is a trick to remove a file or some files from already created tarball. You can use the switch “–delete” to remove files from the tarball. Here I’m illustrating this with some examples.

1. Creating a tar archive

I created the following files to make a tar archive from shell.

# touch file{1..4}.txt

Creating TARBALL using tar command.

# tar -cvf crybit.tar file*

Which creates the tarball “crybit.tar” with the files file1.txt to file4.txt
To test the files in a tarball you can use “-t” switch with tar command.

# tar -tf crybit.tar

2. Removing a file from that tar archive.

Yeah, we can use the “–delete” switch with tar command to remove files from already created tar archive. Here we go!!

# tar --delete -f crybit.tar file4.txt

This command will remove “file4.txt” from the tar archive “crybit.tar.”

See that tar file now:

# tar -tf crybit.tar


Pattern match – Removing files using “–wildcards” options

This is a special feature to remove files using pattern match. See the usage pasted below:

# tar --wildcards --delete -f crybit.tar 'file*'

This will remove all files starting with file. That’s it!

Post navigation

Arunlal A

Senior System Developer at Zeta. Linux lover. Traveller. Let's connect! Whether you're a seasoned DevOps pro or just starting your journey, I'm always eager to engage with like-minded individuals. Follow my blog for regular updates, connect on social media, and let's embark on this DevOps adventure together! Happy coding and deploying!

Leave a Reply

Your email address will not be published. Required fields are marked *