How does one find out how many bits a file has in one command?

181

Solution 1

With GNU du:

du -b FILE | awk '{ print $1 * 8 }'

Solution 2

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

Solution 3

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

Solution 4

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}'

Solution 5

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:
181

Related videos on Youtube

Sekhar
Author by

Sekhar

Updated on September 18, 2022

Comments

  • Sekhar
    Sekhar over 1 year

    I was able to export the blog from a wordpress.com site to a self-hosted one.

    However, I am having issues installing the 'CORE widgets' (http://en.support.wordpress.com/topic/widgets-sidebars/). Are these locked down to wordpress.com only?

    Thanks!

    • Jure C.
      Jure C. almost 12 years
      which one specific are you looking for? Most of them are open.
    • Sekhar
      Sekhar almost 12 years
      I am looking at Twitter, Box.net, Authors, Flickr, SocialVibe Gravatar among few. I want to restore as much functionality as possible and want to use the same plugins - (using similar plugins would be my next preference - ).. Thanks
    • Babak Naffas
      Babak Naffas almost 12 years
      Are you sure these widgets weren't installed as part of your theme?
    • Sekhar
      Sekhar almost 12 years
      ya.. I am using the twenty10 theme.. these widgets were not there..
    • Martin von Wittich
      Martin von Wittich over 10 years
      I don't think so. I also don't see any practical use for a feature like that ^^
    • TheBeginner
      TheBeginner over 10 years
      @MartinvonWittich - internet speeds are often done in 'bits per second'...
    • Ignacio Vazquez-Abrams
      Ignacio Vazquez-Abrams over 10 years
      @wilf: Minus protocol overhead.
    • Felipe
      Felipe over 10 years
      In my case I am using this suit test: csrc.nist.gov/groups/ST/toolkit/rng/documentation_software.h‌​tml and the parameter is the number of bits I want to analyse.
  • John1024
    John1024 over 10 years
    @StephaneChazelas Thanks for the info. Answer updated.
  • Felipe
    Felipe over 10 years
    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
    Felipe over 10 years
    Worked like a charm to me. ➜ tmp stat random-file-3 | sed -n 's/Size: ([0-9]*).*/\1 * 8/p' | bc ➜ 4096000
  • Felipe
    Felipe over 10 years
    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
  • John1024
    John1024 over 10 years
    @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.
  • aragaer
    aragaer over 10 years
    Except it doesn't need bc - awk can do math itself: du -b FILE | awk '{print $1 * 8}
  • Stéphane Chazelas
    Stéphane Chazelas over 10 years
    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
    Stéphane Chazelas over 10 years
    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
    Stéphane Chazelas over 10 years
    Won't work in non-English locales or on non-GNU systems or for a file called ZSize: 5 for instance
  • frostschutz
    frostschutz over 10 years
    how about stat -c %s FILE to print size directly, instead of the sed?
  • Totor
    Totor over 10 years
    @StephaneChazelas with what kind of non-regular file did you try please?
  • Stéphane Chazelas
    Stéphane Chazelas over 10 years
    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