Black Friday Deals for Linux Users are Now Live!
Learn more
Nov 6, 2022 3 min read

Exclude Files and Directories While Creating Tar File

Table of Contents

The tar command is used for creating archive files from various files and folders.

It's quite simple when you have all the required files in one place. But what if you want to put some files or folders in the tar file and exclude some?

One way would be to copy those required files into a new directory and then proceed with the standard procedure, but it is not an efficient way.

Instead, you can tell the tar command to exclude the undesired files in this manner:

tar --exclude="File_to_exclude" [options] [archive_name] [path]

By the way, that's not the only way to do it. Let me show you a couple of methods here.

But before I jump to the guide, I'd like to show you the directory containing multiple files and sub-directories which I'm going to utilize in this guide:

use tree command in linux

Now, let's proceed with the first method.

Method 1.  Using -exclude option

To use the -exclude option, you'd need to follow the given command syntax:

tar --exclude="File_to_exclude" [options] [archive_name] [path]

For example, I'll be creating a tarball named IHateC excluding HelloWorld.c file:

tar --exclude='HelloWorld.c' -zcvf IHateC.tar.gz .
exclude files while creating tar files in linux using --exclude option

First, let me break down the used options here:

  • z uses the gzip utility to compress files in the archive.
  • c is responsible for creating a new archive file.
  • f allows users to specify the name of the new archive.
  • v (verbose) gets us a list of processed files and directories to create archive files.

And the . used at the end of the command indicates I want to utilize the current working directory.

To check the contents of the tar file, you can use the tar command with the -tf option:

tar -tf IHateC.tar.gz
list contents of tar file in linux

but what about excluding multiple files and directories? Here you go.

Excluding multiple files and directories

To exclude multiple files and directories, you can use the chain of --exclude.

For example, I'll disclude Bash.sh and the subdirectory named Sub-Directory-1:

tar --exclude='Bash.sh' --exclude='Sub-Directory-1' -zcvf NoDir.tar.gz .
exclude multiple files and directories while creating tar file in linux

But this command can get too long if you're supposed to exclude various files. And if they have the same extension, things can get a lot easier!

Use file extension to exclude files

You just have to pair the file extension with the --exclude and it will get your job done.

For example, I want to exclude every .mp3 file while creating tarball, then I'd have to follow the given command:

tar --exclude='*.mp3' -zcvf NoMp3.tar.gz .

Method 2. Using a text file to exclude files and directories

In this method, I'll be creating a text file containing the names of files and directories that I want to exclude.

So I made a text file containing 3 filenames that I want to exclude named exclude.txt :

list contents of file using cat command in linux

So now I only have to use that text file to exclude files:

tar -zcvf NewFile.tar.gz --exclude-from="exclude.txt" .
use text file to exclude multiple files and directories while creating tar file in linux

And that's it!

Final Words

This was my take on how you can exclude files and directories while creating tar files in the easiest way possible.

And if you have any queries, you're welcome to extract them in the comments!

Linux Foundation Training
Sagar Sharma
A software engineer who loves to tinker with hardware till it gets crashed. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.
Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Linux Handbook.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.