Linux / Unix: Tarball Delete ( remove ) File

How do I delete OR remove a single file from within a tar ball under Unix / Linux like operating systems?

You can use the following syntax to delete a file from a tar ball:

ADVERTISEMENTS

tar --delete -f example.tar filename

Example: Delete a File From a Tar Ball

First, create a tar ball for testing purpose using the tar command, enter:
# cd /tmp
# tar -cvf foo.tar /etc

To display a list of files, enter:
# tar -tvf foo.tar
OR
# tar -tvf foo.tar | grep 'etc/resolv.conf'
Sample outputs:

-rw-r--r-- root/root        30 2011-09-20 17:51 etc/resolv.conf.tmp
-rw-r--r-- root/root       185 2011-07-27 20:35 etc/resolv.conf
-rw-r--r-- root/root        185 2011-07-27 20:35 etc/resolv.conf.pppd-backup.ppp0
-rw-r--r-- root/root         51 2011-07-12 03:14 etc/resolv.conf~

To delete a file called ‘etc/resolv.conf’ from a tar ball called foo.tar, enter:
# tar --delete -f foo.tar etc/resolv.conf
Verify that file has been deleted from the tar ball:
# tar -tvf foo.tar | grep 'etc/resolv.conf'
Sample outputs:

-rw-r--r-- root/root        30 2011-09-20 17:51 etc/resolv.conf.tmp
-rw-r--r-- root/root        185 2011-07-27 20:35 etc/resolv.conf.pppd-backup.ppp0
-rw-r--r-- root/root         51 2011-07-12 03:14 etc/resolv.conf~

To delete all etc/resolv.conf files, use the –wildcards opitons. It will enable pattern matching:
# tar --wildcards --delete -f foo.tar 'etc/resolv.conf*'
Use the following command to verify that files deleted from the tar ball:
# tar -tvf foo.tar | grep 'etc/resolv.conf'

Please note that you are not allowed to modify or delete files from compressed archives such as tar.gz or tar.bz2.

See also:

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
Network Utilitiesdig host ip nmap
Package Managerapk apt
Processes Managementbg chroot disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w

ADVERTISEMENTS
2 comments… add one
  • Tim Sep 25, 2011 @ 22:07

    I didn’t know this could be done. Unfortunately, it isn’t available in Mac OS X.

  • Evan Oct 28, 2011 @ 19:46

    “Please note that you are not allowed to modify or delete files from compressed archives such as tar.gz or tar.bz2.”

    Of course this can be worked around with pipes. An example with an archive made with tar cJf Compress.tar.lzma

    xzcat Compress.tar.lzma | tar --delete -O -f - deleteFile.txt | xz - > Compress.tar.lzma

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.