Early warning when running out of disk space

Is there a way to get an early warning when running out of disk space? Timeshift had taken all my disk space which resulted in the PC not booting anymore. I managed to delete some files by booting from USB and now the computer runs fine again. But I would like to prepare myself and get an early warning next time when the remaining disk space drops below a certain level.

1 Like

There isn't an easy way to do this, as far as I can tell... Never really thought about this either, to be honest. From what I could find there's a built-in method that works with the Gnome desktop environment, but it doesn't seem very reliably (the highest threshold to trigger the warning is 1GB, which may be already causing the issue you experienced).

If you are willing for a small terminal side quest, you could write a simple shell script an set it to run every day, or whatever interval you like, to send a notification.

#!/bin/bash

# Sends a notification when the disk space is running low

# Minimum amount of space used to trigger the notification, expressed
# as a percentage.
threshold=80

current=$(\df -h | awk '/\/$/ { gsub(/%/, ""); print $(( NF - 1 )) }')

if [[ $current -ge $threshold ]];
then
    notify-send "You've used ${remaining}% of your disk space."
fi

You can modify the $threshold variable to whatever percentage you want. To make this file executable, run: chmod u+x ./disk-monitor.sh (or whatever you named it). Copy and paste this into an empty text file, and give it a meaningful name such as "disk-monitor.sh".

As you can see, this is very simple and far from perfect, but if it works it should be enough to make you aware of the issue. So, before going any further, try to run this script from the command line to see if it works. Just enter ./disk-monitor.sh and see if there's a notification popping up.


You can add an entry into the /etc/fstab file to run the script every two hours, for example, but you can also run it on startup or whenever you like.

To edit this file, run this in the terminal crontab -e. If this is the first time you run this command (I'll assume it is) you will be prompted to choose a terminal-based text editor:

no crontab for zenzen - using an empty one

Select an editor. To change later, run 'select-editor'.

  1. /bin/nano <---- easiest
  2. /usr/bin/vim.tiny
  3. /bin/ed

Choose 1-3 [1]:

Enter 1 and press Enter. At the end of the file you can then enter the following:

0 */2 * * * /home/zenzen/.local/bin/disk-monitor.sh

To save the changes, press Ctrl+O and exit with Ctrl+X.

What this will do is run the provided script every couple of hours, on the hour i.e.: minute 0.

Make sure to update the path to the script, which needs to be provided as a full path, starting at root. You can place it anywhere you want, that doesn't really matter, but I'm using /home/<your_username>/.local/bin because it's a standard convention.

1 Like

Also, a piece of advice, never store Timeshift backups, on your main drive. It is better to get an external hard drive, keep it plugged in, and tell Timeshift to save its backups on the external drive. You never want to put yourself in a situation, where you fill up your main boot drive all the way to the max, thereby preventing your computer from booting up.


2 Likes