Tar, short for Tape Archive, has been a staple in the world of Unix-like operating systems for decades. It’s a versatile tool used to compress and bundle files and directories into a single archive file. While creating tar archives is straightforward, mastering the art of exclusion – selectively including or excluding files and directories – can greatly enhance your archiving skills.
In this article, we will explore exclusion techniques in tar archives through real-world examples, equipping you with the knowledge to efficiently manage your data.
Excluding Files and Directories
- Excluding Specific Files
You can exclude specific files by providing their paths as arguments to the tar command. For example:
tar -cvf archive.tar --exclude=/path/to/exclude/file.txt /path/to/source
- Excluding Files by Pattern
Tar also supports excluding files based on patterns using the
--exclude
flag. Here’s an example:tar -cvf archive.tar --exclude='*.log' /path/to/source
This command excludes all files with the “.log” extension from the archive.
- Excluding Entire Directories
To exclude entire directories, use the
--exclude
flag followed by the directory path:tar -cvf archive.tar --exclude=/path/to/exclude/directory/ /path/to/source
- Excluding Multiple Files and Directories
You can exclude multiple files and directories by providing multiple
--exclude
flags:tar -cvf archive.tar --exclude=/path/to/exclude/file.txt --exclude=/path/to/exclude/directory/ /path/to/source
Real-World Examples
Let’s explore some real-world examples to demonstrate how exclusion techniques can be used effectively.
Example 1: Creating a Backup Archive
Suppose you want to create a backup of your home directory but exclude log files and temporary directories:
tar -cvf backup.tar --exclude='*.log' --exclude='/home/yourusername/tmp/' /home/yourusername/
This command will create a tar archive of your home directory while excluding all log files and the “tmp” directory.
Example 2: Archiving a Web Project
You’re working on a web project and want to create an archive, but you need to exclude the “node_modules” directory and all “*.log” files:
tar -cvf project.tar --exclude='*.log' --exclude='/path/to/node_modules/' /path/to/your/project/
This command will create an archive of your web project, excluding log files and the “node_modules” directory.
Conclusion
Mastering exclusion techniques in tar archives is a valuable skill for efficient data management. With the ability to include or exclude specific files and directories, you have full control over the contents of your archives. By understanding the syntax and applying real-world examples, you can optimize your archiving process to suit your needs, whether it’s creating backups, distributing software packages, or archiving web projects. Tar archives, when unleashed with exclusion techniques, become powerful tools in your data management arsenal.
3 Comments
hi, how to exclude list of patterns get from outside file to make it short and suitable for scripts because of curly brackets involved ?
same as rsync –filter /tmp/some-filters-masks
You are excluding through keyword. Suppose, if I want to exclude a particular path folder only.
Then what would be option?
Had the same question. The solution I found is:
1. use the absolute path to the folder you want to exclude
2. put the –exclude statement at the beginning of the command (before the “czf” stuff)