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

How to create a tar archive of files by multiple filetypes

Create a tar archive called images.tar of image files in directory/ (at all directory levels).

You can use this short tar command.

              
                  tar -cvf images.tar directory/**/*.jp*g directory/**/*.png directory/**/*.gif
              
          

Or this more verbose tar command.

              
                  tar --create --verbose --file images.tar directory/**/*.jp*g directory/**/*.png directory/**/*.gif
              
          

The **/*.jp*g, **/*.png etc. format does the recursion. Ie. finding JPEG's, PNG's etc. at any level in the directory structure.

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. -c or --create tells tar to create a new 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 creating tar archives