On any operating system, especially on Linux systems like Ubuntu, it is important to keep track of available storage space. Sometimes, you might find that your hard drive is full and need to identify which files or directories are consuming the most space.
Fortunately, in Ubuntu, we can use the du
(disk usage) command to get detailed information about disk usage and identify the directories that are taking up the most space. This command allows us to explore the directory structure and quickly see the size of each one.
In this tutorial, I will guide you through the steps to use the du
command in the Ubuntu terminal.
Step 1: Open the Terminal
Open the terminal in Ubuntu. You can do this by searching for “Terminal” in the application menu or using the keyboard shortcut Ctrl + Alt + T
.
Or you can connect via SSH to the console.
Step 2: Navigate to the Directory (Optional)
If you are specifically interested in a particular directory, you can navigate to it using the cd
command. For example:
cd /path/to/directory
Step 3: Use the du
Command
Once in the terminal, use the du
(disk usage) command to get information about disk usage. Here are some examples of how to use it:
View the size of all directories in the current directory:
du -h --max-depth=1
This command will show the size of all directories in the current directory.
View the size of all directories in a specific directory:
du -h --max-depth=1 /path/to/directory
Replace /path/to/directory
with the path to the directory you are interested in.
For example, the user’s home directory path:
sudo du -h --max-depth=1 /home/ubuntu
Sort the results and see the directories that take up the most space first:
du -h --max-depth=1 / sort -hr
This command will show the directories in descending order of size, displaying the largest ones first.
Limit the number of results and see only the top n directories:
du -h --max-depth=1 / sort -hr head -n 10
This command will show the 10 largest directories on the system.
View the space occupied by files within a directory:
- In the directory itself:
du -ah --max-depth=1 sort -rh head -n 10
- In a directory path:
du -ah /path/to/directory sort -rh head -n 10
Step 4: Interpret the Results
Once you run one of the above commands, you will see a list of directories along with the size they occupy on your file system. This will help you identify the directories that are consuming the most space on your system.
Step 5: Take Action (Optional)
Depending on what you find, you can decide to take actions such as deleting unnecessary files or moving them to another location to free up disk space.