How to Check Total Directory Size in Ubuntu Linux
This guide explains how to determine the storage space used by a specific folder and its contents within the Ubuntu operating system. You will learn the primary command-line tool used for this task, along with the necessary flags to display the results in a human-readable format. By following these steps, you can quickly manage disk space and identify large directories on your system.
The Du Command
The command used to calculate the total size of a directory including
subdirectories is du, which stands for disk usage. To see
the total size of a specific folder, open your terminal and type the
following command:
du -sh /path/to/directoryUnderstanding the Flags
The flags used in this command modify the output to make it useful for quick checks:
- -s: This stands for summarize. It prevents the command from listing every single subdirectory and instead provides only the total size for the specified directory.
- -h: This stands for human-readable. It converts the size from bytes into kilobytes, megabytes, or gigabytes, making the number easier to interpret.
Practical Examples
To check the size of your home directory, you can run:
du -sh ~If you are currently inside the folder you want to check, you can use a dot to represent the current directory:
du -sh .In some cases, you may encounter permission errors if the directory
contains files owned by the root user. If this happens, prepend
sudo to the command to grant the necessary privileges:
sudo du -sh /path/to/directoryThis method provides an immediate and accurate measurement of disk usage for any folder on your Ubuntu machine.