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

We are just starting a UNIX class and are learning a lot of Bash commands. Our task includes the execution of various commands in a directory in which there are also several folders.

I know how to list and count all regular files from the root folder using:

find . -type l | wc -l 

But I would like to know where to go from there to find the largest file in the entire directory. I saw something about the du command, but we didn’t find out, so in the repertoire of the things that we learned, I assume that we need to somehow connect it to the ls -t .

And forgive me if my "jargon" is wrong, I'm still used to it!

+91
file bash directory find large-files
Sep 20 '12 at 23:15
source share
15 answers

Quote from this link -

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

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

To limit the search in this directory, use "-maxdepth 1" with find.

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

And print the 10 largest "files and directories":

$ du -a . | sort -nr | head

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

+117
Sep 20 '12 at 23:21
source share

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

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

This will produce the top 25 files by sorting by file size using the "sort -nr -k5" command.

Same with readable file size:

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

+56
May 29 '14 at 3:42
source share
 find . -type f | xargs ls -lS | head -n 1 

exits

 -rw-r--r-- 1 nneonneo staff 9274991 Apr 11 02:29 ./devel/misc/test.out 

If you just need a file name:

 find . -type f | xargs ls -1S | head -n 1 

This avoids the use of awk and allows the use of any flags in ls .

Caveat. Since xargs tries to avoid creating overlapping command lines, this may fail if you run it in a directory with a large number of files, because ls ends more than once. This is not an insurmountable problem (you can collect the output of head -n 1 from each ls call and run ls -S again, looping until you have one file), but this will change this approach a bit.

+10
Sep 21 '12 at 3:45
source share

Enumerates files recursively, if they are ordinary files, sorted by the 7th field (the size of which is in my find output, check yours) and shows only the first file.

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

The first option for find is the starting path for a recursive search. A-type f searches for normal files. Please note: if you try to parse this as a file name, you may fail if the file name contains spaces, newlines or other special characters. sort options are also operating system dependent. I am using FreeBSD.

A β€œbetter” but more complex and heavier solution would be to find cross directories, but maybe use stat to get file information, and then maybe use awk to find the largest size. Note that stat output also depends on your operating system.

+8
Sep 20 '12 at 23:20
source share

There is no simple command to find the largest files / directories in the Linux / UNIX / BSD file system. However, by combining the following three commands (using channels), you can easily find out a list of the largest files:

 # du -a /var | sort -n -r | head -n 10 

If you want more user-friendly output, follow these steps:

 $ cd /path/to/some/var $ du -hsx * | sort -rh | head -10 

Where

  • Var is the directory you searched for
  • du -h command: display sizes in human readable format (e.g. 1K, 234M, 2G).
  • du command -s option: show only the argument common to each (summary).
  • du -x command: skip directories on various file systems.
  • sort command -r: cancel the result of comparisons.
  • sort command -h: compare human readable numbers. This option is for GNU only.
  • command head -10 OR -n 10: display the first 10 lines.
+7
May 26 '14 at 9:34
source share

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

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

To find the largest file in all subdirectories:

 find /path/to/folder -type f -exec ls -s {} \; | sort -nr | awk 'NR==1 { $1=""; sub(/^ /, ""); print }' 
+6
Sep 20 '12 at 23:21
source share

On Solaris, I use:

 find . -type f -ls|sort -nr -k7|awk 'NR==1{print $7,$11}' #formatted 

or

 find . -type f -ls | sort -nrk7 | head -1 #unformatted 

because everything else posted here is not working. This will find the largest file in $PWD and subdirectories.

+3
Oct 13 '13 at 22:33
source share

Try the 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 

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

+2
Sep 21 '12 at 3:23
source share

Try using one single line (display the 20 largest 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 

It works fine under Linux / BSD / OSX compared to other answers, since the find -printf option -printf not exist on OSX / BSD, and stat has different parameters depending on the OS. However, the second command is to work correctly with OSX / BSD (since sort does not have -h ), install sort from coreutils or remove -h from ls and use sort -nr instead.

So these aliases are useful for 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' 
+2
Mar 05 '15 at 12:34
source share

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

or

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

+1
Sep 20 '12 at 23:33
source share

Solution for Linux:. For example, you want to see a list of all the files / folders in your home directory (/) according to the file / folder size ( descending ).

sudo du -xm / | sort -rn | more

+1
Mar 03 '17 at 21:05
source share

This script makes it easy to find large files for further action. I store it in the ~ / 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 -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
Jan 22 '14 at 7:41
source share

This is a pretty simple way to do this:

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

And you get the following: 8445 examples.desktop

0
Mar 14 '14 at 7:40
source share

To display a larger file in a folder

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

The output of ls -sh is ls -sh representing the size s and the person h size of the file number.

You can use ls -shS/pathFolder | head -n 1 ls -shS/pathFolder | head -n 1 ls -shS/pathFolder | head -n 1 ls -shS/pathFolder | head -n 1 . The more S from ls , the more ordered the list is from large to smaller files, but the first result is the sum of all the files in this folder. Therefore, if you just want to list a larger file, one file, you need head -n 2 and check the "second line result" or use the first example with ls sort head .

0
Mar 08 '18 at 1:14
source share
 ls -alR|awk '{ if ($5 > max) {max=$5;ff=$9}} END {print max "\t" ff;}' 
0
Jan 11 '19 at 16:34
source share



All Articles