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

前端 未结 15 1304
醉梦人生
醉梦人生 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'
    
    0 讨论(0)
  • 2020-11-28 18:43

    du -aS /PATH/TO/folder | sort -rn | head -2 | tail -1

    or

    du -aS /PATH/TO/folder | sort -rn | awk 'NR==2'

    0 讨论(0)
  • 2020-11-28 18:46

    Linux Solution: For example, you want to see all files/folder list of your home (/) directory according to file/folder size (Descending order).

    sudo du -xm / | sort -rn | more

    0 讨论(0)
  • 2020-11-28 18:48
    ls -alR|awk '{ if ($5 > max) {max=$5;ff=$9}} END {print max "\t" ff;}'
    
    0 讨论(0)
  • 2020-11-28 18:51

    This script simplifies finding largest files for further action. I keep it in my ~/bin directory, and put ~/bin in my $PATH.

    #!/usr/bin/env bash
    # scriptname: above
    # author: Jonathan D. Lettvin, 201401220235
    
    # This finds files of size >= $1 (format ${count}[K|M|G|T], default 10G)
    # using a reliable version-independent bash hash to relax find's -size syntax.
    # Specifying size using 'T' for Terabytes is supported.
    # Output size has units (K|M|G|T) in the left hand output column.
    
    # Example:
    #   ubuntu12.04$ above 1T
    #   128T /proc/core
    
    # http://stackoverflow.com/questions/1494178/how-to-define-hash-tables-in-bash
    # Inspiration for hasch: thanks Adam Katz, Oct 18 2012 00:39
    function hasch() { local hasch=`echo "$1" | cksum`; echo "${hasch//[!0-9]}"; }
    function usage() { echo "Usage: $0 [{count}{k|K|m|M|g|G|t|T}"; exit 1; }
    function arg1() {
        # Translate single arg (if present) into format usable by find.
        count=10; units=G;  # Default find -size argument to 10G.
        size=${count}${units}
        if [ -n "$1" ]; then
            for P in TT tT GG gG MM mM Kk kk; do xlat[`hasch ${P:0:1}`]="${P:1:1}"; done
            units=${xlat[`hasch ${1:(-1)}`]}; count=${1:0:(-1)}
            test -n "$units" || usage
            test -x $(echo "$count" | sed s/[0-9]//g) || usage
            if [ "$units" == "T" ]; then units="G"; let count=$count*1024; fi
            size=${count}${units}
        fi
    }
    function main() {
        sudo \
            find / -type f -size +$size -exec ls -lh {} \; 2>/dev/null | \
            awk '{ N=$5; fn=$9; for(i=10;i<=NF;i++){fn=fn" "$i};print N " " fn }'
    }
    
    arg1 $1
    main $size
    
    0 讨论(0)
  • 2020-11-28 18:52

    That is quite simpler way to do it:

    ls -l | tr -s " " " " | cut -d " " -f 5,9 | sort -n -r | head -n 1***
    

    And you'll get this: 8445 examples.desktop

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