Use the df -h command to instantly see how much space is used and available on your Unix system. This command reads directly from the operating system kernel, so it provides the most accurate, up-to-the-minute view of your storage capacity without needing to guess based on file sizes. It works on almost any Unix-like system, including Linux, macOS, and FreeBSD, provided you have terminal access.
What You'll Need
- A Unix or Unix-like operating system (Linux, macOS, or similar).
- A terminal or command-line interface (CLI) window.
- Basic knowledge of navigating directories (optional, but helpful).
Step-by-Step
- Open the Terminal: Locate your application launcher or menu and search for "Terminal" or "Command Prompt." Launch the application to open a black window where you will type commands. On macOS, this is often found in the Utilities folder within the Applications folder.
- Gain Root Privileges (If Necessary): Some systems restrict access to disk information to the "root" user or "administrator." If you see an error message stating "Permission denied" after typing your command, you need to run the command with elevated privileges. Type
sudofollowed by a space before the command. For example, typesudo df -hand press Enter. You will be prompted to enter your administrator password. Note that the cursor will not move or show asterisks while you type the password for security reasons. - Execute the Basic Command: Type
df -hand press Enter. Thedfstands for "disk free." The-hflag stands for "human-readable." Without the-hflag, the output will show file system block sizes in bytes, which are difficult for humans to interpret quickly. The-hflag converts these numbers into Gigabytes (G), Megabytes (M), and Kilobytes (K) automatically. - Interpret the Output: The command will print a table of results. The most important column to look at is the one labeled "Use%" (or sometimes "Use%"). This column shows the percentage of the file system that is currently full. A number close to 100% indicates that you are running out of space and may need to clean up files immediately.
- Identify the Problematic File System: Look at the "Mounted on" column to see where the problem is located. For example, if you see
/homehas a high percentage, the issue is in your user home directory. If/(the root file system) is full, the issue is likely with system files or large logs. - Check Specific File Systems (Optional): If you have multiple drives or partitions, you might want to check only the ones you care about. You can use the
-xflag to exclude file systems of a certain type. For example, to check only local hard drives and exclude network mounts, you can rundf -x tmpfs -x devtmpfs. - Get a List of Largest Directories (Advanced): The
dfcommand shows you *where* the space is used, but not *what* is using it. To find the largest directories, you need to use a combination of commands. First, navigate to the root directory by typingcd /. Then, run the following command:du -h --max-depth=1 | sort -hr. This command will list all top-level directories, showing their sizes in human-readable format, sorted from largest to smallest. - Verify Disk Health (Optional): While
dftells you about capacity, it doesn't tell you if the disk is physically failing. For a more comprehensive check, you can use thesmartctlcommand, which is part of the SMART (Self-Monitoring, Analysis and Reporting Technology) suite. This requires thesmartmontoolspackage to be installed on your system.
Common Mistakes
One of the most common mistakes is relying solely on file explorer windows to gauge disk space. While a graphical file manager might show a folder as "full," it often counts files that are currently open by another process as unused space. The df command, however, reads directly from the kernel and accounts for these locked files, giving you the truest picture of available space.
Another frequent error is using the du command without the -h flag or sorting flags. Running du on a large system can generate a massive amount of text output that is impossible to read. The sort -hr flag is crucial for making this data useful, as it organizes the output so you can quickly spot the biggest culprits without scrolling endlessly.
Tips
Always use the -h flag with df and du commands. Working with raw bytes is inefficient and prone to misinterpretation. Human-readable formats standardize the output, making it easier to communicate with team members or to spot anomalies at a glance.
Be aware that file systems have a non-zero overhead. A file system might report that it has 10 GB of free space, but you might not be able to write a 10 GB file. This is because file systems require space for metadata, such as file names, permissions, and allocation tables. The amount of overhead varies depending on the file system type (e.g., ext4, NTFS, APFS), but it is always something to keep in mind when planning storage.
Troubleshooting
Output is confusing or empty. If the command returns no output, ensure you are running it in the correct shell. On some systems, the default shell might not support the df command. Try typing bash or sh to switch to a standard shell, then run the command again.
"Permission denied" error. If you see this error, it means you do not have the necessary permissions to read the disk information. This can happen if you are trying to check a file system mounted by another user or system process. Using the sudo command, as described in Step 2, is the standard way to resolve this.
Values don't match file sizes. If the space reported by df seems significantly lower than the total size of your files, it could be due to a non-sequential write. If you have written files to the disk, deleted them, and then written new files, the old data might be scattered across the disk. The file system might not be able to reclaim that space immediately, even though the files are gone from your view.
Frequently Asked Questions
What does the "Avail" column mean?
The Avail column stands for available space. It represents the amount of space that is currently free and can be used to store new files. This number is calculated by subtracting the amount of used space from the total capacity of the file system.
Is the df command the same on Windows?
No, the df command is specific to Unix-like operating systems. On Windows, the equivalent command is wmic logicaldisk get size,freespace,caption, which performs a similar function but uses a different syntax and output format.
Why is the root file system (/) full?
The root file system contains the operating system files, system logs, and temporary files. If it is full, it usually indicates that a system process has generated a massive log file, or that a system update failed and left behind temporary files. It is important to address this immediately, as a full root file system can cause the system to become unstable or unresponsive.
Can I use this command on a remote server?
Yes, you can use the df command on a remote server as long as you have SSH access and the necessary permissions. You would typically log in to the server using an SSH client and then run the command from the remote terminal.
What is the difference between df and du?
The main difference is that df reports on the total capacity of the file system as a whole, while du reports on the size of specific directories or files. df tells you *how much space is available*, while du tells you *what is taking up that space*.