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?
-
5I don't think so. I also don't see any practical use for a feature like that ^^– Martin von WittichJan 26, 2014 at 22:49
-
1@MartinvonWittich - internet speeds are often done in 'bits per second'...– WilfJan 26, 2014 at 23:07
-
4@wilf: Minus protocol overhead.– Ignacio Vazquez-AbramsJan 26, 2014 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.– FelipeJan 26, 2014 at 23:46
5 Answers
With GNU du
:
du -b FILE | awk '{ print $1 * 8 }'
-
10Except it doesn't need
bc
-awk
can do math itself:du -b FILE | awk '{print $1 * 8}
– aragaerJan 27, 2014 at 1:23 -
1See also
wc -c < FILE
for a portable equivalent (for non-regular files, it has the side-effect of reading them, though) Jan 27, 2014 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) ))
-
-
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?– FelipeJan 26, 2014 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.– John1024Jan 27, 2014 at 0:24
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}'
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).
-
Worked like a charm to me. ➜ tmp stat random-file-3 | sed -n 's/Size: ([0-9]*).*/\1 * 8/p' | bc ➜ 4096000– FelipeJan 26, 2014 at 23:49
-
Won't work in non-English locales or on non-GNU systems or for a file called
ZSize: 5
for instance Jan 27, 2014 at 8:51 -
1how about
stat -c %s FILE
to print size directly, instead of thesed
? Jan 27, 2014 at 10:51
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.
-
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– FelipeJan 26, 2014 at 23:56
-
1Note 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). Jan 27, 2014 at 8:48 -
@StephaneChazelas with what kind of non-regular file did you try please?– TotorJan 29, 2014 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
Jan 29, 2014 at 9:18