How to find the largest file in a directory and its subdirectories?

前端 未结 15 1365
醉梦人生
醉梦人生 2020-11-28 18:29

We\'re just starting a UNIX class and are learning a variety of Bash commands. Our assignment involves performing various commands on a directory that has a number of folder

15条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 18:42

    Try the following one-liner (display top-20 biggest files):

    ls -1Rs | sed -e "s/^ *//" | grep "^[0-9]" | sort -nr | head -n20
    

    or (human readable sizes):

    ls -1Rhs | sed -e "s/^ *//" | grep "^[0-9]" | sort -hr | head -n20
    

    Works fine under Linux/BSD/OSX in comparison to other answers, as find's -printf option doesn't exist on OSX/BSD and stat has different parameters depending on OS. However the second command to work on OSX/BSD properly (as sort doesn't have -h), install sort from coreutils or remove -h from ls and use sort -nr instead.

    So these aliases are useful to have in your rc files:

    alias big='du -ah . | sort -rh | head -20'
    alias big-files='ls -1Rhs | sed -e "s/^ *//" | grep "^[0-9]" | sort -hr | head -n20'
    

提交回复
热议问题