Linux Commands to Check Disk Space
Palavras-chave:
Publicado em: 03/08/2025Linux Commands to Check Disk Space
Checking disk space on a Linux system is a fundamental task for system administrators and developers alike. This article explores common Linux commands to monitor disk space usage, understand their output, and choose the right command for your specific needs.
Fundamental Concepts / Prerequisites
Before diving into the commands, it's essential to understand a few core concepts:
- Filesystem: A hierarchical structure that organizes files on a storage device. Examples include ext4, XFS, and Btrfs.
- Mount Point: A directory in the filesystem where a storage device or partition is attached (mounted). For example, `/` is the root mount point.
- Disk Partitioning: Dividing a physical disk into logical sections called partitions. Each partition can have its own filesystem.
- Units: Disk space is typically measured in bytes, kilobytes (KB), megabytes (MB), gigabytes (GB), and terabytes (TB).
Core Implementation/Solution: Using `df` Command
The `df` (disk filesystem) command is the most commonly used tool to display disk space usage on Linux. Here's an example of using the `df -h` command:
# df -h
Filesystem Size Used Avail Use% Mounted on
udev 7.8G 0 7.8G 0% /dev
tmpfs 1.6G 1.2M 1.6G 1% /run
/dev/sda2 228G 151G 66G 70% /
tmpfs 7.8G 79M 7.7G 2% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
/dev/sda1 511M 5.3M 506M 2% /boot/efi
tmpfs 1.6G 116K 1.6G 1% /run/user/1000
Code Explanation
The `df -h` command provides a human-readable output. Let's break down each column:
- Filesystem: The name of the filesystem (e.g., `/dev/sda2`, `udev`). `/dev/sda2` usually signifies a partition on the first SATA drive. `udev` and `tmpfs` are virtual filesystems residing in memory.
- Size: The total size of the filesystem.
- Used: The amount of disk space currently used.
- Avail: The amount of disk space available.
- Use%: The percentage of disk space used.
- Mounted on: The mount point where the filesystem is attached (e.g., `/`, `/boot/efi`).
The `-h` option makes the output human-readable by displaying sizes in KB, MB, GB, etc. Without `-h`, the sizes are displayed in 1K blocks.
Complexity Analysis
The `df` command's complexity is largely dependent on the number of mounted filesystems. Scanning each filesystem to determine its size and usage involves traversing metadata structures.
- Time Complexity: Generally, O(n), where n is the number of mounted filesystems. The command needs to iterate through each filesystem to gather its statistics.
- Space Complexity: O(1). The command primarily stores a fixed amount of information for each filesystem during processing, so space usage is constant with respect to the size of the filesystems.
Alternative Approaches: Using `du` command
Another useful command is `du` (disk usage). While `df` shows the overall disk space usage for entire filesystems, `du` shows the space used by specific files or directories. A common usage is `du -sh *` which shows the summarized human readable size of all files and directories in the current directory.
# du -sh *
4.0K anaconda3
4.0K data
2.4G deeplearning.ai
8.0K Desktop
8.0K Documents
4.0K Downloads
68K examples.desktop
4.0K Music
4.0K Pictures
4.0K Public
4.0K Templates
4.0K Videos
Trade-offs: `du` can be slower than `df` because it needs to traverse the filesystem tree to calculate the size of each file or directory. However, it provides more granular information about where disk space is being used within a filesystem.
Conclusion
Understanding disk space usage is crucial for managing Linux systems effectively. The `df` command provides a quick overview of filesystem usage, while the `du` command allows you to drill down and identify specific files or directories consuming the most space. Choosing the right command depends on the level of detail required for your monitoring or troubleshooting task.