How to list the size of each file and directory and sort by descending size in Bash?

后端 未结 11 1450
遥遥无期
遥遥无期 2020-12-07 08:02

I found that there is no easy to get way the size of a directory in Bash?

I want that when I type ls -, it can list of all the sum o

11条回答
  •  执念已碎
    2020-12-07 08:57

    Command

    du -h --max-depth=0 * | sort -hr
    

    Output

    3,5M    asdf.6000.gz
    3,4M    asdf.4000.gz
    3,2M    asdf.2000.gz
    2,5M    xyz.PT.gz
    136K    xyz.6000.gz
    116K    xyz.6000p.gz
    88K test.4000.gz
    76K test.4000p.gz
    44K test.2000.gz
    8,0K    desc.common.tcl
    8,0K    wer.2000p.gz
    8,0K    wer.2000.gz
    4,0K    ttree.3
    

    Explanation

    • du displays "disk usage"
    • h is for "human readable" (both, in sort and in du)
    • max-depth=0 means du will not show sizes of subfolders (remove that if you want to show all sizes of every file in every sub-, subsub-, ..., folder)
    • r is for "reverse" (biggest file first)

    ncdu

    When I came to this question, I wanted to clean up my file system. The command line tool ncdu is way better suited to this task.

    Installation on Ubuntu:

    $ sudo apt-get install ncdu
    

    Usage:

    Just type ncdu [path] in the command line. After a few seconds for analyzing the path, you will see something like this:

    $ ncdu 1.11 ~ Use the arrow keys to navigate, press ? for help
    --- / ---------------------------------------------------------
    .  96,1 GiB [##########] /home
    .  17,7 GiB [#         ] /usr
    .   4,5 GiB [          ] /var
        1,1 GiB [          ] /lib
      732,1 MiB [          ] /opt
    . 275,6 MiB [          ] /boot
      198,0 MiB [          ] /storage
    . 153,5 MiB [          ] /run
    .  16,6 MiB [          ] /etc
       13,5 MiB [          ] /bin
       11,3 MiB [          ] /sbin
    .   8,8 MiB [          ] /tmp
    .   2,2 MiB [          ] /dev
    !  16,0 KiB [          ] /lost+found
        8,0 KiB [          ] /media
        8,0 KiB [          ] /snap
        4,0 KiB [          ] /lib64
    e   4,0 KiB [          ] /srv
    !   4,0 KiB [          ] /root
    e   4,0 KiB [          ] /mnt
    e   4,0 KiB [          ] /cdrom
    .   0,0   B [          ] /proc
    .   0,0   B [          ] /sys
    @   0,0   B [          ]  initrd.img.old
    @   0,0   B [          ]  initrd.img
    @   0,0   B [          ]  vmlinuz.old
    @   0,0   B [          ]  vmlinuz
    

    Delete the currently highlighted element with d, exit with CTRL + c

提交回复
热议问题