How to Find What’s Taking Up Disk Space on a Linux Server

If you’ve come to this article, it probably means you’re getting “no space left on device” errors. I’ll help you fix that by finding which files are taking up the most space.

First, make sure you are working as the root user.

sudo -i

Now check which disk is full.

df -h

You should see a list of all your filesystems, some stats about their usage, and where they are mounted. Navigate to where the full disk is mounted. I’m going to assume the disk mounted on “/” is full.

cd /

Now we can drill down and find the directories or files that are eating up all your space. Here’s the command we’re going to use, and an explanation of each part.

du -haxd 1 . | sort -h
  • du
    du is a utility that will show disk usage of files and directories.
  • -h
    Print human readable sizes, like MB and GB. Without it, du will print sizes in bytes.
  • -a
    All files should be shown, not just directories.
  • -x
    Skip directories that are on other filesystems, which is what we want, since this is the full filesystem.
  • -d 1
    Show a depth of 1, meaning for each given file or directory directly immediately under the current directory (“.”), print a summary.
  • sort
    sort is a utility to sort output lines.
  • -h
    Just like du, sort human readable sizes, like MB and GB.

With that command, you should now have a list of files and directories on your disk and how much disk space each one and its contents are using. They are sorted from smallest to largest disk usage, for convenience. You can now begin to dig further by going into large directories and running the same command, looking for files eating up a lot of space.

A note, if you’re not using the -x argument, you can append 2>/dev/null to the command. This will redirect errors to /dev/null (hide them). We don’t need this part when using -x, but when du is searching through directories across filesystems, it will often print errors with virtual filesystems like “/proc”.

I hope this helps you clean up your server. Once you’ve figured out what is taking up so much space, hopefully there’s an easy command to clean those files up. Some things that might be the issue are mysql binlogs, docker image and container cache, snap cache, flatpak cache, or big log files. There are easy ways to clean those up, once you diagnose the issue.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.