If you've ever run
du on a large Linux directory, you get a huge amount of output, and it can be difficult to quickly see which directories are actually using your disk space.
For a quick overview, I use a small Bash function which I called
storagesize. It lets you specify how deep into the directory structure you want to look.
The Bash function
Add this to your
~/.bashrc:
# Custom function to get folder size with depth parameter "storagesize 2"
storagesize() {
local depth="${1:-1}"
sudo du -h --max-depth="$depth" . 2>/dev/null | sort -h
}
Once you've added it, reload your Bash configuration:
source ~/.bashrc
You can now run:
storagesize
Or specify how many directory levels you want to see:
storagesize 2
What does storagesize actually do?
There's a useful amount of functionality packed into this small function.
The depth parameter
The first line inside the function is:
local depth="${1:-1}"
This takes the first argument supplied to the function and stores it in the
depth variable.
So when you run:
storagesize 2
depth becomes
2.
If you don't provide an argument:
storagesize
the
:-1 means that the function defaults to a depth of
1.
That gives us a convenient combination, a simple command for a quick overview, while still allowing us to dig deeper when necessary.
The du command
The actual disk usage calculation is performed by:
sudo du -h --max-depth="$depth" .
du stands for
disk usage. It calculates how much storage files and directories are consuming.
Example output might look like this:
0 ./dev
0 ./proc
0 ./sys
4.0K ./bin.usr-is-merged
4.0K ./cdrom
4.0K ./lib.usr-is-merged
4.0K ./sbin.usr-is-merged
4.0K ./srv
8.0K ./media
8.0K ./scripts
12K ./src
16K ./lost+found
228K ./tmp
7.3M ./opt
9.1M ./run
14M ./etc
95M ./home
233M ./boot
2.2G ./root
6.9G ./snap
7.4G ./usr
9.3G ./var
3.6T .
3.6T ./mnt
The
-h option means
human-readable.
Without it, you might get values such as:
4096
1048576
52428800
With
-h, you get something much easier to understand:
4.0K
1.0M
50M
What does --max-depth do?
This is the part that makes the function particularly useful.
Running:
storagesize
(which defaults to a depth of 1)
gives you a high-level overview.
You might get something like:
.
4.0K ./crash
4.0K ./local
4.0K ./mail
4.0K ./metrics
4.0K ./opt
12K ./logs
48K ./spool
92K ./tmp
5.5M ./backups
11M ./snap
44M ./ossec
338M ./cache
3.5G ./log
5.5G ./lib
9.3G .
Now you immediately know that
lib is consuming most of the space.
You can then move into that directory, or simply run
storagesize 2
:
Now you can see another level of detail (Cropped out a bunch of the smaller directories to focus on the larger ones):
11M ./snap
13M ./ossec/var
20M ./ossec/lib
23M ./cache/fwupd
23M ./lib/swcatalog
31M ./cache/apparmor
44M ./ossec
47M ./log/sysstat
83M ./lib/dpkg
208M ./lib/apt
256M ./cache/apt
338M ./cache
1.3G ./log/nginx
2.1G ./log/journal
3.5G ./log
5.2G ./lib/snapd
5.5G ./lib
9.3G .
You can continue doing this until you find exactly what is consuming your disk space.
Why use sort -h?
The final part of the command is:
| sort -h
The pipe sends the output from
du into
sort.
The
-h option, again, tells
sort that the values are human-readable sizes.
That means it understands that
500M
comes before
2.0G
rather than simply sorting the values alphabetically.
The result is a list ordered from smallest to largest, making the biggest directories easy to spot.
Why use sudo?
You may wonder why the function uses
sudo du
instead of simply
du
.
Linux permissions can prevent your normal user account from reading some directories.
That can result in permission errors and, more importantly, incomplete information.
Using
sudo allows
du to inspect directories that your normal user cannot access.
The function also contains:
2>/dev/null
This redirects error output to
/dev/null, keeping the terminal from being filled with eg. permission-denied messages.
Useful examples
Check the current directory:
storagesize
Look two levels deep:
storagesize 2
Look three levels deep:
storagesize 3
Check a specific location by changing into it first:
cd /var
storagesize 2
Why not just type the full du command?
You absolutely can.
The full command is:
sudo du -h --max-depth=2 . 2>/dev/null | sort -h
There's nothing wrong with that.
I simply found myself typing it over and over again, and it was easier to remember a small function name.
Turning it into
storagesize 2
makes it something you can actually remember and use regularly.
It's especially useful when troubleshooting a server that is suddenly running low on disk space (which mine was).
A small function that makes disk troubleshooting easier
du is already an extremely useful Linux command, but wrapping it in a small Bash function makes it much more convenient for everyday administration.
With
storagesize, the workflow becomes simple:
- Run
storagesize for a quick overview.
- Find the directory consuming the most space.
cd into that directory.
- Run
storagesize 2 or storagesize 3.
- Repeat until you've found what's actually consuming the space.
It's a tiny addition to
~/.bashrc, but one that can save a surprising amount of time when you're trying to figure out where all your disk space went.
Comments (0)
No comments yet. Be the first to comment!
Leave a Comment