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

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

    I think I might have figured out what you want to do. This will give a sorted list of all the files and all the directories, sorted by file size and size of the content in the directories.

    (find . -depth 1 -type f -exec ls -s {} \;; find . -depth 1 -type d -exec du -s {} \;) | sort -n
    
    0 讨论(0)
  • 2020-12-07 09:02

    Apparently --max-depth option is not in Mac OS X's version of the du command. You can use the following instead.

    du -h -d 1 | sort -n

    0 讨论(0)
  • 2020-12-07 09:03

    [enhanced version]
    This is going to be much faster and precise than the initial version below and will output the sum of all the file size of current directory:

    echo `find . -type f -exec stat -c %s {} \; | tr '\n' '+' | sed 's/+$//g'` | bc
    

    the stat -c %s command on a file will return its size in bytes. The tr command here is used to overcome xargs command limitations (apparently piping to xargs is splitting results on more lines, breaking the logic of my command). Hence tr is taking care of replacing line feed with + (plus) sign. sed has the only goal to remove the last + sign from the resulting string to avoid complains from the final bc (basic calculator) command that, as usual, does the math.

    Performances: I tested it on several directories and over ~150.000 files top (the current number of files of my fedora 15 box) having what I believe it is an amazing result:

    # time echo `find / -type f -exec stat -c %s {} \; | tr '\n' '+' | sed 's/+$//g'` | bc
    12671767700
    
    real    2m19.164s
    user    0m2.039s
    sys 0m14.850s
    

    Just in case you want to make a comparison with the du -sb / command, it will output an estimated disk usage in bytes (-b option)

    # du -sb /
    12684646920 /
    

    As I was expecting it is a little larger than my command calculation because the du utility returns allocated space of each file and not the actual consumed space.

    [initial version]
    You cannot use du command if you need to know the exact sum size of your folder because (as per man page citation) du estimates file space usage. Hence it will lead you to a wrong result, an approximation (maybe close to the sum size but most likely greater than the actual size you are looking for).

    I think there might be different ways to answer your question but this is mine:

    ls -l $(find . -type f | xargs) | cut -d" " -f5 | xargs | sed 's/\ /+/g'| bc
    

    It finds all files under . directory (change . with whatever directory you like), also hidden files are included and (using xargs) outputs their names in a single line, then produces a detailed list using ls -l. This (sometimes) huge output is piped towards cut command and only the fifth field (-f5), which is the file size in bytes is taken and again piped against xargs which produces again a single line of sizes separated by blanks. Now take place a sed magic which replaces each blank space with a plus (+) sign and finally bc (basic calculator) does the math.

    It might need additional tuning and you may have ls command complaining about arguments list too long.

    0 讨论(0)
  • 2020-12-07 09:03

    you can use the below to list files by size du -h | sort -hr | more or du -h --max-depth=0 * | sort -hr | more

    0 讨论(0)
  • 2020-12-07 09:05

    Simply navigate to directory and run following command:

    du -a --max-depth=1 | sort -n
    

    OR add -h for human readable sizes and -r to print bigger directories/files first.

    du -a -h --max-depth=1 | sort -hr
    
    0 讨论(0)
提交回复
热议问题