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

前端 未结 15 1370
醉梦人生
醉梦人生 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 19:00

    There is no simple command available to find out the largest files/directories on a Linux/UNIX/BSD filesystem. However, combination of following three commands (using pipes) you can easily find out list of largest files:

    # du -a /var | sort -n -r | head -n 10
    

    If you want more human readable output try:

    $ cd /path/to/some/var
    $ du -hsx * | sort -rh | head -10
    

    Where,

    • Var is the directory you wan to search
    • du command -h option : display sizes in human readable format (e.g., 1K, 234M, 2G).
    • du command -s option : show only a total for each argument (summary).
    • du command -x option : skip directories on different file systems.
    • sort command -r option : reverse the result of comparisons.
    • sort command -h option : compare human readable numbers. This is GNU sort specific option only.
    • head command -10 OR -n 10 option : show the first 10 lines.

提交回复
热议问题