问题
how do I display the average file size (rounded down). Use only: cat, echo, ls, wc, here is what I was able to do so far: echo "$(cat * | wc -w; ls -l | wc -l)" I have both of the numbers, I just can't divide them, any help would be appreciated and thanks in advance
回答1:
Is it allowed to use the shell for the division?
$ ls -l
total 20
-rw-r--r-- 1 james james 968 Dec 29 2016 bar
-rw-r--r-- 1 james james 900 Dec 29 2016 bar.asc
-rw-r--r-- 1 james james 39 Dec 29 2016 compr.txt
-rw-r--r-- 1 james james 1056 Dec 28 2016 foo
-rw-r--r-- 1 james james 896 Dec 29 2016 foo.asc
$ cat * | wc -c
3859
$ ls | wc -l
5
$ echo $(( $(cat * | wc -c) / $(ls | wc -l) )) # solution part
771
$ echo 5*771 | bc
3855
回答2:
You can do
n=( * ); s=( $(ls -sk) ); echo $(( ${s[1]} / ${#n[@]} ))
use an array to count the number of files in the directory and ls to get the total size in Kbytes, then print the result of the quotient.
来源:https://stackoverflow.com/questions/52565341/unix-linux-display-average-file-size-with-restriction