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.

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
file1.txt
file2.txt
file3.txt
file4.txt

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
file1.txt
file2.txt
file3.txt

Cool…

Pattern match – Removing files using “–wildcards” options

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