Black Friday Deals Not Found Anywhere Else! Save up to 55% OFF Hosting, Domains, Pro Services, and more.
Vodien Black Friday Sale applies to new purchase on select products and plans until 4 December 2024. Cannot be used in conjunction with other discounts, offers, or promotions.
Why Businesses Are Reassessing Vendor Lock-In Risks

Best Commands to Check Disk Space Usage in Linux: A Complete Guide

When you run out of disk space on your Linux system, everything stops working. Suddenly, you can’t save new files, applications might crash, and your system’s performance can degrade. That’s why regularly monitoring your disk space usage isn’t just good practice; it’s essential for keeping your Linux machine running smoothly and efficiently. 

This guide is your go-to resource for checking disk space in Linux. We’ll explore a range of powerful commands, from quick overviews to in-depth analyses, giving you the knowledge to confidently monitor and manage your Linux storage and keep your system running smoothly. Let’s get started and ensure you never get caught off guard by an entire disk again!

Understanding Disk Space in Linux

Before we get into the commands, let’s quickly define “disk space” in a Linux system. Essentially, it’s the amount of space on your storage devices—your hard drives or SSDs—to store your operating system, applications, files, etc.

Disk space is typically broken down into a few key categories:

  • Total Disk Space: The maximum capacity of your storage device.
  • Used Disk Space: The portion currently occupied by files.
  • Free Disk Space: The remaining space available for new files.
  • Available Disk Space: Slightly less than free space, as some is reserved for system use

Using the df Command to Check Disk Space

The df command (short for “disk free”) provides information about file system disk space usage, showing how much space is used and available on each mounted file system.

Basic Syntax:

Simply typing df in your terminal will give you a table of information.

df

   

However, the output in its raw form can be a bit cryptic, showing sizes in kilobytes. To make it more human-friendly, we use the following options:

  • -h (Human-Readable): This option displays sizes in a more understandable format, like kilobytes (K), megabytes (M), gigabytes (G), and terabytes (T). 
  • -T (Type): This option includes a “Type” column in the output, which shows the file system type (e.g., ext4, tmpfs, etc.). 

Example Command with Options:

df -hT

Example Output:

Understanding the Columns:

  • Filesystem: The name of the file system (partition).
  • Type (-T flag): File system type (e.g., ext4, vfat).
  • Size: Total file system capacity.
  • Used: Space currently occupied.
  • Avail: Available space for use.
  • Use%: Percentage of used space.
  • Mounted on: Directory where the file system is attached (e.g., / for root).

Checking Disk Usage with the du Command

While df gives you the big picture of file system usage, the du command (short for “disk usage”) is a Linux disk usage analyzer that dives deeper. It allows you to analyze disk space usage at the directory level, which is invaluable for pinpointing where space is consumed within your file system.

Basic Syntax:

du [options] [directory]

 

Running du without options shows disk usage for the current directory and all sub-directories, which can be overwhelming. Useful options:

  • -h (Human-Readable): Displays sizes in K, M, G for better readability.
  • -s (Summary): Shows only the total for each directory instead of listing all subdirectories.

Example Command to Check Current Directory Usage:

du -sh.

 

(The . (dot)  represents the current directory.)

This output tells you that the current directory (and everything within it) uses 669 megabytes of disk space.

Example Command to Check Usage of a Specific Directory (e.g., /var/log):

du -sh /var/log

  Example Output:

 This shows that the /var/log directory uses 421 megabytes of disk space.

Exploring the lsblk Command for Block Devices

The lsblk command lists block devices like hard drives, partitions, and logical volumes in a tree format. Unlike df or du, it doesn’t check space on the Linux server but helps visualize storage layout and capacity

Basic Syntax:

lsblk

   

Example Output:

Understanding the Output:

  • NAME: The name of the block device (e.g., sda, sda1, sda2). sda is typically your first hard drive, and sda1 and sda2 are partitions on that drive.
  • MAJ:MIN: Major and minor device numbers (less critical for basic usage).
  • RM: Is the device removable (1 for removable, 0 for fixed).
  • SIZE: The size of the block device or partition.
  • RO: Whether the device is read-only (1 for read-only, 0 for read-write).
  • TYPE: The type of block device (disk, part, lvm, etc.).
  • MOUNTPOINTS: Where the partition is mounted (if applicable).

Using the stat Command for File-Specific Information

The stat command provides detailed file or system status, including size and disk blocks used. While not meant for disk usage monitoring, it helps inspect individual files.

Basic Syntax:

stat [filename]

Example Command to Check Information about a File (e.g., my_document.txt):

stat my_document.txt

   

Example Output (snippet):

   Understanding Relevant Output:

  • Size: The actual size of the file in bytes (12345 in this example).
  • Blocks: The number of disk blocks allocated to the file.

Advanced Disk Usage Monitoring with ncdu Command

ncdu (NCurses Disk Usage) is a fantastic tool for visually and intuitively exploring disk usage. It provides a navigable interface within your terminal, allowing you to browse directories and see their disk space usage in real time.

Installation (if not already installed):

On most Debian/Ubuntu-based systems:

sudo apt install ncdu

On Fedora/CentOS/RHEL-based systems:

sudo yum install ncdu  # or sudo dnf install ncdu

   

Basic Usage:

To start ncdu in your current directory:

ncdu

   

To start ncdu for a specific directory (e.g., /home):

ncdu /home

   

Once ncdu starts, it will scan the directory to check space on the Linux server and display a list of directories and files, sorted by size in descending order.

  • Arrow Keys (Up/Down): Navigate through the list.
  • Enter: Drill down into a directory to see its contents.
  • q: Quit ncdu.
  • n: Sort by filename.
  • s: Sort by size (default).
  • d: Delete the currently selected file or directory (use with caution!).

Combining Commands for More Efficient Disk Space Checks

For even more powerful Linux disk space analysis, you can combine these commands and use shell features like pipes (|) to filter and refine your results.

Example: Finding the Top 10 Largest Directories in /var:

du -sh /var/* | sort -hr | head -n 10

   

Using Disk Space Monitoring for Linux Hosting and VPS Hosting

For Linux VPS servers and VPS hosting, disk space monitoring is crucial to prevent downtime, app failures, and crashes.

Why It Matters:

  • Performance Impact: Low space slows operations.
  • Service Interruptions: Databases and web servers may fail.
  • Data Loss Risk: Full disks can cause corruption.
  • Hosting Limits: Exceeding quotas may lead to extra charges or suspension.

Conclusion

Let’s recap: Monitoring disk usage in Linux is essential for maintaining system performance, preventing crashes, and ensuring smooth server operations. Whether you’re managing a personal system or a VPS, regularly checking disk space helps avoid service interruptions and potential data loss.

Ready to implement this knowledge and ensure your systems are always running smoothly?  Explore Voiden VPS today and take complete control of your server environment!

Frequently Asked Questions (FAQs)

What is the difference between df and du commands?

df (disk free) shows file systems’ overall disk space usage, while du (disk usage) displays space used by specific files and directories.

How can I check disk space usage on a remote Linux server?

Use ssh user@remote_server “df -h” to check overall disk space or ssh user@remote_server “du -sh /path/to/directory” for specific directories.

How do I find the most extensive files using du?

Run du -ah /path | sort -rh | head -10 to list the 10 most extensive files and directories in the specified path.

Can I automate disk space checks on Linux?

Yes, use a cron job with df -h or du -sh and send alerts via email or logs. Example: crontab -e and add 0 * * * * df -h | mail -s “Disk Usage Report” [email protected].