How to Exclude Files and Directories in Tar

Last updated: November 14, 2022

The tar command is one way of creating archive files and directories in Linux. Tar creates an archive file of different formats, including .tar, .zip, .tar.gz, .tar, etc.

In this guide, we learn how to exclude files and directories in tar.

tar --exclude Option

tar uses the --exclude option to skip files and directories when archiving. It can match a specific pattern and ignore it. The pattern can have wildcard, filename(s), directory, or absolute path.

Here's the syntax.

tar --exclude="PATTERN" [options] [archive-name] path

Exclude files matching PATTERN, a glob(3)-style wildcard pattern.

tar exclude examples

Let's have some useful examples to understand the tar exclude option.

Exclude a Directory

To tar exclude a directory use the following syntax:

tar --exlcude="/path/to/directory-to-exclude" -zcvf filename.tar.gz /path/to/destination-directory/

Note: Remember to remove the trailing slashes at the end of excluded folders.

For example, the following tar command excludes the directory named tar-demo in the current directory.

tar --exclude='tar-demo' -zcvf output1.tar.gz .
exclude a directory


  • The period (.) represents the current working directory.
  • -c creates the new tar file.
  • -f allows specifying the directory name.
  • -z represents gzip.
  • -v represents verbose.

You can verify that the tar file doesn't contain the excluded directory.

tar -tf output1.tar.gz
verify tar exclude

Exclude Multiple Directories

Multiple directories can be excluded by explicitly adding multiple --exclude options.

Note: All the directories must be on the same path for this to work.


tar --exclude='./myfolder' --exclude='./upload/myfolder2' -zcvf backup1.tgz .

The tar command excludes the directories named ./myfolder and the subdirectory named /upload/myfolder2 from the current directory.

exclude multiple directories by placing inside curly braces

Alternatively, you can specify the directories in curly braces to exclude them using one pattern. Excluding directories utilizing this method may not work with bash functions.


tar --exclude={"./myfolder","./upload/myfolder2"} -zcvf ./backup/backup2.tgz .
exclude multiple directories by placing inside curly braces

Exclude Multiple Files

You can tar exclude multiple files the same way did for directories.


tar --exclude='' --exclude='new.txt' -cvf filedemo.tar .
tar --exclude={'','new.txt'} -cvf filedemo.tar .
exclude multiple files

Version control file such as .git can be excluded as follow

tar --exclude .git -czvf filename.tar.gz .

Exclude Using Wildcard

Using wildcard it comes in handy to remove filename with suffixes such as png, txt or mp3


tar --exclude='*.txt' -cvf excluded.tar .
exclude files using wildcard

You can skip tar different file types as follows.

tar --exclude={'*.txt','*.sh'} -cvf excluded.tar .

For excluding all hidden files you can use,

tar --exclude=".*" -cvf excludedhiddenfile.tar .

For GNU tar it has some additional features to skip other auto-generated files and directories using the built-in options.

  • --exclude-backups: It is used for skipping backup and lock files.
  • --exclude-caches: It is used to exclude directory containing the CACHEDIT.TAG apart from the tag itself.
  • --exclude-vcs: It is used to skip all files of the version control systems.
  • --exclude-vcs-ignores: It skips vcs ignore files and directories.

Exclude by a File List

The tar command can also take an exclude file as input using the -X option. This file contains a list of files and directories to be excluded when creating archive files.

In the exclude file, each file or directory name should be separated by a newline.


tar -zcvf test.tar.gz -X exclude-list.txt .
exclude a list of files and directories mentioned in a file


Tar excludes often comes helpful when you have to backup and need to ignore some files or directories. This could be your dot file, hidden files or version control files, or more.

And few points to keep in mind:

  • Some version of the tar requires keeping --exclude at the beginning of the tar command to work.
  • To match the entire directory use /path/directory not /path/directory/ - Should remove the trailing slash.

About The Author

Bobbin Zachariah

Bobbin Zachariah

Bobbin started his career in IT in the year 2000 as a computer science instructor and worked as a Linux system engineer and senior analyst roles. Currently working in DevOps environments to increase efficiency and improve delivery time in AWS Cloud infrastructure. He is certified in RHEL, CCNA, and MCP and holds a Masters's in computer science. When in his free time love playing cricket, blogging, and listening to music.



Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment