To check disk space usage on Linux or macOS, the most common and direct command is df -h for overall file system usage and du -sh for the size of specific directories. The df (disk free) command provides a high-level overview of all mounted partitions, while the du (disk usage) command allows you to drill down into specific folders to find exactly which files are consuming your storage.

What You'll Need

  • A computer running a Unix-like operating system (Linux, macOS, or BSD).
  • Access to the Terminal application (or a command-line interface via SSH).
  • User permissions (standard user for basic checks, sudo or root access for system-protected directories).

Step-by-Step

  1. Open your Terminal: Launch your command-line interface. On macOS, you can find this in Applications > Utilities > Terminal. On most Linux distributions, you can press Ctrl+Alt+T or find "Terminal" in your applications menu.
  2. Check overall system storage: Type df -h and press Enter. The -h flag stands for "human-readable," which converts the output from raw blocks into gigabytes (G) and megabytes (M). Look at the "Used" and "Avail" columns to see how much space is left on your main hard drive (usually listed as / or /dev/sda).
  3. Identify the largest directories: If you see that your disk is nearly full, use the du command to find the culprit. Type sudo du -sh /* and press Enter. The -s flag provides a summary for each item, and the -h makes it readable. The /* tells the system to check every folder in the root directory. Using sudo ensures the command can read system folders that a standard user cannot access.
  4. Drill down into a specific folder: Once you identify a large folder (for example, /var), move into that folder to find the specific sub-folder causing the issue. Type sudo du -sh /var/*. Repeat this process of narrowing down the path until you find the specific files or logs taking up the most space.
  5. Sort results by size: To avoid scrolling through a long list, you can pipe the output to the sort command. Type sudo du -h /home | sort -rh | head -n 10. This command calculates the size of everything in the home directory, sorts them in reverse human-numeric order (largest first), and displays only the top 10 results.
  6. Verify with a visual tool (Optional): If the command line is overwhelming, you can use ncdu (NCurses Disk Usage). If it is not installed, you can usually get it via sudo apt install ncdu (on Debian/Ubuntu) or brew install ncdu (on macOS). Run it by typing ncdu, and it will provide an interactive, navigable list of your disk usage.

Common Mistakes

One of the most frequent mistakes is running df without the -h flag. Without it, the output is displayed in 1K blocks, which makes it very difficult for humans to quickly calculate exactly how many gigabytes are remaining. Always include the human-readable flag for quick assessments.

Another common error is forgetting to use sudo when running du on system directories. If you run du -sh /root or du -sh /var/log as a standard user, the terminal will print dozens of "Permission denied" errors. While this doesn't harm the system, it clutters the output and results in an inaccurate total size because the command skipped the folders it couldn't access.

Users also often confuse df and du. Remember: df is for the Disk (the whole partition/drive), and du is for the Usage (the individual files and folders). If you want to know if your hard drive is full, use df. If you want to know why it is full, use du.

Tips

To find only the largest files on your system regardless of which folder they are in, you can use the find command. For example, sudo find / -type f -size +100M will list every single file on your entire system that is larger than 100 megabytes. This is often faster than drilling down through directories with du.

When dealing with log files in /var/log, be careful about deleting them entirely. Some applications expect the log file to exist to function correctly. Instead of deleting the file, you can "truncate" it (empty the contents while keeping the file) by using the command sudo truncate -s 0 /var/log/example.log. This frees up the space immediately without breaking the application's logging mechanism.

If you are managing a server and suspect that a deleted file is still occupying space, run sudo lsof +L1. In Linux, if a process is still writing to a file that you have deleted via rm, the disk space is not actually freed until the process is stopped or restarted. This command shows you those "deleted but open" files.

Troubleshooting

The command returns "Permission Denied": This happens when you try to access system-level directories (like /root or /etc) without administrative privileges. Prefix your command with sudo and enter your password when prompted.

The output is too long to read: When running du on a directory with thousands of files, the text will fly past your screen. Use a "pipe" to send the output to less, which allows you to scroll. For example: sudo du -h / | less. Press q to exit the less view.

The disk shows as full, but du doesn't show large files: This is often caused by "hidden" files (files starting with a dot) or the "deleted but open" file issue mentioned in the Tips section. Ensure you are using du -ah (the -a flag includes files, not just directories) to see everything. If the discrepancy persists, restart the services that handle large logs or databases, or reboot the system to clear orphaned file handles.

Command not found: If ncdu returns "command not found," it is because it is a third-party utility and not installed by default. Use your package manager (apt, yum, brew) to install it, or stick to the built-in df and du commands.

Frequently Asked Questions

What is the difference between df and du?

df (disk free) reports the amount of available and used disk space on the entire file system. du (disk usage) estimates the space used by specific files or directories.

How do I see disk usage in GB instead of bytes?

Use the -h flag with either command (e.g., df -h or du -h). This converts the output into human-readable formats like K, M, and G.

Can I use these commands on Windows?

No, these are Unix/Linux commands. For Windows, you can use the "Disk Management" GUI, the "Storage Settings" menu in Windows 10/11, or third-party tools like WinDirStat or WizTree.

How do I find the top 5 largest files in a folder?

Use the command du -ah /path/to/folder | sort -rh | head -n 5. This lists all files and folders, sorts them by size in descending order, and shows the first five.

Why does df show more used space than du?

This usually happens because df counts the actual blocks used on the disk, while du counts the size of the files. Discrepancies can be caused by filesystem overhead, reserved blocks for the root user, or files that were deleted but are still being held open by a running process.

Is it safe to delete files found using these commands?

Only if you know what the file is. Deleting files in /bin, /etc, or /lib can break your operating system. Focus on your own home directory (/home/username) or temporary folders (/tmp) when cleaning up space.