How to Exclude Files and Directories When Creating a tar File

We are aware of creating a tar files but in this blog we will learn on how to exclude files and directories when creating a tar File.

On Linux and Unix-based systems, files can be archived using the tar program. Numerous archive formats, including tar, tar.gz, cpio, tar.bz2, zip, and rar, are created using it. When creating.tar.gz files, the command employs the gzip algorithm; when creating.tar.bz2 files, it utilizes the bzip algorithm. You may check GNU Tar

The various techniques to exclude files and directories when creating a tar file will be the main topic of this article.

Using –exclude options

The tar –exclude option has this basic syntax

When building a.tar.gz archive, we’ll use the –-exclude option to skip a file or directory:

Let me try to explain the above example:

  • -z: uses gzip to compress the files and folders.
  • -c: To generate a new archive file
  • -v: provides a verbose summary of the files and directories processed;
  • -f: lets us choose the filename of the archive that is formed;
  • –exclude: while producing the archive, it excludes the file1

The path to the current working directory containing the files we wish to archive is indicated by the “.” at the end of the command.

You may also use this command to list the contents of the backup.tar.gz file without extracting it:

Exclude multiple files and directories

By chaining many -–exclude arguments together, we may exclude more than one file or directory:

As an alternative, we may alternatively provide the following format for the files and folders to be excluded:

Curly braces on the terminal can occasionally cause issues when using Bash functions. With other systems, this form of the –exclude option might not function consistently.

Excluding Files With a Specific Extension

We can even pass patterns to exclude particular file extensions:

With the aforementioned command, all files ending in.txt are skipped.

With the built-in parameters of the tar program, we may choose which files and directories to ignore which are occasionally produced automatically. Let’s examine a few of these choices and the roles they play:

  • –exclude-backups To exclude all backup and lock files,
  • –exclude-caches: To exclude all backup and lock files, -exclude-caches: removes all directories that have a CACHEDIR.TAG, with the exception of the tag itself;
  • –exclude-vcs : Removes all files related to version control systems;
  • –exclude-vcs-ignores: removes files that correspond to certain patterns associated with version control system ignore files.

Excluding Files & directories using a exclude_file

When generating or extracting archive files, we may give the tar command a file that contains a list of files or directories to exclude. We refer to this file as an exclusion file.

Let’s look at how to utilize an exclusion file to omit particular directories and files from the archive process.

We’ll start by creating a file called exclude_file.txt:

You can now add all the files and directories which you want to be excluded

Now Save the file and exit. Now when you run the below command and use the above exclude_file it will skip all the files and directories listed.

We used the -X option to add a file to be excluded. This is same as to use –exclude-from option.

We may add file extension patterns to the exclusion file to be skipped by using the tar command.


Let’s adjust the content of exclude_file content to reflect this:

Now We’ll attempt to compress the files once more after updating the file:

The above will skip all the files with the extension .txt

Conclusion

In this post, we looked at a few ways to exclude particular files or directories from a.tar.gz archive. In situations where we need to quickly produce an archive inside a tiny directory tree, using the –exclude option might be helpful. As an alternative, when there’s a sizable directory tree with thousands of files and directories, it’s helpful to use an exclusion file. You may also like to check How to extract Tar files in Linux

Leave a comment