0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Check Disk Usage with Output

Use the Bash command df -h inside a script to check disk usage in a human-readable format, for example: #!/bin/bash df -h.
📋

Examples

Input/
OutputFilesystem Size Used Avail Use% Mounted on /dev/sda1 50G 20G 28G 42% /
Input/home
OutputFilesystem Size Used Avail Use% Mounted on /dev/sda2 100G 60G 35G 64% /home
Input/nonexistent
Outputdf: /nonexistent: No such file or directory
🧠

How to Think About It

To check disk usage, the script runs the df command which reports file system disk space usage. Using the -h option makes the output easy to read by showing sizes in KB, MB, or GB. The script can optionally accept a directory path to check usage for that specific mount point.
📐

Algorithm

1
Accept an optional directory path as input
2
Run the command <code>df -h</code> with the directory if provided, else run <code>df -h</code> for all mounts
3
Display the output to the user
💻

Code

bash
#!/bin/bash

# Check if a directory argument is given
if [ -n "$1" ]; then
  df -h "$1"
else
  df -h
fi
Output
Filesystem Size Used Avail Use% Mounted on /dev/sda1 50G 20G 28G 42% / tmpfs 1.9G 0 1.9G 0% /dev/shm /dev/sda2 100G 60G 35G 64% /home
🔍

Dry Run

Let's trace the script when called with the argument '/'

1

Check if argument exists

Argument is '/' so condition is true

2

Run df command with argument

Execute: df -h /

3

Output disk usage for '/'

Shows disk usage info for root filesystem

StepActionValue
1Check argument'/' present
2Run commanddf -h /
3OutputFilesystem Size Used Avail Use% Mounted on /dev/sda1 50G 20G 28G 42% /
💡

Why This Works

Step 1: Check for input argument

The script uses [ -n "$1" ] to see if a directory path was given to check specific disk usage.

Step 2: Run disk usage command

It runs df -h with or without the argument to get human-readable disk usage info.

Step 3: Display results

The output shows filesystem, size, used space, available space, usage percentage, and mount point.

🔄

Alternative Approaches

Using du for directory size
bash
#!/bin/bash

if [ -n "$1" ]; then
  du -sh "$1"
else
  echo "Please provide a directory to check size."
fi
This checks the size of a directory, not overall disk usage; useful for folder-specific size.
Using df with grep for specific filesystem
bash
#!/bin/bash

if [ -n "$1" ]; then
  df -h | grep "$1"
else
  df -h
fi
Filters disk usage output for lines matching the input, useful to focus on a specific mount or device.

Complexity: O(1) time, O(1) space

Time Complexity

The df command runs in constant time relative to the number of mounted filesystems, which is usually small and fixed.

Space Complexity

The script uses minimal extra memory, only storing the input argument and command output temporarily.

Which Approach is Fastest?

Using df -h directly is fastest for overall disk usage; du is slower as it scans directory contents.

ApproachTimeSpaceBest For
df -hO(1)O(1)Overall disk usage summary
du -shO(n)O(1)Size of specific directory
df -h with grepO(1)O(1)Filtered disk usage info
💡
Use df -h for easy-to-read disk usage info with sizes in GB or MB.
⚠️
Forgetting to quote variables like "$1" can cause errors with paths containing spaces.