When using the du or diff command in the terminal, you may notice that it includes all directories and subdirectories by default. While this can be useful in some cases, there may be times when you want to exclude certain directories or subdirectories from the calculations. In this article, we will explain how to exclude directories and subdirectories from the du and diff command calculations.
Using the du Command
The du command, short for "disk usage," is used to estimate file and directory space usage. By default, it includes all directories and subdirectories in its calculations. However, you can exclude specific directories or subdirectories by using the --exclude option.
Here's the basic syntax of the du command:
du [options] [directories]
To exclude a directory or subdirectory, you can use the --exclude option followed by the path of the directory you want to exclude. For example, to exclude a directory named "logs" located in the current directory, you would use the following command:
du --exclude=./logs
If you want to exclude multiple directories, you can specify them one after another using the --exclude option. For example, to exclude both "logs" and "cache" directories, you would use the following command:
du --exclude=./logs --exclude=./cache
By using the --exclude option, the du command will exclude the specified directories and subdirectories from its calculations.
Using the diff Command
The diff command is used to compare files and directories. It also includes all directories and subdirectories by default. However, you can exclude directories and subdirectories from its calculations by using the --exclude option.
Here's the basic syntax of the diff command:
diff [options] [directory1] [directory2]
To exclude a directory or subdirectory from the diff command calculations, you can use the --exclude option followed by the path of the directory you want to exclude. For example, to exclude a directory named "logs" from the comparison between two directories, you would use the following command:
diff --exclude=./logs [directory1] [directory2]
Similar to the du command, you can exclude multiple directories by specifying them one after another using the --exclude option. For example, to exclude both "logs" and "cache" directories, you would use the following command:
diff --exclude=./logs --exclude=./cache [directory1] [directory2]
By using the --exclude option, the diff command will exclude the specified directories and subdirectories from its calculations.
Conclusion
By using the --exclude option with the du and diff commands, you can easily exclude directories and subdirectories from their calculations. This can be useful when you want to focus on specific directories or exclude directories that are not relevant to your analysis.
References
| Source | Link |
|---|---|
| du Man Page | https://man7.org/linux/man-pages/man1/du.1.html |
| diff Man Page | https://man7.org/linux/man-pages/man1/diff.1.html |