How to compute the total size of files included by tar after exclude flags
When working with tar, a common task is to create an archive of files and directories. Tar provides various options to include or exclude specific files or directories. However, it can be tricky to determine the total size of the resulting archive after applying exclude flags. In this article, we will explore how to compute the total size of files included by tar after excluding certain files or directories.
Understanding Tar Exclude Flags
Tar is a command-line utility that allows you to create, extract, and manipulate archive files. It supports several options to include or exclude specific files or directories during the archiving process. The exclude flags in tar are used to specify patterns or filenames that should be excluded from the archive.
For example, if you want to exclude all files with the .txt extension, you can use the --exclude='*.txt' flag. This will prevent any files matching the pattern from being included in the archive.
Computing the Total Size
To compute the total size of files included by tar after applying exclude flags, you can use the du (disk usage) command. The du command provides information about the disk usage of files and directories.
First, create the tar archive with the desired exclude flags:
tar -cf archive.tar --exclude='*.txt' directory
Once the archive is created, you can use the du command to determine its total size:
du -sh archive.tar
The -s option in du displays only the total size of the specified file or directory, while the -h option prints the size in a human-readable format (e.g., kilobytes, megabytes, gigabytes).
Running the above command will output the total size of the tar archive after excluding the specified files or directories.
Example
Let's consider an example to illustrate the process. Suppose you have a directory named documents that contains multiple files and subdirectories. You want to create a tar archive of this directory but exclude all files with the .docx extension.
The command to create the tar archive with the exclude flag would be:
tar -cf archive.tar --exclude='*.docx' documents
After executing the above command, you can use the du command to compute the total size of the resulting archive:
du -sh archive.tar
The output will display the total size of the tar archive, excluding the .docx files.
Conclusion
Calculating the total size of files included by tar after applying exclude flags can be accomplished using the du command. By combining the power of tar's exclude flags and the disk usage command, you can efficiently manage the size of your tar archives and exclude specific files or directories as needed.
References
| Source | Description |
|---|---|
| GNU Tar Documentation | Official documentation for GNU Tar |
| du(1) - Linux man page | Manual page for the du command in Linux |