All commands tested on Mac OS Monterey / Jan 05, 2022

How to append files of a specific filetype to an existing tar archive

Add all the *.txt files in a directory to the existing files.tar archive.

You can use this short tar command.

              
                  tar -rvf files.tar ./my-directory/**/*.txt
              
          

Or this more verbose tar command.

              
                  tar --append --verbose --file files.tar ./my-directory/**/*.txt
              
          

The **/*.txt format does the recursion. Ie. finding all the .txt files at any level in the directory structure before appending them to the archive.

Note: There are alternative ways to build a list of files by filetype using the find command and then piping that into tar. However, if you want to achieve this with tar only, the above command should work on your tar implementation.


Options explained

  1. -r or --append tells tar to append to an existing archive
  2. v or --verbosetells tar to produce verbose output
  3. f or --filetells tar that the next parameter will be the tar file to create, extract, append to, list, etc.

More commands for appending to tar archives