4 Ways To Check Disk Space in Linux

Managing storage space is a critical task for any administrator to ensure system stability and performance.

Without enough room, your system might trigger a no space left on device error, which can halt database operations or prevent software updates. Checking disk space in Linux helps you monitor consumption across partitions and identify specific files or directories that are exhausting your resources.

This guide explains four practical methods to verify your storage levels using built-in terminal commands. Master these approaches to maintain effective control over your file system.

Method 1: Using the du Command

The linux du command reveals actual disk consumption by calculating the physical space files occupy on a storage device. It is the best tool for investigating which directories are “space hogs.”

Run this command for basic usage: du -h filename

The -h flag is essential as it converts bytes into human-readable units like KB, MB, or GB. To check an entire directory and its subfolders, use the -s flag to get a total summary: du -sh /var/log

This provides an aggregate consumption total, accurately reflecting storage usage even when dealing with complex directory structures.

Method 2: Applying the df Command

While du checks directory usage, the df (disk free) command provides a high-level overview of your entire file system. it shows the total capacity, used space, and available room on all mounted partitions.

Use this syntax for a human-readable report: df -h

This is typically the first command used when troubleshooting storage issues, as it quickly identifies if your root (/) or home (/home) partitions are nearing 100% capacity. It ensures you have the necessary disk space needed for installation before starting large updates.

See also: Mastering the Linux Command Line — Your Complete Free Training Guide

Method 3: Using the ls Command

The ls command is primarily used to list directory contents, but it also provides quick size information for individual files.

Run this command to view file details: ls -lh filename

The -l flag activates the long format, displaying permissions and owners, while the fifth column shows the file size. To identify the largest files in a folder, combine it with the -S flag: ls -lhS

This sorts the output from largest to smallest, making it easy to spot massive log files or archives. Note that ls reports “apparent size” rather than actual disk allocation.

Method 4: Searching with the find Command

The find command is an advanced tool that locates files matching specific size criteria across your entire system. This is invaluable for hunting down large files hidden deep within the directory tree.

To locate files larger than 100MB, use: find / -size +100M

You can even set ranges to find large files in Linux between specific thresholds, such as between 50MB and 100MB: find / -size +50M -size -100M.

This command searches recursively through directories and can be filtered by type (e.g., -type f for regular files) to provide a more focused result.


Understanding Apparent vs. Actual Size

Linux stores data in fixed blocks, usually 4KB each. This leads to two different ways of measuring size:

  • Apparent Size: The actual number of bytes the data contains (shown by ls).
  • Actual Size: The total amount of physical disk space the file occupies (shown by du).

For example, a tiny 1-byte file will still occupy a full 4KB block on the disk. This difference is particularly noticeable with sparse files, which contain “holes” of empty data that do not consume physical storage but appear large in metadata.

Step-by-Step Process

  1. Open your terminal application.
  2. Run df -h to see which partition is running low on space.
  3. Navigate to the suspect directory using cd.
  4. Execute du -sh * | sort -h to list all subdirectories ranked by their storage consumption.
  5. Use ls -lhS within the largest directories to find individual files to delete or compress.

Practical Tips

  • Include Hidden Files: Use the -a flag (e.g., ls -la) to ensure you aren’t missing hidden configuration files that might be taking up space in home directories.
  • Check Exact Bytes: If you are writing a script, use stat -c "%s" filename to get the exact byte count without human-readable conversion.
  • Monitor /tmp: Remember that /tmp is a world-writable space often used for temporary data; check it regularly to prevent it from filling up your root partition.

Summary Table

TaskRecommended CommandBest For…
Check partition statusdf -hViewing free space on all disks.
Directory totalsdu -sh dirnameChecking how much space a folder uses.
Sort files by sizels -lhSFinding the biggest files in a folder.
Hunt for massive filesfind / -size +500MFinding large files system-wide.
Exact byte countsstat -c "%s" filePrecise measurements for scripts.

FAQs

What’s the quickest way to check disk space? Run df -h. It provides an immediate overview of all your drives and their current percentage of usage.

Why do du and ls show different file sizes? ls shows the “apparent size” (content), while du shows the “actual size” (block allocation). Sparse files and block-level storage defaults cause this discrepancy.

Can I check directory size recursively? Yes, du -sh directoryname will calculate the total consumption of that folder plus all of its subdirectories.

How do I find all large files on my system? Run find / -type f -size +100M to list every regular file larger than 100MB.

Related Posts

David Cao
David Cao

David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.

Articles: 611

Leave a Reply

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