Disk full again? It's a common frustration for any Linux user. You're trying to download a new program, save important documents, or just generally work on your system, and suddenly you're greeted with those dreaded "No space left on device" errors. Wondering where all your precious disk space disappeared to? The answer usually lies in large, often forgotten files and folders lurking within your system.
Table of Contents
-
Quickly Find Disk Space Hogs: Using the du Command to Locate Large Files in Linux
- The Challenge: du Output is... a Lot!
- Sorting du Output by Size: Introducing the sort -h Combination
- Reversing the Sort Order: sort -rh to put the biggest first!
- Limiting the Output: Focusing on the Top Results with head
- Combining it All: The Ultimate "Find Large Files" Command
- Going Deeper: Targeting Specific Directories for Analysis
- Conclusion: You Can Now Pinpoint Disk Space Hogs!
Fortunately, Linux provides powerful tools to pinpoint these disk space culprits, and the du
command is at the heart of it. In this guide, we'll take your du
command skills to the next level. We'll show you how to combine du
with other command line tools to quickly and efficiently locate the largest files and folders on your Linux system, so you can reclaim your storage and get your system running smoothly again!
Quickly Find Disk Space Hogs: Using the du Command to Locate Large Files in Linux
In our previous article, "Linux du Command: Check Disk Space - A Beginner's Guide", we covered the basics of using du
to understand disk usage. You learned how to check the size of directories, use human-readable output, and summarize folder sizes. But when you're facing a full disk, just knowing the size of every directory isn't always helpful. You need to find the biggest ones, the real space hogs, and fast!
The Challenge: du
Output is... a Lot!
If you've experimented with the basic du
command, especially on larger directories, you've probably noticed something: it produces a lot of output. Running du -h
in your home directory, for example, can generate hundreds or even thousands of lines, listing the size of every subdirectory and file.
While all this information is technically useful, when you're trying to quickly identify the largest space users, sifting through this massive list is like searching for a needle in a haystack. We need a way to filter and sort this output to bring the most important information – the biggest files and folders – to the top.
Sorting du
Output by Size: Introducing the sort -h
Combination
This is where the power of the Linux command line really shines. We can combine the du
command with other commands to manipulate its output. Our first step is to sort the output of du
by size. For this, we'll use the sort
command.
The sort
command, as its name suggests, is used to sort lines of text. We can "pipe" the output of du
directly to sort
using the pipe symbol |
. The pipe takes the output of the command on the left and feeds it as input to the command on the right.
To sort the human-readable output of du -h
correctly by size, we need to use the -h
option with sort
as well. This tells sort
to understand the "human-readable" size units (K, M, G) correctly when sorting numerically.
Try this command in your terminal (perhaps in your home directory to start):
Example Code Block:
du -h | sort -h
and press Enter.
You'll see the output of du -h
again, but this time, it's sorted by size!
4.0K ./Documents/notes.txt
8.0K ./Documents/todo.txt
12K ./Downloads/script.sh
16K ./Desktop/config.json
24K ./Documents/report.pdf
100K ./Pictures/logo.png
1.2M ./Videos/demo.mp4
2.5M ./Downloads/archive.zip
8.0M ./Music/song.mp3
16M ./Videos/tutorial.mp4
50M ./Downloads/software.tar.gz
125M ./ISO/ubuntu.iso
Notice how the output is now ordered. The folders using the least disk space are at the beginning, and as you scroll down, you'll see folders using progressively more space. This is already a big improvement for finding larger items!
Reversing the Sort Order: sort -rh
to put the biggest first!
While having the output sorted by size is helpful, we usually want to find the largest files and folders first, so they should be at the top of the list, not the bottom. We can easily reverse the sort order using the -r
option with the sort
command.
Combine -r
(reverse) with -h
(human-numeric sort) to get sort -rh
. Let's run this with du -h
:
Example Code Block:
du -h | sort -rh
Press Enter, and behold!
125M ./ISO/ubuntu.iso
50M ./Downloads/software.tar.gz
16M ./Videos/tutorial.mp4
8.0M ./Music/song.mp3
2.5M ./Downloads/archive.zip
1.2M ./Videos/demo.mp4
100K ./Pictures/logo.png
24K ./Documents/report.pdf
16K ./Desktop/config.json
12K ./Downloads/script.sh
8.0K ./Documents/todo.txt
4.0K ./Documents/notes.txt
Now, the folders and directories using the most disk space are listed right at the top! This is exactly what we need to quickly identify the biggest space users on our system. You can immediately see which folders are consuming the most storage.
Key Intermediate Tip: du -h | sort -rh
is your go-to command combination for quickly finding large folders! It's a powerful tool for disk space management.
Limiting the Output: Focusing on the Top Results with head
Even with sorting, the output can still be quite long, especially in directories with many subfolders. Often, we only need to see the top few largest folders to get a good idea of where our disk space is going. For this, we can use the head
command.
The head
command, by default, displays the first 10 lines of its input. Let's pipe the output of our sorted du
command to head
:
Example Code Block:
du -h | sort -rh | head
Press Enter, and the output will be much shorter! You'll only see the top 10 largest directories from the du
output, sorted by size in descending order.
125M ./ISO/ubuntu.iso
50M ./Downloads/software.tar.gz
16M ./Videos/tutorial.mp4
8.0M ./Music/song.mp3
2.5M ./Downloads/archive.zip
1.2M ./Videos/demo.mp4
100K ./Pictures/logo.png
24K ./Documents/report.pdf
16K ./Desktop/config.json
12K ./Downloads/script.sh
If you want to see a different number of top results, you can use the -n
option with head
followed by the number of lines you want. For example, to see the top 5 largest folders:
Example Code Block:
du -h | sort -rh | head -n 5
This will show you the top 5 largest directories, giving you a very concise and focused view of your largest space users.
Combining it All: The Ultimate "Find Large Files" Command
Now, let's put all these pieces together to create a truly powerful command for finding large files and folders. We'll combine du
, sort
, head
, and add one more useful option to du
: -a
.
The -a
option tells du
to include files as well as directories in its output. By default, du
only reports on directories. If you want to find large individual files, you need to add -a
.
Here's the ultimate command:
Example Code Block:
du -ha | sort -rh | head -n 20
Let's break down this powerhouse command:
du -ha
: Calculates disk usage for all files and directories in the current location, and outputs in human-readable format.| sort -rh
: Pipes the output tosort
, sorts it in reverse order based on human-readable numerical values (sizes).| head -n 20
: Pipes the sorted output tohead
, and displays only the top 20 lines (largest items).
Practical Example Scenario: Run this command in your home directory. You'll get a list of the 20 largest files and folders in your personal files, sorted from largest to smallest. This is incredibly useful for quickly identifying what's taking up space and deciding what you might want to clean up or move!
Key Intermediate Tip: du -ha | sort -rh | head -n <number>
is a fantastic command for quickly identifying the biggest space users, including both files and folders. Adjust <number>
to see more or fewer top results.
Going Deeper: Targeting Specific Directories for Analysis
Just like with the basic du
command, you can also specify a path to a directory with our combined command to analyze disk usage in a specific location.
For example, to investigate the /var/log
directory, which often contains system log files that can grow quite large, you can use:
Example Code Block:
du -ha /var/log | sort -rh | head -n 10
This command will show you the top 10 largest files and folders within the /var/log
directory, helping you quickly assess if log files are consuming excessive disk space. You can replace /var/log
with any directory path you want to analyze.
Conclusion: You Can Now Pinpoint Disk Space Hogs!
Congratulations! You've now mastered a powerful technique for quickly finding disk space hogs on your Linux system using the du
command in combination with sort
and head
. You can:
- Sort
du
output by size to easily see larger items. - Reverse the sort order to put the largest items at the top.
- Limit the output to focus on the top few largest files and folders.
- Find both large files and folders using the
-a
option. - Target your analysis to specific directories.
With these skills, you can efficiently manage your disk space, identify and clean up large, unnecessary files, and keep your Linux system running smoothly.
Ready to put your new skills to the test? Run the command du -ha | sort -rh | head -n 20
in your home directory right now. What are the largest files and folders on your system? Are you surprised by what you find? Share your findings or any disk space cleanup stories in the comments below! Happy hunting for those disk space hogs!