The du command is a handy tool for Linux users that allows you to analyze how much space individual files and directories are taking up on your file system. It is also straightforward to use and it will quickly provide you with the size of everything in the current directory, including subdirectories. The following tutorial will demonstrate common examples of the du command in a Linux terminal environment.
Please note it is best to run the du commands in the root account, or you may need to add sudo to the command depending on the area or permissions set for the directories, such as system files, as they may not be accessible without root permissions.
Table of Contents
How to Display Summary of Disk Usage
First, let’s show how the du command in Linux displays the sizes of directories and files in a file system. To display a disk usage summary, you can use the -s option, which only shows the total size of a directory rather than the sizes of all the files and subdirectories within it.
For example, to display the total size of the current directory, you can run the following command:
du -s
This will display the total size of the current directory in kilobytes.
You can also use the -h option, which tells du to display sizes in “human-readable” format, with sizes in kilobytes, megabytes, or gigabytes, depending on the directory size. For example:
du -sh
This will display the total size of the current directory in a more readable format.
You can also use du to display the total size of a different directory by specifying the path to that directory as an argument. For example:
du -sh /path/to/directory
This will display the total size of the directory at /path/to/directory in a human-readable format.
How to Display the Disk Usage Total Size
To display the total size of a directory, along with the total sizes of all its files and subdirectories, you can use the du command with the -c and -h flags. The -c flag tells du to display a total, and the -h flag tells it to display sizes in “human-readable” format, with sizes in kilobytes, megabytes, or gigabytes, depending on the size of the directory.
For example, to display the total size of the current directory, along with the total sizes of all its files and subdirectories, you can run the following command:
du -ch
You can also use this command to display the total size of a different directory by specifying the path to that directory as an argument. For example:
du -ch /path/to/directory
How to Display Disk Information in Kilobytes / Megabytes
To display the sizes of files and directories in a file system in kilobytes or megabytes, you can use the du command in combination with the -k or -m options, respectively.
For example, to display the sizes of the files and directories in the current directory in kilobytes, you can run the following command:
du -k
To display the sizes in megabytes, you can use the -m option instead:
du -m
By default, du will display the sizes of all the files and directories in a directory. You can use the -s option to display only the total size of a directory rather than the sizes of all the files and subdirectories within it.
For example, to display the total size of the current directory in kilobytes, you can run the following:
du -sk
To display the total size of a different directory in megabytes, you can run the following:
du -sm /path/to/directory
How to Display Disk Information in Gigabytes
To list the sizes of directories and files in a file system in gigabytes, you can use the du command with the -h option, which tells du to display sizes in “human-readable” format. The sizes will be displayed in gigabytes if the directory size is larger than 1 gigabyte.
For example, to list the sizes of the files and directories in the current directory in gigabytes, you can run the following command:
du -h
To list the sizes of a different directory in gigabytes, you can specify the path to that directory as an argument. For example:
du -h /path/to/directory
You can also use the -d option to specify the maximum depth of the directory tree to display. For example, to list the sizes of only the top-level directories in a file system, you can run the:
du -hd1 /path/to/directory
How to Display Last Modified Date
To display the last modified date with the du command, you can use the –time flag. This flag shows the last modified time for each file, along with the file size.
Here is an example of how to use the du command with the –time flag:
du --time *
You can also use the -h flag to display sizes in “human-readable” format (e.g., in powers of 1024, with a size abbreviation such as “M” for megabytes). For example:
du -h --time *
This will display each file’s size and last modified time in the current directory in a human-readable format. The output will be similar to this:
4.0K 2022-01-07 13:14 file1.txt
8.0K 2022-01-07 13:14 file2.txt
16M 2022-01-07 13:14 file3.txt
You can also use the -l flag to display the sizes of local files only (i.e., not including symbolic links). For example:
du -l --time *
This will display each local file’s size and last modified time in the current directory. Note that symbolic links will be excluded from the output.
How to Exclude Specific File Format
To exclude specific file formats from the output of the du command, you can use the –exclude flag, followed by a pattern that matches the file formats you want to exclude.
For example, to exclude all files with the .tmp extension from the output, you can run the following command:
du --exclude=*.tmp /path/to/directory
This command will display the sizes of all the files and directories in the /path/to/directory, except those with the .tmp extension.
You can use any pattern recognized by the fnmatch function in Python. For example, to exclude all files with the .tmp or .log extension, you can use the following pattern:
du --exclude=*.tmp --exclude=*.log /path/to/directory
How to Display Disk Usage Detail with Timestamp
To display the disk usage of a directory, along with the timestamp of each file and directory, you can use the du command in combination with the ls command and the -l option, which tells ls to display detailed information about each file and directory.
For example, to display the disk usage of the current directory, along with the timestamp of each file and directory, you can run the following command:
du -a | sort -n | ls -l
This command will display a list of all the files and directories in the current directory, sorted by size, along with their timestamps.
You can also use this command to display the disk usage of a different directory by specifying the path to that directory as an argument to du. For example:
du -a /path/to/directory | sort -n | ls -l
Conclusion
This small tutorial taught you the basics of using the Linux du command. The above command examples are just some ways to interact with and further enhance the command. For most users, the above commands should provide adequate information for most cases, especially if you are on a headless server environment.