42

This works:

du -cshm .

But this fails:

du -cshg .

How can I see it in unit of GB?

4 Answers 4

62

GNU du has the --block-size option:

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

As sajb noted, omitting the block size argument will automatically scale the output (and display the unit). Using any block size argument displays the number but omits the unit.

3
  • At (GNU coreutils) 8.25 -h didn't work, while block-size=1G did! May 28, 2019 at 10:46
  • @FlorianStraub: I'm not sure what you mean about -h not working. It is a supported option which causes the output to be in "human readable" (i.e. with unit suffixes and scaled). But it is true that it is ignored when --block-size is used. This is noted in my answer. May 28, 2019 at 11:54
  • my bad: I meant "-g didn't work" May 28, 2019 at 12:16
14

For convenience, here's reference for macOS:

  • -h "Human-readable" output. Use unit suffixes: Byte, Kilobyte, Megabyte, Gigabyte, Terabyte and Petabyte.
  • -k Display block counts in 1024-byte (1-Kbyte) blocks.
  • -m Display block counts in 1,048,576-byte (1-Mbyte) blocks.
  • -g Display block counts in 1,073,741,824-byte (1-Gbyte) blocks.

Here is how the various options work given a 1,234,567 KB file:

$ mkfile -n 1234567k file.txt

$ du file.txt
2469136 file.txt

$ du -k file.txt
1234568 file.txt

$ du -m file.txt
1206    file.txt

$ du -g file.txt
2   file.txt

$ du -h file.txt
1.2G    file.txt

Also worth noting, you can configure implicit behaviour though the BLOCKSIZE environment variable:

BLOCKSIZE If the environment variable BLOCKSIZE is set, and the -k option is not specified, the block counts will be displayed in units of that size block. If BLOCKSIZE is not set, and the -k option is not specified, the block counts will be displayed in 512-byte blocks.

2
  • Am I the only one, that thinks, that 1,048,576 Byte should be labelled as "1 MiByte" or "1 Mibibyte", not "1 MByte"? "1 MByte" is 1,000,000 Byte. HD and SSD vendors as well as network equipment suppliers follow the metric system. RAM is still different, but du is about disk space and/or network transfer time, not about memory footprint.
    – Kai Petzke
    2 days ago
  • @KaiPetzke I've lost the willpower to enforce that distinction long, long agoooo. 99% of the time I'm looking at these kinds of figures, I just care about the order of magnitude, and relative size lol
    – Alexander
    2 days ago
1

Use du -B1073741824 but beware, it gives the result in integer-units only, and won't be meaningful with -h

0

In addition to the previous answers, it also seems to differ between different coreutils versions (or locale?), since on my host I get:

$ du -csh .
32G     .
32G     total
$ du --version | head -1
du (GNU coreutils) 7.4

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .