How to Display Disk Usage in GB Instead of MB Using Linux du Command


2 views

When working with large directories in Linux, the default du command output in megabytes (MB) often becomes less readable. While du -h provides human-readable output, it automatically scales between KB, MB, and GB, which isn't always precise enough for technical analysis.

The -m flag in du -cshm successfully displays output in megabytes because:

du -cshm .  # Works: shows size in megabytes
du -cshg .  # Fails: invalid option

The -g flag isn't a standard option in most du implementations, despite what some might expect from the pattern.

Here are three reliable methods to display disk usage in gigabytes:

1. Using Block Size Adjustment

du -csh --block-size=1G .

Or more precisely:

du -csh --block-size=1G --apparent-size .

2. Combining with awk for Custom Formatting

du -csm . | awk '{printf "%.2f GB\\n", $1/1024}'

3. Using numfmt for Unit Conversion

du -cbs . | numfmt --to=si --suffix=B | grep total

For scripting scenarios where precise GB output is needed:

# For a specific directory
DIRSIZE=$(du -sb /path/to/dir | cut -f1)
echo "Directory size: $(echo "$DIRSIZE/1024/1024/1024" | bc) GB"

# Sorting largest directories in GB
du -sm */ | sort -nr | awk '{printf "%5.1f GB %s\\n", $1/1024, $2}'

While du -h provides automatic scaling, it's not ideal for:

  • Scripting where consistent units are required
  • Precise comparisons between directories
  • Cases where GB-specific threshold monitoring is needed

Some newer du implementations (like GNU coreutils 8.25+) support:

du -csh --si .  # Uses power-of-10 (1GB=1000MB) instead of power-of-2

Check your version with:

du --version | head -1

The du command in Linux by default shows disk usage in kilobytes (KB) unless specified otherwise. While the -m flag displays output in megabytes (MB), there isn't a direct -g flag for gigabytes as many users would expect.

The most reliable approach is to use the -h (human-readable) flag which automatically scales the output to the most appropriate unit:

du -h --max-depth=1 /path/to/directory

To specifically get output in gigabytes, you can use the --block-size=GB parameter:

du --block-size=GB -csh .

For more precise control, combine du with awk to convert MB to GB:

du -sm * | awk '{printf "%.2fGB\t%s\n", $1/1024, $2}'

Here's how you might implement this in a shell script:

#!/bin/bash
TARGET_DIR=${1:-.}
echo "Disk usage for $TARGET_DIR (GB):"
du --block-size=GB -csh $TARGET_DIR | grep -v "total"

Be aware that using GB blocks may show 0GB for smaller directories (under 1GB). For more granular output, consider using:

du -b | awk '{printf "%.2fGB\t%s\n", $1/1073741824, $2}'