Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I know I can use ls -lat to find out how many bytes has a file and then multiply by 8 to find out how many bits. But is this possible in only one command line?

share|improve this question
I don't think so. I also don't see any practical use for a feature like that ^^ – Martin von Wittich Jan 26 '14 at 22:49
@MartinvonWittich - internet speeds are often done in 'bits per second'... – Wilf Jan 26 '14 at 23:07
@wilf: Minus protocol overhead. – Ignacio Vazquez-Abrams Jan 26 '14 at 23:21
In my case I am using this suit test: csrc.nist.gov/groups/ST/toolkit/rng/documentation_software.html and the parameter is the number of bits I want to analyse. – Felipe Micaroni Lalli Jan 26 '14 at 23:46
up vote 13 down vote accepted

With GNU du:

du -b FILE | awk '{ print $1, "* 8" }' | bc
share|improve this answer
It worked perfectly too. Thank you. – Felipe Micaroni Lalli Jan 26 '14 at 23:51
Except it doesn't need bc - awk can do math itself: du -b FILE | awk '{print $1 * 8} – aragaer Jan 27 '14 at 1:23
See also wc -c < FILE for a portable equivalent (for non-regular files, it has the side-effect of reading them, though) – Stéphane Chazelas Jan 27 '14 at 8:45

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) ))
share|improve this answer
@StephaneChazelas Thanks for the info. Answer updated. – John1024 Jan 26 '14 at 23:30
It seems a good explanation, thank you. However in my test this command returned to me: title:7: bad math expression: operand expected at `%s ' ]2;echo 4096000 ---- the answer is 4096000, but what are the other errors? – Felipe Micaroni Lalli Jan 26 '14 at 23:49
@FelipeMicaroniLalli I cannot reproduce that error and I do not see any circumstance that would both (a) give that error message, and, at the same time, (b) give the right numerical answer. Curious. – John1024 Jan 27 '14 at 0:24

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).

share|improve this answer
Worked like a charm to me. ➜ tmp stat random-file-3 | sed -n 's/Size: ([0-9]*).*/\1 * 8/p' | bc ➜ 4096000 – Felipe Micaroni Lalli Jan 26 '14 at 23:49
Won't work in non-English locales or on non-GNU systems or for a file called ZSize: 5 for instance – Stéphane Chazelas Jan 27 '14 at 8:51
how about stat -c %s FILE to print size directly, instead of the sed? – frostschutz Jan 27 '14 at 10:51

With GNU find (predates GNU stat by decades):

find file -prune -printf '%s*8\n' | bc

Relatively portably:

ls -ld -- "$file" | awk '{print $5*8;exit}'
share|improve this answer

Single file:

wc -c yourfile | awk '{print $1*8}'

Mutiple files:

wc -c yourfile1 yourfile2 | awk '{$1*=8; print $0}'

This one also works for a single file. It is not completely bulletproof though, see Stephane's comment.

These are POSIX standard compliant commands.

share|improve this answer
Great. Awesome. ➜ tmp wc -c random-file-1 random-file-2 random-file-3 | awk '{$1*=8; print $0}' ➜ 32832 random-file-1 ➜ 49152 random-file-2 ➜ 4096000 random-file-3 ➜ 4177984 total – Felipe Micaroni Lalli Jan 26 '14 at 23:56
Note that it has side effects if the files are not regular files. The second one would display a a    b file as a b (sequences of blanks converted to a single space, trailing blanks removed). – Stéphane Chazelas Jan 27 '14 at 8:48
@StephaneChazelas with what kind of non-regular file did you try please? – Totor Jan 29 '14 at 8:32
Any type (fifos, sockets, doors, devices, directories...). You'll have issues with filenames containing newline characters as well. You may want to add a NR == 1 – Stéphane Chazelas Jan 29 '14 at 9:18

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.