Linux Disk Space Low? Fix It with These Commands

Linux Disk Full? Troubleshoot with Command Line Tools

Do you want to understand how to fix Linux disk space issues? Not sure what you can safely delete? Fear not, we are here to help you safely free up disk space on your server.

We will focus on Linux Disk Space Commands and use universal commands that work on any Linux Distribution.

Let’s start with the basics.

If your looking for detailed disk space procedures, check out our other popular Post about it.

Show Linux Disk Space Usage on mounted filesystems

The most basic command all sysadmins need to know is:

Bash
df

It actually stands for “disk-free”. Its purpose is to display the amount of disk space available on the file system containing each file name argument.

If no file name is given, the space available on all currently mounted file systems is shown. Disk space is shown in 1K blocks by default unless the environment variable POSIXLY_CORRECT is set, in which case 512-byte blocks are used.

Show Disk Space – Human Readable

If you have tested the above command, you will see that the disk size is displayed in bytes. This can be hard to decipher, especially the bigger the hard disk is. One simple way to get around this is to use the -h syntax.

Bash
df -h

Here is an example of the human-readable output; you can see it displayed in GB, MB, and %.

Bash
Filesystem                                        Size  Used Avail Use% Mounted on
tmpfs                                             3.2G  3.2M  3.1G   1% /run
/dev/mapper/keystore-rpool                        437M   28K  404M   1% /run/keystore/rpool
rpool/ROOT/ubuntu_auw0m6                          400G   11G  389G   3% /
tmpfs                                              16G  373M   16G   3% /dev/shm
tmpfs                                             5.0M  4.0K  5.0M   1% /run/lock
efivarfs                                          438K  201K  233K  47% /sys/firmware/efi/efivars
rpool/USERDATA/rbailey_bbki65                     436G   47G  389G  11% /home/rbailey
rpool/USERDATA/root_bbki65                        389G   21M  389G   1% /root
rpool/ROOT/ubuntu_auw0m6/srv                      389G  256K  389G   1% /srv
rpool/ROOT/ubuntu_auw0m6/var/games                389G  256K  389G   1% /var/games
rpool/ROOT/ubuntu_auw0m6/var/lib                  399G  9.6G  389G   3% /var/lib
rpool/ROOT/ubuntu_auw0m6/usr/local                390G  462M  389G   1% /usr/local
rpool/ROOT/ubuntu_auw0m6/var/log                  390G  293M  389G   1% /var/log
rpool/ROOT/ubuntu_auw0m6/var/mail                 389G  256K  389G   1% /var/mail
rpool/ROOT/ubuntu_auw0m6/var/spool                389G  4.3M  389G   1% /var/spool
rpool/ROOT/ubuntu_auw0m6/var/snap                 389G  6.9M  389G   1% /var/snap
rpool/ROOT/ubuntu_auw0m6/var/www                  389G  256K  389G   1% /var/www
rpool/ROOT/ubuntu_auw0m6/var/lib/NetworkManager   389G  1.0M  389G   1% /var/lib/NetworkManager
rpool/ROOT/ubuntu_auw0m6/var/lib/AccountsService  389G  256K  389G   1% /var/lib/AccountsService
rpool/ROOT/ubuntu_auw0m6/var/lib/apt              390G  114M  389G   1% /var/lib/apt
rpool/var/lib/docker                              390G   72M  389G   1% /var/lib/docker
rpool/ROOT/ubuntu_auw0m6/var/lib/dpkg             390G   80M  389G   1% /var/lib/dpkg
tmpfs                                              16G     0   16G   0% /run/qemu
bpool/BOOT/ubuntu_auw0m6                          1.8G  301M  1.5G  17% /boot
/dev/nvme0n1p1                                    511M   53M  459M  11% /boot/efi
tmpfs                                             3.2G   76K  3.2G   1% /run/user/128
tmpfs                                             3.2G  1.8M  3.2G   1% /run/user/1000

Show Linux Disk Space Usage and used inodes on mounted filesystems

The df -i command in Linux is used to display information about the file system Linux Disk Space Usage, specifically focusing on the number of inodes used and available on each file system. An inode is a data structure used to represent a file or directory in the file system.

What are inodes? Inodes are data structures in a filesystem that store metadata about files, such as their size, ownership, permissions, and location on the disk. Every file and directory in a filesystem is represented by an inode.

Why is it important? Unlike disk space, which can be expanded, the number of inodes in a filesystem is fixed when it’s created. If you run out of inodes, you won’t be able to create new files or directories, even if there’s free disk space available.

When you run df -i, it lists information similar to the df command, but it specifically includes inode-related details. This can be useful for monitoring inode usage, especially on file systems that have a limited number of inodes available. Inodes are important because they represent the metadata associated with files, such as ownership, permissions, and file type.

Bash
df -i

Display disk partition sizes and types

The most important command to display disk information is fdisk

Purpose:
A tool for creating, editing, and managing partitions on hard drives.

Compatibility:
Works with various partition table formats (GPT, MBR, Sun, SGI, BSD).

Partitions:
Divide storage drives into logical sections (partitions).

Key Features:
Optimizes partition layout for modern drives (4K sectors)
Aligns partitions for best performance.
Prioritizes easy-to-use interface for human users.

Bash
fdisk -l

Display disk usage for all files and directories in human-readable format

du -ah (disk usage – all – human-readable):

  • Purpose:
    It quickly shows you how much disk space is being used by files and folders, including hidden ones (files starting with a dot).
  • Benefits:
    • Easy to read: Displays sizes in a friendly format (KB, MB, GB) instead of just numbers.
    • Comprehensive: Includes hidden files in the calculations.

Example:

If you run du -ah in your home folder, you’ll see a list of all the folders and files in your home directory, along with how much space each one is using, presented in an easy-to-understand way.

Bash
du -ah

Display total disk usage of the current directory

du -sh gives you a quick summary of how much disk space a specific directory (and everything inside it) is using, in a format that’s easy to read.

Bash
du -sh

Linux Disk Space Usage – Find large files:

Summarizes disk usage and sorts by size (largest first)

du -h –summarize .: This command uses du to estimate disk usage of the current directory (.) and its subdirectories, displaying the sizes in a human-readable format (-h) and summarizing the total usage of each directory (–summarize). Instead of listing each file’s size, it provides a summary for each directory.

sort -nr: This command sorts the output from the previous command in reverse (-r) numerical (-n) order. By default, sort sorts text data, but with the -n option, it treats the data as numerical. With -r, it sorts in descending order.

So, when you run du -h –summarize . | sort -nr, it calculates the disk usage for each directory in the current directory and its subdirectories, summarizes the total usage for each directory, and then sorts the output in descending order based on the disk usage.

Bash
du -h --summarize . | sort -nr

Focus on specific directories:

It shows only the size of the /var/log directory

Elsewhere On TurboGeek:  What is Linux, and Why Should I Use It?

Bash
du -sh /var/log

Combine with find:

Locates files larger than 100MB

Bash
find . -type f -size +100M -print

Explore advanced options:

  • du --help or man du will show the full list of options (including --apparent-size-a, and -c) for customizing output

Now you know how to get started on freeing up disk space on Linux. Check out our other blogs. We welcome comments below.

Richard.Bailey

Richard Bailey, a seasoned tech enthusiast, combines a passion for innovation with a knack for simplifying complex concepts. With over a decade in the industry, he's pioneered transformative solutions, blending creativity with technical prowess. An avid writer, Richard's articles resonate with readers, offering insightful perspectives that bridge the gap between technology and everyday life. His commitment to excellence and tireless pursuit of knowledge continues to inspire and shape the tech landscape.

You may also like...

Leave a Reply

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

Translate »