How to use the command 'du' (with examples)

How to use the command 'du' (with examples)

The ‘du’ command is used to estimate and summarize file and directory space usage. It provides information about the sizes of files and directories, either in bytes or in human-readable form. The command is very useful for finding out the disk space occupied by specific directories or files, and for monitoring disk usage on a system.

Use case 1: List the sizes of a directory and any subdirectories, in the given unit (B/KiB/MiB)

Code:

du -b|k|m path/to/directory

Motivation: This use case is helpful when you need to know the sizes of all the files and directories within a specific directory and its subdirectories, in a specific unit such as bytes (B), kibibytes (KiB), or mebibytes (MiB).

Explanation: The ‘-b’, ‘-k’, and ‘-m’ options specify the unit in which the sizes should be displayed. ‘-b’ is for bytes, ‘-k’ is for kibibytes, and ‘-m’ is for mebibytes. ‘path/to/directory’ is the path to the directory for which you want to calculate the sizes.

Example output:

1000B    path/to/directory/file1.txt
2000B    path/to/directory/file2.txt
3000B    path/to/directory/subdirectory/file3.txt
Total    6000B

Use case 2: List the sizes of a directory and any subdirectories, in human-readable form

Code:

du -h path/to/directory

Motivation: This use case is useful when you want to get a human-readable summary of the sizes of all the files and directories within a specific directory and its subdirectories. The command automatically selects the appropriate unit (B, KiB, MiB, etc.) for each size.

Explanation: The ‘-h’ option stands for human-readable. It formats the sizes of files and directories in a way that is easier for humans to understand. ‘path/to/directory’ is the path to the directory for which you want to calculate the sizes.

Example output:

1.0K    path/to/directory/file1.txt
2.0K    path/to/directory/file2.txt
3.0K    path/to/directory/subdirectory/file3.txt
Total    6.0K

Use case 3: Show the size of a single directory, in human-readable units

Code:

du -sh path/to/directory

Motivation: This use case is handy when you need to find out the size of a specific directory without displaying the sizes of its contents. It gives you a human-readable size of the directory itself.

Explanation: The ‘-s’ option stands for summary. It displays only the total size of the specified directory, excluding the sizes of its contents. The ‘-h’ option formats the size in a human-readable form. ‘path/to/directory’ is the path to the directory for which you want to calculate the size.

Example output:

6.0K    path/to/directory

Use case 4: List the human-readable sizes of a directory and of all the files and directories within it

Code:

du -ah path/to/directory

Motivation: This use case is useful when you want to get a detailed human-readable summary of the sizes of all the files and directories within a specific directory, including hidden files (files starting with a dot).

Explanation: The ‘-a’ option stands for all. It includes entries for all files and directories within the specified directory, including hidden files. The ‘-h’ option formats the sizes in a human-readable form. ‘path/to/directory’ is the path to the directory for which you want to calculate the sizes.

Example output:

1.0K    path/to/directory/file1.txt
2.0K    path/to/directory/.hidden_file.txt
3.0K    path/to/directory/subdirectory/file2.txt
Total    6.0K

Use case 5: List the human-readable sizes of a directory and any subdirectories, up to N levels deep

Code:

du -h --max-depth=N path/to/directory

Motivation: This use case is helpful when you want to limit the depth of the subdirectories included in the summary. It allows you to specify how many levels deep the command should go.

Explanation: The ‘-h’ option formats the sizes in a human-readable form. The ‘–max-depth=N’ option limits the depth of the summary to N levels. Replace ‘N’ with the desired number, for example, ‘–max-depth=2’ for two levels deep. ‘path/to/directory’ is the path to the directory for which you want to calculate the sizes.

Example output:

1.0K    path/to/directory/file1.txt
2.0K    path/to/directory/subdirectory/file2.txt
Total    3.0K

Use case 6: List the human-readable size of all ‘.jpg’ files in subdirectories of the current directory, and show a cumulative total at the end

Code:

du -ch */*.jpg

Motivation: This use case is useful when you want to know the sizes of all ‘.jpg’ files in the subdirectories of the current directory and get a cumulative total of their sizes at the end.

Explanation: The ‘-c’ option stands for cumulative. It shows a grand total of the sizes of all files and directories at the end. ‘/.jpg’ is a pattern that matches all ‘.jpg’ files in subdirectories of the current directory. The ‘-h’ option formats the sizes in a human-readable form.

Example output:

1.0K    subdirectory1/image1.jpg
2.0K    subdirectory2/image2.jpg
Total    3.0K

Conclusion:

The ‘du’ command is a powerful tool for estimating and summarizing file and directory space usage. It provides different options to display sizes in various units and formats. Whether you need a detailed summary or just the size of a single directory, ‘du’ has got you covered. By mastering the use cases described in this article, you’ll be able to efficiently manage disk space on your system.

Related Posts

How to use the command modprobe (with examples)

How to use the command modprobe (with examples)

The ‘modprobe’ command is used to add or remove modules from the Linux kernel.

Read More
How to use the command qm stop (with examples)

How to use the command qm stop (with examples)

The qm stop command is used to stop a virtual machine in Proxmox Virtual Environment.

Read More
How to use the command testssl (with examples)

How to use the command testssl (with examples)

The command testssl is used to check the SSL/TLS protocols and ciphers supported by a server.

Read More