Linux Du Command Examples
The du Linux command is very popular for UNIX and Linux systems. This command is used to view the information about disk usage of all directory tree and files on a machine. This command has various parameter flags or options that helps to view the information of disk usage in many different formats. Using the du command, you can also display the files and directory size details in a repeated manner.
We will give a brief demo in this article on the different uses of Linux du command with examples.
Display Summary of Disk Usage
Using the ‘du’ command, the complete disk usage summary including all sub-directories can be displayed by running the following command:
The following output displays the information about the total number of disk blocks along with all sub-directories of the ‘/home/kbuzdar’ directory.
Use of Du Command with Various Flags
Below are the following options that you can use with the du command:
Use of -h flag with Du Command:
When the du command is used with the ‘-h’ flag, it shows the disk usage summary in a human-readable format. The block size information is displayed in bytes, kilobytes (K), megabytes (M), etc.
Use of -s flag with Du Command:
Use option ‘-sh’ with du command to display the total disk usage size of a directory as follows:
Use of -a flag with Du Command
To display the disk usage details of directories and all files, use the option ‘-a’ with the du command.
To view the disk usage information of all directories and files in a human-readable format, use the option ‘-a’ along with the ‘-h’ flag as follows:
Display Disk Information in Kilobytes / Megabytes
To display the disk usage information of a directory tree included with all sub-tree directories in kilobytes (1024 bytes), use the option ‘-k’ with du command as follows:
Similarly, use option ‘-mh’ for megabytes and human-readable format to display the disk usage of all directory tree.
Display the Disk Usage Total Size
Use the ‘-ch’ flag with the du command to get the total disk usage space information at the last line.
Exclude Specific File Format
The following command displays the disk usage information of all directories and files except those files that match the given search pattern. For example, we want to exclude all text files while displaying the total directory size. So, it will exclude all .txt file formats through the –exclude flag.
The following output will be shown on the terminal:
Display Disk Usage Detail with Timestamp
To display the disk usage information along with the timestamp, use the –time flag along with the du command as follows:
We have explained the different uses of du command with examples in this article. The Linux du command helps users to get details of disk usage in a human-readable format so that the user can easily understand the system’s disk usage summary.