Linux du Command
In general, the du command displays the details of disk space calculation of each file or directory. This command provides a number of parameter flags or options that allow you to view disk utilization in different formats.
The du command basic syntax is:
When the du command is used without any parameters, it displays the disk usage details of the current directory and its subdirectories in kilobytes.
Multiple files or folders names can be provided but, if you will not provide the file or directory name, it displays the disk usage size of the current directory.
What does du –exclude?
The du –exclude excludes the files or directories from disk usage calculations that match the pattern. Using the -X or –exclude option, we provide a specific file or directory pattern that we want to exclude. When we run the command, it excludes one file or multiple files that will match the specified pattern.
Exclude Single File
Let us take an example. For example, we want to exclude a file with the filename ‘test.txt’. To exclude this specific file, use the —exclude option with the du command in Linux in the following way:
Exclude Multiple Files with the Same File Format
We can also exclude the specific file formats from disk usage calculation using the du –exclude option.
For example, we need to display the whole directory size but exclude all ‘text (.txt) files from the disc usage calculations. As result of the –exclude option, all ‘.tar’ file types will be excluded except for those files that fit the specified search pattern. The following command displays disk usage information for all directories and files.
The above command will exclude files that end with the ‘.tar ’ extension during the disk usage calculation.
For example, we want to exclude a directory ‘snap’ from the disk space calculation. You can exclude this directory by specifying the path using the following syntax:
In the above command, we have excluded the ‘snap’ directory. It will display the disk usage information of each file or directory except snap.
Conclusion
In this tutorial, we studied how to use the Linux command du –exclude. If we wish to exclude certain files, such as ‘.txt’ or ‘.gz’, we must use the du command with the file/directory name and option. It will exclude all files that match the pattern from the disk storage calculation.