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

后端 未结 11 1439
遥遥无期
遥遥无期 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:46

    Another simple solution.

    $ for entry in $(ls); do du -s "$entry"; done | sort -n
    

    the result will look like

    2900    tmp
    6781    boot
    8428    bin
    24932   lib64
    34436   sbin
    90084   var
    106676  etc
    125216  lib
    3313136 usr
    4828700 opt
    

    changing "du -s" to "du -sh" will show human readable size, but we won't be able to sort in this method.

    0 讨论(0)
  • 2020-12-07 08:49

    I tend to use du in a simple way.

    du -sh */ | sort -n
    

    This provides me with an idea of what directories are consuming the most space. I can then run more precise searches later.

    0 讨论(0)
  • 2020-12-07 08:53

    ls -S sorts by size. Then, to show the size too, ls -lS gives a long (-l), sorted by size (-S) display. I usually add -h too, to make things easier to read, so, ls -lhS.

    0 讨论(0)
  • 2020-12-07 08:55
    du -s -- * | sort -n
    

    (this willnot show hidden (.dotfiles) files)

    Use du -sm for Mb units etc. I always use

    du -smc -- * | sort -n
    

    because the total line (-c) will end up at the bottom for obvious reasons :)

    PS:

    • See comments for handling dotfiles
    • I frequently use e.g. 'du -smc /home// | sort -n |tail' to get a feel of where exactly the large bits are sitting
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-07 08:58

    Simple and fast:

    find . -mindepth 1 -maxdepth 1 -type d | parallel du -s | sort -n
    

    *requires GNU Parallel.

    0 讨论(0)
提交回复
热议问题