Getting file sizes with wc or du

wc with the -c option is a useful and portable way to get the size of a file (but not a directory) in bytes. The file does not have to be text for this to work:

$ wc -c /bin/bash
1099016

The output for the preceding command shows us that the /bin/bash binary is just over 1 MB in size.

The du program can show this information for a file or a directory, but its only portable unit size as specified in POSIX is 1 kibibyte (1024 bytes), with the -k option:

$ du -k /bin/bash
1076    /bin/bash
$ du -k /bin
13796   /bin

GNU du has a -b option, which does allow byte counts, but this is not portable, meaning you cannot rely on it working on all Unix-like systems.

Get Bash Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.