Finding largest file recursively

use find (here assuming GNU find) to output file names with the file size. sort. print out the largest one.

find . -type f -printf "%s\t%p\n" | sort -n | tail -1

That assumes file paths don't contain newline characters.


Using a loop in bash with the GNU implementation of stat:

shopt -s globstar
max_s=0
for f in **; do
  if [[ -f "$f" && ! -L "$f" ]]; then
    size=$( stat -c %s -- "$f" )
    if (( size > max_s )); then
      max_s=$size
      max_f=$f
    fi
  fi
done
echo "$max_s $max_f"

This will be significantly slower than the find solution. That also assumes that file names don't end in newline characters and will skip hidden files and not descend into hidden directories.

If there's a file called - in the current directory, the size of the file open on stdin will be considered.

Beware that versions of bash prior to 4.3 followed symbolic links when descending the directory tree.


This command helps to list out defined size too.

find . -type f -size +100M -exec ls -lh {} \;

This works on BSD/macOS and uses fast but non-POSIX -ls extension to find utility:

find . -type f -ls | sort -k7 -r | head -n 3

This is slower but may work on POSIX systems where -ls extension is not available in find:

find . -type f -exec ls -al {} \; | sort -k5 -r | head -n3

How it works:

  • find is a powerful file search utility that will show you results based on a given query explained below.
  • find . will search in current working directory.
  • find . -type f will search only for a specified file type "f" that is a regular file (it means it will skip directories, special files, links, sockets, etc).
  • -ls switch will instruct find display full information about files found. However, according to man on BSD system, it is an extension to IEEE Std 1003.1-2001 ("POSIX.1") standard and may not be available on all platforms.
  • Alternatively if -ls extension is not available on your system you may -exec .. {} \; with ls -al command and use 5th field to sort the results. {} means the file name that has been found. \; encloses the -exec command.
  • | Unix symbol means "pipe" that is will redirect output from one program to input of another program. It will send find program output text as inpurt to sort program.
  • sort is a sorting utility. It gets lines of data and then sort those lines ascending order as result.
  • sort -k7 means take 7th field as the sorting parameter value. Parameters are split by spaces. 7th parameter is the file size produced by the find utility. If you use -exec ls -al {} \; instead of -ls then you will use 5th field -k5 to sort.
  • -r tells sort to use reverse sorting that is put biggest first. Because we want to see biggest files first.
  • In addition you can also pipe (|) output of the sort to head utility that will only show three first lines (as instructed with -n 3) of the resulting output if you do not really want to see hundrends of the lines.

Long story short: Use find to find recursively regular files only starting search in currently working directory, then display full information about that file using -ls extension (or execute ls -al). Later on pass the results from find to sort using unix pipe | and make it sort biggest first based on the 7th (or 5th) field. In addition you can limit results to n lines using head utility.

I mean "use brainz" to make you read "man" and search for solutions offline yourself that will train you in problems solving from scratch :-)