19

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
leetbacoon
  • 1,111
  • 2
  • 9
  • 32
  • 2
    Why are you using the `h` flag then? Read the man page. – Mat Dec 22 '19 at 07: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. – leetbacoon Dec 22 '19 at 07:54
  • 4
    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`](https://blog.superuser.com/2011/04/16/confused-by-the-output-of-du-in-linux/) – kvantour Dec 22 '19 at 08: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 – leetbacoon Dec 22 '19 at 08:17

1 Answers1

24

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
Romeo Ninov
  • 6,538
  • 1
  • 22
  • 31
  • It does not. "b" is equivalent to "--apparent-size --block-size=1" and will give you apparent size is bytes (https://man7.org/linux/man-pages/man1/du.1.html) – sergiz Oct 28 '20 at 16:20
  • 1
    @sergiz, `--block-size=1` mean in bytes. As `--block-size=1k`==`-k` – Romeo Ninov Oct 28 '20 at 17:15
  • 1
    Your comment is the correct answer since using "-b" will give you apparent size. – sergiz Oct 29 '20 at 21:11