How to Check Disk Space Usage in Linux

How to Check Disk Space Usage in Linux

Running out of disk space on a server is a classic problem that can cause everything from application errors to a complete system crash. Proactive monitoring of your disk space usage is a critical task for maintaining a healthy, high-performing server. Understanding the state of your Linux file system helps you prevent these issues before they affect your users.

When you need to check Linux disk space, the terminal provides a powerful set of tools to quickly identify what’s taking up room and where. This guide will walk you through the essential commands, from basic checks to advanced analysis, so you can keep your server’s storage in check.

How to Check Disk Space in Linux

The primary Linux command to check disk space comes in two main forms: df for a high-level view of your filesystems, and du for a detailed look at directory and file sizes. Understanding the difference is key: df (disk free) reports on the total space used and available on your mounted partitions, while du (disk usage) calculates the space consumed by specific files and directories. You’ll use df to see if a partition is getting full and du to find out what’s filling it up.

Check file space usage in Linux with the df command

The df command gives you a quick overview of all your mounted filesystems, their total size, how much space is used, and how much is available. Running it without any options produces output in 1-kilobyte blocks, which can be hard to read. Always use the -h (human-readable) flag to see sizes in gigabytes (G), megabytes (M), and kilobytes (K).

df -h

The output shows each filesystem, its size, the amount used and available, the percentage of space used, and where it’s mounted. This is your first stop when you suspect a disk space issue.

Check file space usage in Linux with the du command

Once df tells you a partition is nearly full, the Linux du command helps you pinpoint the culprit. By default, du shows the size of every subdirectory, which can be overwhelming. To get a summary for a specific directory, use the -s (summarize) and -h (human-readable) flags.

du -sh /var/log

This command will return a single, human-readable total for the /var/log directory. To see the size of each subdirectory one level deep, you can use the --max-depth flag:

du -h --max-depth=1 /var

This helps you systematically drill down into the file system to find where space is being consumed.

While df and du are the workhorses of disk space analysis, several other tools offer more advanced or user-friendly ways to get the job done.

Check disk space usage in Linux with the ncdu command

The Linux ncdu command provides an interactive, ncurses-based disk usage analyzer right in your terminal. After installing it (sudo apt install ncdu or sudo yum install ncdu), run ncdu /path/to/directory. It will scan the directory and present an interactive, sorted list of files and directories by size. You can navigate with the arrow keys, drill down into subdirectories, and even delete files or directories directly from the interface. This makes ncdu one of the fastest ways to find and clean up what’s consuming your Linux disk space.

Check disk space usage in Linux with the pydf command

If you find the standard df output a bit plain, the Linux pydf command is a great alternative. As a Python-based script, pydf displays the same information as df but with a colorful, more readable layout that helps you quickly spot filesystems that are nearing capacity. You may need to install it first (sudo apt install pydf or sudo yum install pydf), but its clear output can make monitoring your Linux disk size a more manageable experience.

List block devices in Linux with the lsblk command

Sometimes you need to see the underlying physical and logical disks that your filesystems are built on. The lsblk command in Linux (list block devices) provides this context. It displays a tree view of all your block devices – disks, partitions, and logical volumes – and shows how they are mounted. This is especially helpful for understanding the relationship between the devices df reports on and the actual hardware in your server.

Get detailed file information in Linux with the stat command

When you need to know everything about a single file, the stat command is your tool. Running stat filename provides detailed metadata, including the file’s exact size, its block usage on disk, permissions, and important timestamps like its last access, modification, and change times. This is perfect for deep inspection when analyzing a specific file’s properties.

Ways to Combine Commands During Disk Space Checks

The real power of the Linux command line comes from combining tools. By piping the output of one command into another, you can create highly specific queries to find exactly what you’re looking for.

Sort files by size

One of the most common tasks is finding the largest files or directories. You can easily sort Linux files by size by combining du and sort. The -r flag for sort reverses the order (to show largest first), and -h makes it understand human-readable numbers like ‘1G’ or ’10M’.

du -h /var | sort -rh | head -n 10

This command finds the top 10 largest directories within /var, giving you an instant list of places to investigate.

Exclude files by type

Sometimes you want to calculate a directory’s size while ignoring certain files, like backups or archives. The du command has a handy --exclude flag for this purpose.

du -sh --exclude='*.zip' /home/user/documents

This calculates the total size of the documents directory but ignores all .zip files in its calculation.

Exclude files by size

To find files based on their size, the find command is the perfect tool. You can use it to locate all files larger (or smaller) than a specific size, which is invaluable for cleaning up large log files or old archives.

find /var/log -type f -size +100M

This command will search the /var/log directory and list every file (-type f) that is larger than 100 megabytes (-size +100M).

Linux Disk Space Usage Management FAQ

What’s the difference between df and du?

df (disk free) reports on filesystem-level usage, showing the total size and free space on a partition. du (disk usage) calculates the space consumed by a specific set of files or directories. Use df to see if a disk is full and du to find out what is filling it.

Why does my disk usage (df) seem higher than the files I can find (du)?

This common issue is often caused by open file descriptors. If a program is writing to a file that has been deleted, the space is not freed until the program closes the file. du can’t see the deleted file, but df knows the space is still allocated. Use a command like lsof +L1 to find processes holding onto deleted files.

What are the first places to check when a disk is full?

Start with common culprits for runaway growth: log directories (/var/log), temporary file directories (/tmp and /var/tmp), and user home directories (/home).

How can I safely clean up log files?

Avoid simply deleting log files that are in use. The best practice is to use the logrotate utility, which handles rotation, compression, and deletion automatically. For manual cleanup, you can truncate a file to zero size without deleting it by running > filename. This frees up the space immediately, even if a process still has the file open.

Conclusion

Proactive monitoring is the key to preventing storage-related outages. By regularly using tools like df, du, and the more advanced ncdu, you can stay ahead of problems and keep your server running smoothly. Learning to combine these commands with sort, find, and other filters gives you a powerful toolkit for analyzing and managing your Linux disk space efficiently.

Ultimately, having a reliable platform is the foundation of a healthy server. A reliable Linux VPS gives you the solid base you need, and with these disk management skills, you’ll be well-equipped to maintain it for the long term.

Scroll to Top