Robin.te.Hofstee

How to Check Disk Usage and Find Large Files on Linux

Robin te Hofstee4 min readViews3
How to Check Disk Usage and Find Large Files on Linux

After setting up a new Linux server, you’ll want to know how to check what’s taking up space. Disk space is often the weakest link in system stability

How to Check Disk Usage and Find Large Files on Linux

After setting up a new Linux server, you’ll want to know how to check what’s taking up space. Disk space is often the weakest link in system stability. Here’s how to identify space hogs quickly.

Basic Disk Space Check

The fastest way to see overall disk usage is df -h. The -h flag shows sizes in human-readable format.

df -h

This shows mounted filesystems, their total size, used space, free space, and mount points. Monitor /var/log and /home – they’re common culprits for filling up /.

Check Disk Usage Per Directory

Use du (disk usage) to see how much space each directory consumes. Without arguments, it checks the current directory. Add -h for readability and -s to show summaries per directory.

du -sh *

This lists every item in the current directory with its total size. Pipe it through sort -h to see the largest first:

du -sh * | sort -h

Find the 10 Largest Files

Use find to search for files and sort to order them by size. The following command starts from /, skips directories (-type f), and prints only files with these syntax:

find / -type f -exec du -sh {} + 2>/dev/null | sort -h | tail -n 10

Breakdown:

  • -exec du -sh {} + runs du on every found file and prints formatted sizes.
  • 2>/dev/null dismisses permission errors.
  • sort -h sorts by human-readable sizes.
  • tail -n 10 shows the 10 largest.

Warning: Running find as root can be destructive. Use sudo only if necessary.

Identify Old Log Files

Logs in /var/log often grow indefinitely. Check which service logs are largest:

sudo du -sh /var/log/* | sort -h

For example, journalctl logs can take up gigabytes. Clear old logs with:

sudo journalctl --rotate --vacuum-time=7d

This purges logs older than seven days.

Cleaning Up Temporary Files

The tmp directory (/tmp) is for temporary files. While it’s safe to clear, avoid deleting files owned by running processes. Use lsof to check active files:

lsof /tmp

For older temporary files not in use:

sudo rm -rf /tmp/*

Warning: This command is safe for /tmp, but never delete files from other directories without verification.

Advanced: Track Disk Usage Over Time

For ongoing monitoring, write a script that runs df -h periodically and logs the output. Use cron to automate it:

#!/bin/bash
df -h >> /var/log/disk_usage.log

Add to crontab to run every hour:

crontab -e

Add this line:

0 * * * * /path/to/your/script.sh

Summary

  • Use df -h to check total disk usage.
  • Run du -sh * | sort -h to list directory sizes.
  • Find large files with find / -type f -exec du -sh {} + | sort -h | tail -n 10.
  • Monitor /var/log for bloated logs and clear them with journalctl.
  • Regularly clean /tmp, but avoid deleting active files.

These commands, used together, help you maintain disk health and prevent system slowdowns.

Share this post
Robin te Hofstee

Written by Robin te Hofstee

Technology enthusiast with a deep love for programming. Sharing practical guides on React, Next.js, Linux and self-hosting.

How Git Works

1

Git is a distributed version control system that allows multiple people to collaborate on a project while maintaining a complete history of all change