Linux Disk Space Usage Commands

Want to improve your Linux Disk Space Usage game? Need to safely free up disk space on your server? This is the article you need. 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

df is the most basic of Linux Disk Space Usage commands used.

This is what the Man file has to say about df

This manual page documents the GNU version of df. df displays 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.

If an argument is the absolute file name of a disk device node containing a mounted file
system, df shows the space available on that file system rather than on the file system conโ€
taining the device node. This version of df cannot show the space available on unmounted
file systems, because on most kinds of systems doing so requires very nonportable intimate
knowledge of file system structures.

Bash
df -h

Here is an example of the output:

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.

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

This is what the man pages of fdisk say:

fdisk is a dialog-driven program for creation and manipulation of partition tables. It
understands GPT, MBR, Sun, SGI and BSD partition tables.

Block devices can be divided into one or more logical disks called partitions. This division
is recorded in the partition table, usually found in sector 0 of the disk. (In the BSD world
one talks about `disk slices’ and a `disklabel’.)

All partitioning is driven by device I/O limits (the topology) by default. fdisk is able to
optimize the disk layout for a 4K-sector size and use an alignment offset on modern devices
for MBR and GPT. It is always a good idea to follow fdisk’s defaults as the default values
(e.g., first and last partition sectors) and partition sizes specified by the
+/-<size>{M,G,…} notation are always aligned according to the device properties.

CHS (Cylinder-Head-Sector) addressing is deprecated and not used by default. Please, do not
follow old articles and recommendations with fdisk -S <n> -H <n> advices for SSD or
4K-sector devices.

Note that partx(8) provides a rich interface for scripts to print disk layouts, fdisk is
mostly designed for humans. Backward compatibility in the output of fdisk is not guaranteed.
The input (the commands) should always be backward compatible.

Bash
fdisk -l

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

The du -ah command in Linux is used to estimate and display the Linux Disk Space Usage of files and directories in a human-readable format, including hidden files (those whose names start with a dot) and displaying the sizes in a more understandable format, such as kilobytes (K), megabytes (M), and gigabytes (G).

Breaking it down:

  • du: Short for “disk usage,” this command is used to estimate file space usage.
  • -a: This option tells du to include all files and directories, including hidden ones.
  • -h: This option is used to display sizes in a “human-readable” format, making it easier for users to interpret the sizes.

When you run du -ah, it recursively scans the current directory and all its subdirectories, calculating the disk space used by each file and directory. It then displays this information in a list format, showing the size of each file and directory along with their names. The sizes are displayed in a format such as “4.2K” for kilobytes, “23M” for megabytes, and “1.5G” for gigabytes.

Bash
du -ah

Display total disk usage of the current directory

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.

Elsewhere On TurboGeek:  Installing and Using the Alation Data Catalog

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:

Shows only the size of the /var/log directory

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

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 ยป