🐧 Linux β€” Check Disk Space Usage

Ido Montekyo
idomongo
Published in
3 min readJun 4, 2022

--

Photo by Patrick Lindenberg on Unsplash

I got an IO error in my application. Trying to figure out what had happened, I’ve noticed Linux shell commands are generating a β€œNo Space Left on Device” error message. This means once again, my Linux server ran out of disk space.

Finding out what is consuming disk space is not an easy task and at this point, we cannot install any 3rd party utilities because there is no space left on the device.

Luckily, we can use the following methods.

Method #1 β€” Using du Command

We need to identify the specific directories consuming a large amount of disk space. For this, a directory traversing is required.

du -ch --max-depth=2 .

Output example:

.0K    ./bin
0 ./public/images
0 ./public/javascripts
4.0K ./public/stylesheets
4.0K ./public
12K ./views
8.0K ./routes
8.0K ./services
60K .
60K total

β€” max-depth=2 is the traversing subdirectory depth.

Method #2 β€” Using the Tree command

I am using Oracle Enterprise Linux which has the Tree command preinstalled.

tree --du -h

Output example:

.
β”œβ”€β”€ [ 1.8K] app.js
β”œβ”€β”€ [ 1.6K] bin
β”‚ └── [ 1.5K] www
β”œβ”€β”€ [ 392] package.json
β”œβ”€β”€ [ 204] public
β”‚ β”œβ”€β”€ [ 6] images
β”‚ β”œβ”€β”€ [ 6] javascripts
β”‚ └── [ 134] stylesheets
β”‚ └── [ 111] style.css
β”œβ”€β”€ [ 2.0K] routes
β”‚ β”œβ”€β”€ [ 1.8K] index.js
β”‚ └── [ 203] users.js
β”œβ”€β”€ [ 4.9K] services
β”‚ β”œβ”€β”€ [ 2.5K] MqttAqBridge.js
β”‚ └── [ 2.4K] QueueService.js
└── [ 336] views
β”œβ”€β”€ [ 84] error.jade
β”œβ”€β”€ [ 66] index.jade
└── [ 125] layout.jade
11K used in 8 directories, 11 files

Method #3 β€” Using my own script

Writing our own script would be slower and less efficient as we need to recursively traverse the directory tree, however, we can design the printed output as we see fit.

#!/bin/bashdir_recurse() {
cd $1

# Calculate depth
depth=`pwd | tr -d -c '/' | wc -m`

# Add spaces for tree print
empty_space=""
for i in `seq 1 $depth` ; do
empty_space=$empty_space' '
done

# Calculate disk space used
x=`du -skh`
echo "${empty_space} ${1} ${x}"

# Traverse directories
for i in *;do
if [ -d "$i" ];then
# recurse
dir_recurse "$i"
fi
done
cd ..
}
dir_recurse .

Usage example:

./mytree.sh

Output example:

. 60K       .
bin 4.0K .
public 4.0K .
images 0 .
javascripts 0 .
stylesheets 4.0K .
routes 8.0K .
services 8.0K .
views 12K .

Filtering the output

So, three different methods generated a similar output showing the unit of measure e.g. K for Kilobytes, M for Megabytes, or G for Gigabytes. As a typical server holds millions of files, the output will most likely contain lots of small directories we can filter out by using the Linux redirection operator:

| grep G

For example:

tree --du -h | grep G]

This will show the directories with Gigabytes of files (or subdirectories).

There are more methods to check disk space usage. If you know some, feel free to drop a comment and share your thoughts.

🀝🏼

--

--

Ido Montekyo
idomongo

System Analysis. System Design. Architecture. Databases. Project Management. Speaker. People Motivator.