Linux Disk Space File System Management Commands

Introduction

Here are some commands to assist with mastering disks and file space management on Linux.

TL;DR ncdu is my preferred option.

Disk Usage by Directory

We often have to see how much disk space is being used by specific directory or all the files in a specific directory. At other times and under high pressure one might have to find a particularly large file that is filling up the file system causing the operating system to crash.

Most Linux distributions already have du installed, but there is a  better alternative called ncdu. Both utilities take a while to calculate disk space if you have a large amount of files on the file system, as it has to traverse all the files and directories. Both utilities are really useful to find a particular large file in the system if you don’t know where it might be.

To use ncdu, just do this:

ncdu

At good place to start in often /var/log

To exclude a certain directory, do this:

ncdu --exclude /var/lib/kopano/attachments
ncdu --exclude /var/lib/kopano/attachments --exclude /.snapshots --exclude /proc

Warning aboutncdu on busy systems and virtual file systems (e.g. cPanel)

On a really busy system ncdu might take long (e.g. 10 or more minutes) to complete. This is not a good thing. It’s always best to start smaller and work up to root from there. Also, be careful of not running ncdu in perpetuity. Use top to see if it’s not stuck.

This problem of ncdu taking excruciating long is prevalent on cPanel file servers which use a virtual file system. You’ve been warned!

ncdu versus du

ncdu isn’t installed by default on Debian based distributions but it has a smarter interface than du. Claims are also it’s faster. The other bonus of ncdu is by default it sorts by size. In crisis mode  you would want to use this utility.

To install ncdu on a Debian based distribution:

apt install ncdu

To install ncdu on a openSUSE Tumbleweed based distribution:

zypper install ncdu

du

The du command is quite powerful but as with ncdu be aware of performance implications if you’re running it against large subdirectories. The first time you run du it will take longer than the second time, because on the second run it should be taking advantage of caching of stat() metadata.

If you have a vague clue in which parent directory the large file in question is, first change to that specific directory, then do the following:

du -h --max-depth=1
du --summarize

du see usage sorted by size

If you don’t have access to ncdu you can use the du command pipe syntax below to sort disk usage by size in the current directory:

sudo du -ks $(ls -d */) | sort -nr | cut -f2 | xargs -d '\n' du -sh 2> /dev/null

Note: On a 800 GB server with an SSD array which 100 000s of files it took around 10 minutes before output was produced.

See Disk Space Available

duf versus df -h

duf is a much more user friendly version of df -h which is included with most Linux distributions. As of Ubuntu 22.04 duf is included with the operating system, but otherwise you can install it on a Debian based distribution like so:

wget https://github.com/muesli/duf/releases/download/v0.8.1/duf_0.8.1_linux_amd64.deb
dpkg -i duf_0.8.1_linux_amd64.deb

Next, to see disk usage, do this:

duf -hide special

Determine the Linux file system type

df -Th | grep "^/dev"

Find out which Linux partitions exist on a file system

fdisk -l

From the manual:

List  the  partition  tables  for  the specified devices and then exit.  If no devices are given, those mentioned in /proc/partitions (if that exists) are used.

References

Share this article

Leave a Reply

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

Scroll to Top