ThisCalculator

Everyday

How to Find What’s Using Disk Space in Linux and Free It

Updated · Standard formula, shown below

This guide shows you how to identify which files and directories are consuming the most disk space on a Linux system, and then safely remove or relocate them to free up storage. You’ll use standard command-line tools that come pre-installed on virtually every Linux distribution, so no extra software is needed.

What You’ll Need

  • A Linux system with a terminal (SSH access works fine for remote servers).
  • Sudo or root privileges to inspect system directories and delete protected files.
  • Basic familiarity with the command line (navigating directories, running commands).

Step-by-Step

  1. Check overall disk usage. Run df -h to see which filesystems are mounted and how full they are. The output shows each mount point, total size, used space, available space, and usage percentage. Identify the partition that is nearly full (e.g., / or /home).
  2. Find the largest directories at the top level. Navigate to the root of the full filesystem (e.g., cd /) and run sudo du -x -h --max-depth=1 2>/dev/null | sort -hr | head -20. This lists the top 20 largest directories under the current directory. The -x flag keeps it on one filesystem, -h gives human-readable sizes, and --max-depth=1 limits the depth.
  3. Drill down into the largest directories. For each big directory from step 2, run the same du command with a deeper max-depth (e.g., --max-depth=2) to find the subdirectories that are taking up space. Repeat until you locate the actual files or folders that are huge.
  4. List the largest files in a directory. Use find /path/to/dir -type f -printf '%s %p\n' | sort -rn | head -20 to show the 20 biggest files in that directory. This is faster than du for finding individual files.
  5. Check for deleted but open files. On Linux, a file that is deleted while still open by a process will not release its space until the process closes it. Run lsof +L1 to list such files. The output shows the process ID and the path (often marked as deleted). If you find a large one, you can restart the process or kill it (with caution) to free the space.
  6. Examine common space hogs. Check these typical culprits:
    • Package manager caches: /var/cache/apt (Debian/Ubuntu), /var/cache/pacman (Arch), /var/cache/dnf (Fedora).
    • Log files: /var/log – large .log files or rotated archives.
    • User caches: ~/.cache (thumbnails, browser caches, etc.).
    • Old kernels: /boot or /usr/src may hold multiple kernel versions.
    • Docker images/containers: /var/lib/docker if you use Docker.
  7. Decide what to remove. For each large item, ask:
    • Is it a cache or temporary file? Safe to delete.
    • Is it a log file? Check if it’s still active, then either truncate it (sudo truncate -s 0 /var/log/big.log) or remove old rotated logs.
    • Is it a downloaded package or ISO? Delete if you no longer need it.
    • Is it a user’s personal data? Confirm with the user before deleting.
  8. Free the space. Use rm for files (e.g., rm /path/to/file) and rm -r for directories (rm -r /path/to/dir). Be absolutely certain you have the correct path before running rm, as there is no undo.
  9. Clean up package caches safely. Instead of manually deleting, use the package manager’s own cleanup:
    • Debian/Ubuntu: sudo apt clean and sudo apt autoremove.
    • Fedora: sudo dnf clean all and sudo dnf autoremove.
    • Arch: sudo pacman -Sc (keep the latest versions) or sudo pacman -Scc (remove all cached packages).
  10. Remove old kernels (if applicable). On Debian/Ubuntu, sudo apt autoremove usually removes old kernels automatically. On Fedora, sudo dnf remove --oldinstallonly does the same. On Arch, sudo pacman -Sc will not remove kernels, so you may need to manually remove them with sudo pacman -R linux-old (replace with the actual kernel package name).
  11. Empty the trash. User trash directories (~/.local/share/Trash) can accumulate large files. Empty them with rm -rf ~/.local/share/Trash/* or via your file manager.
  12. Verify the freed space. Run df -h again to confirm the filesystem now has more available space.

Common Mistakes

  • Deleting system files without knowing what they are. Never remove files in /usr, /bin, or /lib unless you are absolutely sure they are not needed. Use dpkg -S (Debian/Ubuntu) or rpm -qf (Fedora) to check which package owns a file before deleting.
  • Using rm -rf on a directory without double-checking the path. A single typo can wipe your home directory or system files. Always use ls first to confirm.
  • Ignoring open deleted files. If you delete a large file but space doesn’t free up, it’s likely still open by a process. Check with lsof +L1.
  • Deleting log files while services are running. The service may keep writing to the same inode, and the space won’t be freed until the service restarts. Instead, use truncate or restart the service after deletion.
  • Running du without sudo. You may get permission errors and miss large directories you can’t read.

Tips

  • Use ncdu if you prefer an interactive, visual interface. Install it with your package manager (sudo apt install ncdu, etc.) and run sudo ncdu / to browse sizes with arrow keys.
  • Set up log rotation with logrotate to prevent logs from growing unbounded. Most distributions already do this, but you can adjust the size limits in /etc/logrotate.conf.
  • For Docker, run docker system prune -a to remove unused images, containers, and volumes (be careful: this removes all unused images, not just dangling ones).
  • Use du -h --max-depth=1 on your home directory periodically to spot trends before they become problems.
  • Consider moving large but rarely used data to an external drive or cloud storage instead of deleting it.

Troubleshooting

  • Space still not freed after deleting files. Check for open deleted files with lsof +L1. If you find a process holding a deleted file, restart that service or kill the process (if it’s safe).
  • du shows a directory larger than the partition. This can happen if you have bind mounts or if you are not using -x and the directory includes another filesystem. Use -x to stay on one filesystem.
  • Permission denied when running du or find. Use sudo to read all directories. If you still get errors, some directories may be intentionally inaccessible (e.g., /proc).
  • You can’t find the space hog. Check for sparse files or files with holes using du -h vs ls -l – if du shows much less than the apparent size, it’s sparse. Also check for large files in /tmp, /var/tmp, and /run.
  • After deleting a file, df still shows it as used. This is the open-deleted-file issue again. Use lsof | grep deleted to find the process.

Frequently Asked Questions

What is the difference between df and du?

df reports the free space on entire filesystems, while du estimates the size of files and directories. Use df to see if a partition is full, and du to find what is filling it.

Is it safe to delete files in /tmp?

Generally yes, but only files that are not currently in use by a running process. It’s safer to reboot first, which clears /tmp automatically on most systems.

How do I find the largest files on the entire system?

Run sudo find / -type f -printf '%s %p\n' | sort -rn | head -20. This may take a while on large disks, but it gives the absolute largest files.

Can I delete the contents of ~/.cache?

Yes, it’s safe to clear user cache directories. Applications will recreate them as needed. It may make apps slower to start the first time after clearing.

What should I do if I accidentally delete a system file?

Immediately stop using the system if possible. Use your distribution’s package manager to reinstall the package that owns the file. For example, on Debian/Ubuntu, run sudo apt install --reinstall . If you don’t know the package, you may need to restore from backup.

Why does du show a file as 0 bytes but ls shows a large size?

That indicates a sparse file, where the file has logical length but doesn’t actually occupy disk blocks. This is common for database files or virtual machine images. The disk space is only used when data is written to the holes.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.