Unix/Linux display average file size with restriction

≡放荡痞女 提交于 2019-12-13 08:48:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!