How To Check Disk Space In Linux - Commands For Disk Space Usage

Updated on May 16, 2024

When managing disk space on Linux systems, there are several powerful command-line tools that can help you analyze storage usage and find areas for optimization. In this article, we'll look at how to use these commands to understand and manage your disk space on Linux.

Key Takeaways

  • The 'df' command shows total and available storage space on every disk attached to the system
  • 'df -H' displays output in a human-readable format (MB, GB, TB)
  • Customize 'df' output with '--output' flag followed by field names (e.g., source, size, used, avail)
  • The 'du' command reports sizes of directories and files
  • Combine 'du' with 'sort' and 'head' to find the largest directories (e.g., 'du -a . | sort -n -r | head -n 10')

df Command

Overview of the 'df' command

The 'df' command, which stands for "disk filesystem", is a tool for checking disk space usage on Linux systems. It shows information about the total and available storage space on every disk attached to the system, including local drives and network file systems. This command has been used by Linux users to analyze disk space utilization since the early days of the operating system in the 1990s.

Basic usage of 'df'

To use 'df', run the command with the '-H' flag, like this: df -H. This shows the output in a human-readable format, displaying disk space sizes in megabytes, gigabytes, or terabytes. The output includes several columns of information for each file system:

Column Description
Filesystem The file system name or device path
Size The total size of the file system
Used The amount of space used on the file system
Available The remaining free space on the file system
Use% The percentage of the file system that is occupied
Mounted on The mount point where the file system is attached

For example, to check how much space is left on your main system drive, which is usually mounted at '/', run:

df -H /

This might output something like:

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1       100G   65G   35G  65% /

To view information about a specific disk or partition, include the device path as an argument to 'df'. For instance, df -H /dev/sda1 shows the space usage for the first partition on the primary SATA drive.

Customizing 'df' output

By default, 'df' includes a set of columns in its output. You can customize this using the '--output' flag followed by a comma-separated list of field names. Some useful fields are:

  • source: the file system source device
  • size: total file system size
  • used: amount of space used
  • avail: amount of available space
  • pcent: percentage of space used

For example, to create a report showing the device, total size, used space, and available space, run:

df -H --output=source,size,used,avail

This prints a table with the selected columns:

Filesystem      Size  Used Avail
/dev/sda1       100G   65G   35G
/dev/sda2       500G  100G  400G

du Command

What is the 'du' command?

The du command, short for "disk usage", is a Linux utility that reports the sizes of directories and files. Unlike the df command which provides a high-level view of disk utilization by file system, du lets you see the space used by specific directories and files. This makes it useful for finding which subdirectories and files are using the most disk space on your Linux system.

How to use 'du'

To use du, give the directory you want to analyze as an argument:

du /home/user/Documents

This outputs a list of all subdirectories in /home/user/Documents, along with the disk space each one uses. By default, the sizes are shown in kilobytes.

To make the output easier to read, use the -h flag to show sizes in human-readable format (KB, MB, or GB), and the -s flag to show only a summary for the directory:

du -sh /home/user/Documents

This might output something like:

25G     /home/user/Documents

Showing that the /home/user/Documents directory and its contents use 25 gigabytes of disk space in total.

You can also use du on individual files to see their disk usage:

du -h /home/user/Documents/large-file.zip

For example, if you have a large video file in your Documents folder, you could run:

du -h /home/user/Documents/vacation-video.mp4

And get output like:

2.3G    /home/user/Documents/vacation-video.mp4

Showing that the video file takes up 2.3 gigabytes of space.

Finding largest directories with 'du'

A common use for du is to find directories that are using the most disk space. While you could check the output manually to find the largest subdirectories, it's easier to combine du with other commands to sort the results.

To list the top 10 subdirectories in the current directory by size, run:

du -a . | sort -n -r | head -n 10

Here's what each part of the command does:

  • du -a .: Calculates the disk usage of all files and subdirectories in the current directory (.). The -a flag includes files as well as directories.
  • sort -n -r: Sorts the du output numerically (-n) in reverse order (-r), so the largest numbers are first.
  • head -n 10: Takes the first 10 lines of the sorted output.

The full command gives you a list of the 10 largest subdirectories and files, which you can then look at further to see if any space can be reclaimed.

You can also specify a different directory to start from:

du -a /home/user/Documents | sort -n -r | head -n 10 

This finds the top 10 space consumers within the /home/user/Documents directory and its subdirectories.

For example, say your /home/user/Documents directory contains these subdirectories:

/home/user/Documents/work
/home/user/Documents/personal
/home/user/Documents/photos
/home/user/Documents/music

Running the command above might output something like:

45G     /home/user/Documents/photos
30G     /home/user/Documents/music  
10G     /home/user/Documents/work
5G      /home/user/Documents/personal

Showing that the photos and music subdirectories are using the most space.

stat Command

The stat command displays information about a file, directory, or file system, including its size, block size, and disk usage.

To use stat, specify the path to the file, directory, or device you want to analyze. For example:

stat /home/user/Documents/file1.txt

This will output information about file1.txt:

  File: /home/user/Documents/file1.txt
  Size: 4096            Blocks: 8          IO Block: 4096   regular file
Device: 10301h/66049d   Inode: 1234567     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/    user)   Gid: ( 1000/    user)
Access: 2023-04-20 10:30:00.000000000 +0200
Modify: 2023-04-20 10:30:00.000000000 +0200
Change: 2023-04-20 10:30:00.000000000 +0200
 Birth: -

From this output, you can see the file's size (4096 bytes), the number of blocks it occupies on disk (8), and other metadata such as permissions, ownership, and timestamps.

You can also use stat on a directory or file system. For example:

stat /home/user/Documents

This will show the size and disk usage of the /home/user/Documents directory, with other information.

Scenarios where stat can be helpful include:

  • Analyzing the disk usage of a specific file or directory to identify storage inefficiencies
  • Checking file permissions and ownership to troubleshoot access issues
  • Examining file timestamps to determine when a file was last modified or accessed

Example: Checking File Permissions and Ownership

To check the permissions and ownership of a file, you can use stat:

stat /home/user/Documents/important_file.txt

The output will include the file's permissions and ownership:

Access: (0640/-rw-r-----)  Uid: ( 1000/    user)   Gid: ( 1000/    user)

In this case, the file has read and write permissions for the owner (user), read permissions for the group (user), and no permissions for others. The owner and group are both "user" with UID 1000 and GID 1000.

If the permissions or ownership are incorrect, you can use chmod and chown to adjust them:

chmod 0644 /home/user/Documents/important_file.txt
chown user:group /home/user/Documents/important_file.txt

This sets the permissions to -rw-r--r-- (read and write for owner, read for group and others) and changes the owner to "user" and the group to "group".

fdisk -l Command

The fdisk -l command is used to display information about disk partitions and their sizes. It lists the partition tables for all storage devices attached to the system and shows the disk layout.

Running fdisk -l as root (or with sudo) will output information about each disk and its partitions:

Disk /dev/sda: 50 GiB, 53687091200 bytes, 104857600 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: A1B2C3D4-E5F6-G7H8-I9J0-K1L2M3N4O5P6

Device       Start      End  Sectors Size Type
/dev/sda1     2048   206847   204800 100M EFI System
/dev/sda2   206848  2303999  2097152   1G Linux filesystem
/dev/sda3  2304000 31457279 29153280  14G Linux filesystem

This output shows that /dev/sda has a total size of 50 GiB and contains three partitions: a 100 MB EFI System partition, a 1 GB Linux filesystem partition, and a 14 GB Linux filesystem partition.

The fdisk -l command is useful for understanding the structure of your storage devices and the sizes of the partitions. It can help with tasks such as:

  • Identifying the total size and free space on each storage device
  • Determining the partition layout and sizes when planning a new installation or repartitioning
  • Verifying that partitions are set up correctly and aligned with the storage device

Example: Identifying Total Disk Size and Free Space

To quickly check the total size and free space of a disk, you can combine fdisk -l with grep and awk:

sudo fdisk -l /dev/sda | grep -e "Disk /dev/sda" -e "^$" | awk '{print $3 " " $4}'

This command extracts the relevant lines from the fdisk -l output and displays the total disk size and unit:

50 GiB

To calculate the free space, subtract the used space (sum of partition sizes) from the total disk size. You can automate this with a script:

#!/bin/bash

disk="/dev/sda"
total_size=$(sudo fdisk -l $disk | grep "Disk $disk" | awk '{print $3}')
used_space=$(sudo fdisk -l $disk | awk '/^\/dev/ {sum += $5} END {print sum}')

free_space=$((total_size - used_space))
echo "Total size: $total_size bytes"
echo "Used space: $used_space bytes"
echo "Free space: $free_space bytes"

This script calculates the total disk size, sums up the partition sizes to get the used space, and subtracts the used space from the total size to determine the free space. The output will look like:

Total size: 53687091200 bytes
Used space: 15728640000 bytes
Free space: 37958451200 bytes

This helps you quickly assess the free space on a disk without having to manually calculate it from the partition sizes.

ls -al Command

The ls -al command lists a directory's contents, including hidden files, and shows their sizes with other information. It is useful for analyzing disk usage of specific files within a directory.

For example, running ls -al in the /home/user/Documents directory may show output like:

total 24
drwxr-xr-x 2 user user 4096 Apr 20 10:30 .
drwxr-xr-x 5 user user 4096 Apr 19 09:15 ..
-rw-r--r-- 1 user user 4096 Apr 20 10:30 file1.txt
-rw-r--r-- 1 user user 8192 Apr 20 10:15 file2.txt
drwxr-xr-x 2 user user 4096 Apr 19 15:45 subdirectory

Here, ls -al provides a listing of the directory, showing file permissions, ownership, modification dates, and sizes. This command helps when you need to analyze the disk usage of individual files within a directory.

Some use cases for ls -al include:

  • Finding large files that are taking up disk space
  • Checking file permissions and ownership to ensure access control
  • Comparing file modification dates to see which files have been recently updated

Example: Finding Large Files

To find large files in a directory, you can combine ls -al with sort and head:

ls -al /home/user/Documents | sort -k 5 -n -r | head -n 5

This command lists the contents of /home/user/Documents, sorts the output by the fifth column (file size) in reverse numerical order, and displays the top 5 results. The output will show the largest files in the directory:

-rw-r--r-- 1 user user 1048576 Apr 20 11:00 large_file1.bin
-rw-r--r-- 1 user user  524288 Apr 20 10:45 large_file2.bin
-rw-r--r-- 1 user user  262144 Apr 20 10:30 large_file3.bin
-rw-r--r-- 1 user user  131072 Apr 20 10:15 large_file4.bin
-rw-r--r-- 1 user user   65536 Apr 20 10:00 large_file5.bin

This helps you quickly identify the largest files that may be consuming disk space.

Combining Commands for Detailed Analysis

Sorting Files and Directories by Size

You can combine the du command with sort to list files and directories sorted by their disk usage. This is useful for finding the largest files and directories.

For example, to sort the contents of your Desktop directory by size from largest to smallest, run:

du -h /home/user/Desktop | sort -rn

The -h flag makes the output easy to read, while sort -rn sorts the results numerically (-n) in reverse order (-r).

If you only want to see the largest or smallest results, you can pipe the output to head or tail:

Command Description
du -h /home/user/Desktop \| sort -rn \| head -n 5 See the 5 largest files/directories on your Desktop
du -h /home/user/Desktop \| sort -rn \| tail -n 5 See the 5 smallest files/directories on your Desktop

Here's an example of what the output might look like when finding the 5 largest items:

4.0G    /home/user/Desktop/videos
1.5G    /home/user/Desktop/images
1.0G    /home/user/Desktop/project.zip
500M    /home/user/Desktop/music
100M    /home/user/Desktop/documents

This shows that the videos directory is taking up the most space at 4.0 gigabytes, followed by images at 1.5 gigabytes, and so on.

Filtering Results by Size

To find files that are larger than a certain size, you can combine du with grep. The grep command searches for lines that match a pattern.

To find files in your Desktop directory that are larger than 1 gigabyte, run:

du -h /home/user/Desktop | grep '^\s*[0-9\.]\+G'

This looks for lines that start with (^) some whitespace (\s*), followed by one or more (+) digits or periods ([0-9\.]), and end with a G (for gigabytes).

You can change the G to M to find files larger than a megabyte, or K for kilobytes:

# Find files larger than 100 megabytes
du -h /home/user/Desktop | grep '^\s*[0-9\.]\+M'

# Find files larger than 10 kilobytes  
du -h /home/user/Desktop | grep '^\s*[0-9\.]\+K'

For instance, if you run the command to find files larger than 100 megabytes, you might see output like this:

200M    /home/user/Desktop/presentation.ppt
150M    /home/user/Desktop/archive.tar.gz
120M    /home/user/Desktop/video.mp4

This technique is handy when you need to find large files that might be taking up too much disk space.

Excluding Files by Type

When analyzing disk usage, you may want to exclude certain types of files from the results. The du command has an --exclude flag that lets you skip files that match a pattern.

For example, to get the disk usage of your Desktop directory while excluding text files, run:

du -h /home/user/Desktop --exclude="*.txt"

This will calculate the sizes of all files and subdirectories as usual, but will leave out any files ending in .txt.

du -h /home/user/Desktop --exclude={"*.txt","*.jpg","*.png"}

Both of these commands will analyze the disk usage while skipping text files, JPEGs, and PNGs.

Using --exclude is helpful when you're trying to find large files to clean up, but know that certain file types are supposed to be there and don't need to be counted. For instance, if you have a large photo collection, you can exclude image files to focus on other types of files that might be taking up space unnecessarily.

Practical Use Cases

Here are some real-life situations where combining du with other commands can be beneficial:

  • Freeing up disk space: When your hard drive is low on storage, you can use du -h | sort -rn | head to identify the largest directories and files. This helps you decide what to delete or move to another drive to free up space.

  • Preparing for a system migration: Before migrating your data to a new computer or drive, it's a good idea to check your current storage usage. Commands like du -h --exclude="*.bak" --exclude="*.tmp" let you get an accurate picture of your data's size while excluding temporary or backup files that don't need to be transferred.

  • Troubleshooting performance issues: If your system is slow or certain processes are taking a long time, it could be because they're processing very large files. Using du -h | grep '^\s*[0-9\.]\+G' can help you spot large files that might be causing performance problems.