How does one find out how many bits a file has in one command?
With GNU du
:
du -b FILE | awk '{ print $1 * 8 }'
A shell + GNU coreutils solution:
echo $(( 8 * $(stat -c%s FILE) ))
The -c%s
option to stat
returns just the file size in bytes, eliminating any need for additional text processing. This syntax is supported by GNU coreutils and therefore should work under most linux distributions.
As an exception on linux, if one is running zsh with the optional zsh/stat module, then one needs to specify a path to get the GNU coreutils:
echo $(( 8 * $(command stat -c%s FILE) ))
It is possible in one line, because you can put several commands on one line, e.g. connected by pipes or command substitutions:
echo $(stat -c %s FILE) '* 8' | bc
(Thanks @frostschutz for the update).