Print in bytes with "du"
Asked Answered
B

1

28

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
Baier answered 22/12, 2019 at 7:46 Comment(4)
Why are you using the h flag then? Read the man page.Tachyphylaxis
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.Baier
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 duAlbertalberta
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 neededBaier
H
34

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
Hyphenate answered 22/12, 2019 at 9:24 Comment(4)
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)Rumanian
@sergiz, --block-size=1 mean in bytes. As --block-size=1k==-kHyphenate
Your comment is the correct answer since using "-b" will give you apparent size.Rumanian
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).Pufahl

© 2022 - 2024 — McMap. All rights reserved.