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

How to create a tar archive excluding files by multiple filetypes

Create a tar archive called my-directory.tar of a directory my-directory/ excluding files by multiple filetypes.

You can use this short tar command.

              
                  tar --exclude='*.svn' --exclude='*.tmp' -cvf my-directory.tar my-directory/
              
          

Or this more verbose tar command.

              
                  tar --exclude='*.svn' --exclude='*.tmp' --create --verbose --file my-directory.tar my-directory/
              
          

Replace the *.svn and *.tmp in the above examples with whatever file extensions you need to prevent from being archived.


Options explained

  1. --exclude tells tar not to process files or directories that match the specified pattern
  2. -c or --create tells tar to create a new 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 for creating tar archives