31

I am using du -sh to see the size of directories. If I check a 1KiB directory, I will see:

1.0K . 

However, I want the output in bytes, and only the bytecount.

For example:

$ du -sh . 1024 
4
  • 4
    Why are you using the h flag then? Read the man page. Commented Dec 22, 2019 at 7:48
  • Mat, if I omit h I do not get a byte count. For example, an 8K directory gives me 16 without -h. 8 kilobytes is not 16 bytes. Commented Dec 22, 2019 at 7:54
  • 5
    The command du stands for disk usage, i.e. how much space does this file/directory use on disk (sectors, etc). It does not stand for how many bytes are stored in the file, but more how many bytes are needed to store a file of N Bytes of content. The pure byte count is done with du -b. See I'm confused by the output of du Commented Dec 22, 2019 at 8:10
  • Thank you kvantour. This only works on GNU du, but that is okay. I also piped through grep -o '^[0-9]\+' to get the true output I needed Commented Dec 22, 2019 at 8:17

1 Answer 1

39

To get size in bytes you should use command on this way:

du -sb 

(this b mean bytes)

for the du which do not work properly with -b you can use

du -s --block-size=1 
Sign up to request clarification or add additional context in comments.

4 Comments

It does not. "b" is equivalent to "--apparent-size --block-size=1" and will give you apparent size is bytes (man7.org/linux/man-pages/man1/du.1.html)
@sergiz, --block-size=1 mean in bytes. As --block-size=1k==-k
Your comment is the correct answer since using "-b" will give you apparent size.
On macOS, these options are not supported. -B1 is equivalent to --block-size=1, but rounds up to at least 512. There is -A for apparent size, which turns off the rouding according to man page, but issuing du -A -B1 dir produces 512 blocks anyways. I have to use du -h -A -B1 to get file sizes in giga/mega/kilobytes, which shows correct value. I don't know if it is a bug in macOS du or in other BSD unices. (Alternatively, install gnu coreutils on macOS via macports).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.