Check free disk space for current partition in Bash

Learn check free disk space for current partition in bash with practical examples, diagrams, and best practices. Covers linux, bash development techniques with visual explanations.

How to Check Free Disk Space for the Current Partition in Bash

A terminal window displaying disk space usage with various commands.

Learn various Bash commands to accurately determine available disk space on your current Linux partition, understand their output, and interpret key metrics.

Understanding disk space usage is crucial for system administration and preventing critical failures. In Linux, Bash provides several powerful commands to inspect disk utilization. This article will guide you through the most common and effective methods to check the free disk space specifically for your current working directory's partition, explaining the output and best practices.

Using df for Partition-Level Disk Usage

The df (disk free) command is the primary utility for reporting file system disk space usage. By default, df shows information for all mounted file systems. To focus on the partition where your current directory resides, you can use a specific option or pipe its output. The most common and human-readable way to use df is with the -h flag, which displays sizes in human-readable format (e.g., G for gigabytes, M for megabytes).

df -h .

Check disk space for the current partition in human-readable format.

The . (dot) argument tells df to report on the filesystem containing the current directory. The output typically includes columns like Filesystem, Size, Used, Avail, Use%, and Mounted on. The Avail column is what you're looking for to determine free space.

flowchart TD
    A[Start]
    A --> B{Current Directory Location?}
    B --> C["Identify Mounted Filesystem (e.g., /dev/sda1)"]
    C --> D["Execute 'df -h .' command"]
    D --> E["Parse Output: Filesystem, Size, Used, Avail, Use%, Mounted on"]
    E --> F["Focus on 'Avail' column for free space"]
    F --> G[End]

Workflow for checking current partition disk space using df.

Understanding du for Directory-Level Usage (and why it's different)

While df reports on filesystem usage, du (disk usage) reports on directory usage. It's important to understand the distinction. du calculates the space consumed by files and subdirectories within a specified path. It does not directly tell you the free space on the partition, but rather how much space a particular directory (like your current one) is occupying. This can be useful for identifying large directories that are consuming space on your current partition.

du -sh .
# Or to see top 10 largest directories in current path
du -sh * | sort -rh | head -10

Check disk usage for the current directory and its contents.

Advanced df Usage and Filtering

For more specific scenarios, you might want to filter df's output or extract just the free space value. You can combine df with tools like awk or grep for advanced parsing.

# Get only the 'Avail' space for the current partition
df -h . | awk 'NR==2 {print $4}'

# Get the percentage used for the current partition
df -h . | awk 'NR==2 {print $5}'

Extracting specific values from df output using awk.

In the awk commands above, NR==2 selects the second line of the df -h . output (which contains the actual data for the current filesystem), and $4 or $5 selects the fourth (Avail) or fifth (Use%) column, respectively.