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

前端 未结 15 1305
醉梦人生
醉梦人生 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:53

    This will find the largest file or folder in your present working directory:

    ls -S /path/to/folder | head -1
    

    To find the largest file in all sub-directories:

    find /path/to/folder -type f -exec ls -s {} \; | sort -nr | awk 'NR==1 { $1=""; sub(/^ /, ""); print }'
    
    0 讨论(0)
  • 2020-11-28 18:55

    This lists files recursively if they're normal files, sorts by the 7th field (which is size in my find output; check yours), and shows just the first file.

    find . -type f -ls | sort +7 | head -1
    

    The first option to find is the start path for the recursive search. A -type of f searches for normal files. Note that if you try to parse this as a filename, you may fail if the filename contains spaces, newlines or other special characters. The options to sort also vary by operating system. I'm using FreeBSD.

    A "better" but more complex and heavier solution would be to have find traverse the directories, but perhaps use stat to get the details about the file, then perhaps use awk to find the largest size. Note that the output of stat also depends on your operating system.

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

    Try following command :

    find /your/path -printf "%k %p\n" | sort -g -k 1,1 | awk '{if($1 > 500000) print $1/1024 "MB" " " $2 }' |tail -n 1 
    

    This will print the largest file name and size and more than 500M. You can move the if($1 > 500000),and it will print the largest file in the directory.

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

    Quote from this link-

    If you want to find and print the top 10 largest files names (not directories) in a particular directory and its sub directories

    $ find . -printf '%s %p\n'|sort -nr|head

    To restrict the search to the present directory use "-maxdepth 1" with find.

    $ find . -maxdepth 1 -printf '%s %p\n'|sort -nr|head

    And to print the top 10 largest "files and directories":

    $ du -a . | sort -nr | head

    ** Use "head -n X" instead of the only "head" above to print the top X largest files (in all the above examples)

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

    To find the top 25 files in the current directory and its subdirectories:

    find . -type f -exec ls -al {} \; | sort -nr -k5 | head -n 25

    This will output the top 25 files by sorting based on the size of the files via the "sort -nr -k5" piped command.

    Same but with human-readable file sizes:

    find . -type f -exec ls -alh {} \; | sort -hr -k5 | head -n 25

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

    To list the larger file in a folder

    ls -sh /pathFolder | sort -rh | head -n 1
    

    The output of ls -sh is a sized s and human h understandable view of the file size number.

    You could use ls -shS /pathFolder | head -n 1. The bigger S from ls already order the list from the larger files to the smaller ones but the first result its the sum of all files in that folder. So if you want just to list the bigger file, one file, you need to head -n 2 and check at the "second line result" or use the first example with ls sort head.

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