How does one find out how many bits a file has in one command?
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.
Related videos on Youtube
Sekhar
Updated on September 18, 2022Comments
-
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. almost 12 yearswhich one specific are you looking for? Most of them are open.
-
Sekhar almost 12 yearsI 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 almost 12 yearsAre you sure these widgets weren't installed as part of your theme?
-
Sekhar almost 12 yearsya.. I am using the twenty10 theme.. these widgets were not there..
-
Martin von Wittich over 10 yearsI don't think so. I also don't see any practical use for a feature like that ^^
-
TheBeginner over 10 years@MartinvonWittich - internet speeds are often done in 'bits per second'...
-
Ignacio Vazquez-Abrams over 10 years@wilf: Minus protocol overhead.
-
Felipe over 10 yearsIn 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.
-
-
John1024 over 10 years@StephaneChazelas Thanks for the info. Answer updated.
-
Felipe over 10 yearsIt 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 over 10 yearsWorked like a charm to me. ➜ tmp stat random-file-3 | sed -n 's/Size: ([0-9]*).*/\1 * 8/p' | bc ➜ 4096000
-
Felipe over 10 yearsGreat. 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 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 over 10 yearsExcept it doesn't need
bc
-awk
can do math itself:du -b FILE | awk '{print $1 * 8}
-
Stéphane Chazelas over 10 yearsSee also
wc -c < FILE
for a portable equivalent (for non-regular files, it has the side-effect of reading them, though) -
Stéphane Chazelas over 10 yearsNote that it has side effects if the files are not regular files. The second one would display a
a b
file asa b
(sequences of blanks converted to a single space, trailing blanks removed). -
Stéphane Chazelas over 10 yearsWon't work in non-English locales or on non-GNU systems or for a file called
ZSize: 5
for instance -
frostschutz over 10 yearshow about
stat -c %s FILE
to print size directly, instead of thesed
? -
Totor over 10 years@StephaneChazelas with what kind of non-regular file did you try please?
-
Stéphane Chazelas over 10 yearsAny 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