How to Use Shell Command to tar Directory Excluding Files/Folders?

Linux provides pre-installed “tar” command to save the files/directories in an archive file using different compression formats such as .gz, .bz2, .xz, and so on. It consumes less disk storage space than original files/directories and can be shared easily. 

By default, the tar utility creates an archive of the directory “recursively,” including its whole content. However, it can be used to exclude unnecessary files/ directories that are not required in the future.

This post describes how to use a shell command to tar a directory excluding certain files/folders:

  • Using tar “–exclude” Argument
  • Using Separate Exclude Text File

Method 1: Using tar “–exclude” Argument

The “tar” command offers the “–exclude” argument to exclude/eliminate the desired files or directories during the compression/extraction of file/directory. It follows the following generalized syntax to perform this task.

Syntax

$ tar --exclude="pattern" [options] [archive_name] [path]

The above syntax holds the following parameters:

  • Tar: Represents the main “tar” command to compress and decompress the file/directory.
  • exclude: Eliminates the specified “pattern” that is a file/directory.
  • Options: Supported tar arguments to compress and decompress the mentioned file.
  • archive_name: Specifies the name of the archive having extensions .gz/xz/.bz2 and so on.
  • path: Specifies the absolute path of the file/directory that needs to be compressed/decompressed.

Let’s see its practical implantation with the help of an example.

Example:

There is a “Documents” directory having the following files and subdirectories listed by the “ls” command followed by the “-l(displays all parameters)” flag:

$ ls -l Documents

To tar/compress the whole “Documents” directory without “File1.txt” execute this command:

$ tar --exclude="File1.txt" -Pczvf dir.tar.gz /home/itslinuxfoss/Documents

The supported options used in above command are:

  • P: Ignores the checking of leading characters(/) of the path.
  • c: Creates the archive of the mentioned directory.
  • z: Uses to compress a directory in “gzip” compression format
  • v: Displays the details of the command execution
  • f: Specify the filename of the archive file.

The above output shows that the “Documents” directory has been compressed as an archive file named “dir.tar.gz” excluding “File1.txt”.

List tar Directory Content

For more verification, the user can use the “t(viewing the archive content)”, and “f(archive filename)” options of the tar command to list the tar directory content:

$ tar -Ptf dir.tar.gz

The above “dir.tar.gz” list verifies that there is no “File1.txt”.

Multiple Files/Folders

The “–exclude” argument is also useful to exclude more than one file/subdirectory during particular directory compression. 

Specify the targeted files and directories name with the “–exclude” command one by one in this format:

$ tar --exclude='Dir1' --exclude='Dir2' --exclude='Dir3' --exclude='File2.txt'-Pczf dir1.tar.gz /home/itslinuxfoss/Documents

The output shows that the specified files and subdirectories of “Documents” have been excluded in tar “dir1.tar.gz”.

Use File Extension

The user can exclude the files easily if all have the same extension. The “–exclude” argument supports the “*(asterisk)” character that represents “all” to exclude all files of the same extension.

In this scenario, all the “.txt” files are specified to exclude. In contrast, the compression of the “Documents” directory:

$ tar --exclude="*.txt" -Pczf dir2.tar.gz /home/itslinuxfoss/Documents

The “v” flag identifies that no “.txt” file is compressed in the archive “dir2.tar.gz”.

Method 2: Using Separate Exclude Text File

To specify the exclude files and subdirectories one by one is a time taking task. Create a file and add all those files and subdirectories you want to exclude during the directory compression. Let’s see it with the following example.

Create an Exclude Text File

Execute the easiest and majorly used “cat” command to create the file named “Excludefile.txt.” Write the name of the excluded files and subdirectories in it:

$ cat ExcludeFile.txt

Press “Ctrl+d” to save the file and exit the “cat” command.

Tar a Directory

Now execute the “tar” command and specify the created “Excludefile.txt” with the “–exclude-from” argument: 

$ tar --exclude-from="Excludefile.txt" -Pzcvf compdir.tar.gz /home/itslinuxfoss/Documents

The above command successfully excluded all the files and subdirectory from compressed “compdir.tar.gz” archive mentioned in “Excludefile.txt.”

Add File Extension into Exclude Text Files

Apart from files and directories; the file extension can also be specified in text files with “*” for all in this way:

$ cat Ex_File.txt

The “Ex_File.txt” excluded text file has been created having “*.txt” for all “.txt” extension files

Run the “tar” command to create a “comdir” archive of “Documents” directory followed by “–exclude-from” argument having “Ex_File.txt”:

$ tar --exclude-from="Ex_File.txt" -Pzcvf compdir.tar.gz /home/itslinuxfoss/Documents

The newly created “compdir” doe snot contains any “.txt” file with the help of “–exclude” argument.

Conclusion

Linux shell command “tar” supports the “–exclude” argument to exclude certain files/directories by placing them one after the other. Instead of specifying unnecessary files/directories one by one, the user can add them in an Exclude-text file and mention it with the “–exclude” argument.

This post has covered the complete procedure to tar a directory excluding certain files/folders using the shell command.