The Linux Command du
allows you to easily check the size of files or directories. du
, short for “disk usage,” is a highly useful tool for monitoring disk space. In this post, we will walk you through the basics of the du
command and explore various options you can use to make the most of this command.
Table of Contents
What is the Linux Command du?
The du
command in Linux is used to check the disk space occupied by files or directories. By default, it recursively calculates the size of directories and also allows you to check the size of individual files.
du [options] [file or directory]
ShellScriptThis is the general syntax for using the command. Here, [options]
allows you to specify the output format or calculation method, and [file or directory]
is the target whose size you want to check. By default, the du
command outputs the size of all files and subdirectories in the current directory.
Basic Usage of the du Command
The simplest way to use the du
command is by just entering the command alone.
du
ShellScriptWhen you enter this command, it will output the size of all files and directories in the current directory in bytes.
Main Options of the du Command
The du
command offers several useful options. Here are some commonly used ones:
-h Option: Human-Readable Format
The output generated by default may not be very intuitive. In such cases, you can use the -h
option to display the size in a more human-readable format.
du -h
ShellScriptThe -h option stands for “human-readable” and converts the size into KB (Kilobytes), MB (Megabytes), GB (Gigabytes), etc. For example, 1024 bytes will be displayed as 1KB, and 1048576 bytes as 1MB.
-s Option: Display Summary Only
If you want to check the total size of a specific directory rather than the size of individual files or subdirectories, you can use the -s
option.
du -sh
ShellScriptThis command displays the total size of the current directory in a human-readable format. Without the -s
option, the command would display the size of all subdirectories individually, so this option is very useful when you want to know the total size of a specific directory.
-a Option: Display All Files and Directories
By default, the du
command only outputs the size of directories. However, if you want to know the size of individual files within a directory, you can use the -a
option.
du -ah
ShellScriptThe -a option outputs the size of all files as well as directories, as shown in the figure below.
–max-depth Option: Limit Output Depth
When working with deep directory structures, you may not want to see the sizes of all subdirectories. In such cases, you can use the --max-depth
option to limit the depth of the output.
du -h --max-depth=1
ShellScriptThis command outputs the size of the current directory and only its immediate subdirectories. Since the --max-depth
option is set to 1, it only calculates the first level of subdirectories. Subdirectories at deeper levels are not included in the output.
In the following figure, the --max-depth
value is set to 2. As you can see, the sizes are displayed up to the second level of subdirectories.
-c Option: Display Total Sum
When checking the size of multiple directories using the du
command, you may also want to know their total combined size. In such cases, you can use the -c
option.
du -ch
ShellScriptThis command outputs the size of each directory and file, and then adds a total sum at the end of the output.
Precautions When Using the du Command
The du
command is very useful for checking disk usage, but there are some things you should be aware of.
Displaying Size of Symbolic Links: -L Option
By default, the du
command does not follow symbolic links (special files that reference the location of another file or directory). Therefore, the size of a symbolic link is not calculated as the size of the actual file. To prevent this, you can use the -L
option to follow symbolic links.
Permission Issues: sudo Command
The du
command only calculates the size of files and directories for which the user has read permissions. Therefore, directories or files without permissions will not be included in the calculation, which may lead to unexpected results. To check the disk usage of the entire system, it’s recommended to use the sudo
command alongside du
.
sudo du -sh /var
ShellScriptThis command outputs the total size of the /var
directory, including directories for which you don’t have read permissions.
Useful Usage: –time Option
The du
command can be used for more than just checking disk usage. For instance, when disk space is running low on a server, the du
command can help you locate and clean up large files or directories. It can also be useful for monitoring changes in the size of specific directories.
du -h --time
ShellScriptThis command displays the size of each file and directory along with the last modification time. This can help you identify files or directories that have rapidly increased in size recently.
Summary
In this post, we covered how to use the du
command in Linux and explored various options. The du
command is an extremely useful tool for checking and managing disk usage. Whether you’re dealing with basic usage or advanced options, mastering this command can greatly aid in server management and system monitoring. Disk space management is critical for system stability and performance, so make sure to leverage the du
command effectively.