All commands tested on Mac OS Monterey / Jul 02, 2024

How to delete files by filetype from an existing tar archive

Deletes files by filetype from an existing tar archive.

You can use this short tar command.

              
                  tar --exclude 'files/**/*.min.*' -cvf --file temp.tar @files.tar && rm files.tar && mv temp.tar files.tar
              
          

Or this more verbose tar command.

              
                  tar --exclude 'files/**/*.min.*' --create --verbose --file temp.tar @files.tar && rm files.tar && mv temp.tar files.tar
              
          

What's heppening here?

We want to remove the minified files (*.min.*) from files.tar. Whereas some tar implementations support the --delete option, for those that don't (Mac OS, for sure), here's how this works:

  1. Create a new tar archive temp.tar with the contents of files.tar, excluding files with a *.min.* extension.
  2. Remove the original tar archive.
  3. Rename the new temp archive to the name of the original.

Note: Because we're deleting the original tar file, do ensure to take a backup before you do this.


Options explained

  1. --exclude tells tar not to process files or directories that match the specified pattern
  2. u or --update tells tar to add to the archive if the modification time is newer than in the archive
  3. v or --verbosetells tar to produce verbose output
  4. f or --filetells tar that the next parameter will be the tar file to create, extract, append to, list, etc.

More commands to update tar archives