tar exclude files & directories

Tar Directory Exclude Files & Folders

Tar is a popular Linux utility to archive one more files & folders in .tar files. It even allows you to compress them into .tar.gz files. By default, tar will include all files & folders present in your input folder. But sometimes while using tar command, you may need to exclude one or more files & directories. You may need to exclude hidden files and files based on names and patterns. You can easily do all this using tar command. In this article, we will learn how to exclude files & folders while using tar directory.


Tar Directory Exclude Files & Folders

Here is the basic command to tar a folder.

$ tar [options]  tar_file_name /path/to/folder

Here is an example to tar a folder /home/ubuntu/data to data.tar

$ tar  data.tar /home/ubuntu/data

Now if you want to exclude a file or folder present in /home/ubuntu/data folder include their path in –exclude option. Here is an example to exclude file projects.pdf from tar command.


Exclude Files in Tar

$ tar --exclude='/home/ubuntu/data/projects.pdf' data.tar /home/ubuntu/data

If you want to exclude multiple files include them one after the other with separate –exclude options as shown below.

$ tar --exclude='/home/ubuntu/data/projects.pdf' --exclude='/home/ubuntu/data/projects1.pdf' data.tar /home/ubuntu/data


Exclude Directory in Tar

If you want to exclude folders from tar command, you can also include them, just like files, in –exclude option. Here is a command to exclude /home/data/folder1

$ tar --exclude='/home/ubuntu/data/folder1' data.tar /home/ubuntu/data

Just like excluding files, if you want to exclude multiple folders, you can mention them one after the other with separate –exclude option for each directory. Here is an example to exclude /home/ubuntu/folder1 and /home/ubuntu/data/folder2.

$ tar --exclude='/home/ubuntu/data/folder1'  --exclude='/home/ubuntu/data/folder2' data.tar /home/ubuntu/data


Exclude Hidden Files

If you want to hide hidden files in tar, you can easily mention them by adding dot(.) at their beginning. Here is an example to hide .ssh file from tar.

$ tar --exclude='/home/ubuntu/.ssh' data.tar /home/ubuntu/data

If you want to hide all hidden files in your directory, you can simply use wildcard character * in your exclude option as shown below.

$ tar --exclude='.*' data.tar /home/ubuntu/data


Exclude Patterns

So far we have seen commands where we specify full paths to files and folders that need to be excluded from tar command. But you can also use patterns & regular expressions to exclude multiple files & folders at one go. Here is an example to exclude all .txt files from tar.

$ tar --exclude='*.txt' data.tar /home/ubuntu/data

Here is command to exclude .txt and .pdf files.

$ tar --exclude='*.txt' --exclude='*.pdf' data.tar /home/ubuntu/data

Please note, in some tar versions, you need to mention –exclude option after the .tar filename.

tar tar_filename --exclude=PATTERN1 --exclude=PATTERN2 /path/to/directory
tar tar_filename -X /path/to/exclude.txt /path/to/directory
tar tar_filename --exclude-tag-all=exclude.tag /path/to/directory

Of course, you can always use tar command with other options also, when you exclude files & folders. Here is an example where we create .tar.gz file using compression, as well as exclude certain files from tar command.

$ tar --exclude='*/pdf' -zcvf data.tar /home/ubuntu/data

In this article, we have learnt how to exclude files & folders in tar, with many examples and use cases. You can customize them as per your requirement. Tar is a useful command that allows you to easily archive and compress one or more files & directories into a single file. You can add the above commands into your shell script, if you want to automate tasks. You can also create a cronjob out of it, if you want to regularly create tar files for your purpose.

Also read:

How to Change Color Scheme in Vim
Python Script to Check URL Status
How to Create Executable in Python
How to Store JSON Data in MySQL
How to Fix Dpkg Was Interrupted Error in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *