I’m trying to check the disk usage of my files and directories on my Linux system, but I’m not quite sure how to use the ‘du’ command. Could someone explain the basics or provide some useful examples?
The ‘du’ command in Linux is pretty handy for checking disk usage of files and directories. It stands for “disk usage” and comes with several options that make it adaptable for various tasks. Here’s a comprehensive guide with some examples to get you started:
First off, the most basic usage of ‘du’ is:
du
This will list the disk usage of the directory tree starting from your current directory. The output shows the space used by each directory and sub-directory in blocks.
If you’re interested in human-readable formats, i.e., KB, MB, GB, you can use the -h
option:
du -h
This is much easier to read, especially if you’re scanning through quickly.
For example:
$ du -h
1.0K ./dir1
1.0K ./dir2
4.0K ./dir3/file
6.0K .
But what if you only want to see the total usage of each directory, without the details of each sub-directory? That’s where the -s
(summary) option comes in:
du -sh *
In this command, the * is a wildcard that sums up the usage for each item in the current directory:
$ du -sh *
4.0K dir1
2.0K file1
12K dir2
Sometimes, you might want to check the disk usage of a specific directory or file. Just specify the path after the command:
du -h /path/to/directory
If you’re dealing with a massive number of files and need to sort them by usage, pipe the output to sort
:
du -sh * | sort -h
This command will give you a sorted list with the smallest files/directories at the top and the largest at the bottom:
2.0K file1
4.0K dir1
12K dir2
Want real-time updates on disk usage? Combine watch
with du
:
watch -n 30 'du -sh /path/to/directory'
This updates every 30 seconds.
Sometimes you might only want to see details for a certain depth. The --max-depth
option lets you set this:
du -h --max-depth=1
This will only show the sizes of directories and files at the first depth level:
4.0K ./file1
12K ./dir1
16K .
Additionally, if there are symbolic links and you want to include those as well, -L
(dereference all symbolic links) can be used:
du -L -h /path/to/directory
In a multi-user environment, disk usage can accumulate in ways that are not always evident. If you are root or have sudo privileges, running du
with sudo
can help ensure you see all data for a directory:
sudo du -sh /path/
Here’s another practical example, combining multiple options:
sudo du -h --max-depth=1 /var | sort -h
This command sorts the disk usage of the /var
directory (often large due to logs) at depth=1 in a human-readable format.
If you find specific file types filling up your disk, adding --exclude
can help filter them out. For instance, to exclude all .log
files:
du -h --exclude='*.log' /path/to/directory
For extensive analysis, you might want to view options that include apparent size (considering sparse files differently) using --apparent-size
:
du -h --apparent-size
And finally, integrating with other commands, like finding files in specific time ranges, combining find
can work wonders:
find /path -mtime -7 -exec du -sh {} +
This finds and displays the disk usage of files modified in the last 7 days.
Exploring more? Look at man du
for comprehensive usage and options. Happy shelsscripting!
@byteguru has laid out a solid foundation for using the du
command, but let’s dig into some alternative scenarios and caveats that might come in handy.
First off, if you’re dealing with large filesystems or network filesystems, running du
can be a bit slow. In cases like these, the ncdu
(NCurses Disk Usage) tool can be a game-changer. It provides a curses-based interface to explore the file system usage interactively. You can install it using your package manager (sudo apt install ncdu
on Debian-based systems). Unlike du
, ncdu
refreshes the disk usage information quite fast and lets you navigate directories easily.
ncdu /path/to/directory
Another thing to consider is using du -a
to list individual files along with directories. This can be helpful when you need detailed usage info:
du -ah /path/to/directory
Additionally, rather than using watch
for real-time updates—which will just spam your terminal with du
data—consider alternative methods like iotop
or atop
that give you combined IO and system statistics.
For those who want more fine-grained control, Glances (glances
package) is another powerful tool that aggregates disk usage in real-time while monitoring system performance.
Regarding symbolic links, -L
indeed dereferences them, but be cautious: this can lead to potential loops or unexpected output if your links form a cyclic dependency. Always consider the -l
option (lowercase L) to limit the depth of recursion into links.
And, though --exclude
is useful for ignoring specific patterns, don’t overlook the possibility of using find
with !
to exclude certain files. Here’s an example:
find /path/to/directory -name '*.log' -prune -o -print0 | xargs -0 du -sh
This will effectively skip .log
files instead.
On a more practical note, dealing with permissions can be troublesome when running du
. Chiming in with sudo
is useful, but for large user directories (like /home
), you may want to avoid using sudo du
due to potential security concerns. Instead, target specific directories you have permission to scan:
du -sh /home/*/Documents
Lastly, if you want to automate these checks, consider cron jobs for periodic checking and logging. Here’s a tiny example that appends the result to a log file daily (very basic, but illustrative):
0 0 * * * du -sh /path/to/directory >> /var/log/disk_usage.log
Experiment and see what fits best for your specific use case!
Honestly, I think you guys are overcomplicating it. Most users don’t need half of these fancy options. Let’s keep it simple:
If you just want the size of a directory, du -sh [directory]
is more than sufficient. All those extra options like --apparent-size
or -L
for symbolic links can actually confuse the average user and are unnecessary in most cases. And using watch
? Seriously? Just run the command when you need it. Unless you’re running real-time systems that need constant monitoring, I can’t see the point.
Also, suggest to use ncdu
if performance is a major concern - yes, it gives a neat interface. But for 99% of tasks, du -h
or du -sh
with the specific directory or file path does the job without add-ons, multiple pipes, or fancy filters. Simplify, don’t confuse. And be careful with sudo du
; it’s a potential security risk. Only use it if absolutely necessary.
Sure, if you’re running some complex setups, maybe dive into those advanced features, but for most, keep it straightforward and you’ll save time and headache.