For Linux users and administrators in a server-like environment, knowing the exact size of a directory tree through the system terminal is important. It will help you compare file directory properties and determine their storage allocation when you want to copy or move these directories to a different location.
du Command Examples
The command du is an abbreviation for disk usage. This command is case-sensitive. Be specific in the use of uppercase and lowercase naming conventions while referencing directory names.
Let us look at the various ways of implementing the du command to list directory sizes in Linux.
1. Listing Current Directory Size
Navigate to the directory you are interested in and key in the “du” command.
$ du
The default usage of the “du” command without other command arguments or options inside a targeted directory can be broken down into two parts.
- It lists the sizes of the sub-directories inside the main directory. From the above example screenshot, “LinuxShellTips” is the main directory while “screenshots” and “August” are its sub-directories. The size of each sub-directory is listed in kilobytes (kb).
- It lists the size of the main directory housing other sub-directories as the last command line output entry. From the above example case, the main directory “LinuxShellTips” has a total size of 12576 kb.
2. Listing Specific Directory Sizes
If you are inside one director and you want to know the size of a particular sub-directory, implement your du command in the following manner:
$ du screenshots/
If you are after multiple directory sizes, you can also list them:
$ du screenshots/ august/
The file directory sizes in your home directory can be achieved in the following manner:
$ du ~/Downloads/ ~/Documents/ ~/Desktop/
It will list all other associated sub-directories to their root level.
3. Listing Directory Sizes in Human-Readable Format
In the above-covered examples, the directory sizes were listed in kilobytes format. To get an output in a human-readable format, implement the du command with the "-h"
tag as a command argument:
$ du -h $ du -h screenshots
4. Listing Directory Sizes in Specified Formats
You can specify whether you want the targeted directory size display to be in kilobytes (k) or megabytes (m).
$ du -k August/ $ du -m August/
5. Listing a Directory Total Size
The du command is implemented by combining it with the "-s"
and "-h"
flags. In this case, the grand total size of the “LinuxShellTips” directory is 13M or 13 megabytes.
$ du -sh
For the combined grand total of targeted multiple directories, use the following command:
$ du -sh Documents/ Downloads/ Desktop/
The below command lists the sub-directory sizes of the targeted directories and sums them up to a final value.
$ du -ch Documents/ Downloads/ Desktop/
You can also combine the du command with the grep command, to sum up, a directory’s total size.
$ du -ch Documents/ | grep total
6. N-th Level Listing of Directory and Sub-Directory Sizes
This option is recommended for a Linux system with a large and deep-rooted directory tree structure. You might want to know the combined size of specific directories from N levels deep.
$ du -h --max-depth=2 ~/Downloads
The "--max-depth"
parameter specifies the depth you want to reach from the current directory structure. It could be 1, 2, 3, etc.
7. Listing and Sorting Directories Based on Size
This command helps you know the disk size usage of a parent working directory (pwd) directory from a specified sub-directory level. It sorts them by size.
$ du -h --max-depth=1 | sort -hr
8. Listing Directory and File Sizes
If you want your directory sizes list to be associated with directory files, include the "-a"
flag in your du command.
$ du -ah screenshots/
9. Listing Directory Sizes by Excluding File Types
Maybe you don’t want your listed directory size to account for any file with an extension like “.mp4”. You could implement this du command in the following manner from your parent working directory.
$ du -ch --exclude='*.mp4' | grep total
This tutorial has made listing directory and sub-directory sizes in a Linux environment is a non-issue. You now know more than one way to achieve this objective. For more references to the du command, key in man du on your Linux terminal.