I am trying to find the largest file in a directory recursively. If there is a subdirectory inside of that directory the function needs to go inside that directory and check to see if the largest file is there. Once the largest file is found the output is displayed with the relative path name and the name and size of the largest file.


dude@shell2 (~...assignment/solutions) % bash maxfile.sh ~/test
dude.h.gch: 9481628

This is what I have:


recursiveS() {
    for d in *; do
        if [ -d $d ]; then
            (cd $d; echo $(pwd)/$line; du -a; recursiveS;)

I have been stuck for a while now. I cannot implement this by pipelining a number of existing Unix tools. Any ideas would be nice!


6 Answers 6


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
for f in **; do
  if [[ -f "$f" && ! -L "$f" ]]; then
    size=$( stat -c %s -- "$f" )
    if (( size > max_s )); then
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.

  • Thanks, it works! I appreciate the help. I am trying to get use to programming in shell. I do not know a whole lot right now so I appreciate you telling me what is happening with that line of code. Sep 29, 2014 at 20:24
  • Quick question: Out of curiosity is there a way to do it without piping commands? I am curious because every example I have seen has used piping of some kind. Sep 29, 2014 at 20:25
  • 2
    I'm sure there are other ways to do it. The UNIX philosophy is that tools should be single-purpose, and to chain them together so the output of one command is fed into the input of the next. Sep 29, 2014 at 20:29
  • That makes sense. Thank you again for your help. Sep 29, 2014 at 20:30
  • 2
    @user2419571: tail -n 1 <(sort -n <(find . -type f -printf "%s\t%p\n")) ;)
    – Cyrus
    Sep 29, 2014 at 20:38

This command helps to list out defined size too.

find . -type f -size +100M -exec ls -lh {} \;
  • This is the first one to work for me on Unix (MacOS)
    – Dawson B
    Apr 23, 2022 at 4:20

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 :-)

  • Not really working on MacOS as it fails to return size correctly and returns a huge number of columns.
    – sorin
    Aug 21, 2018 at 8:26

With zsh, for the biggest regular file:

ls -ld -- **/*(.DOL[1])

(of course you can replace ls -ld -- with any command. If using GNU ls or compatible see also the -h option for human readable sizes)

  • .: only regular files (not directories, symlinks, devices, fifos...)
  • D: include hidden ones and descend into hidden dirs
  • OL: reverse-ordered by size (Length).
  • [1]: only the first match.

If there are ties, you'll get any one of them at random. If you want the first in alphabetical order, add an extra on (order by name) to sort ties alphabetically.

Note that it considers the files size, not disk usage.

  • ... I start to believe that you are on zsh's payroll ;) (which it very well could be?). zsh is unfortunately not available on all systems... Nov 20, 2017 at 18:21
  • Possible to get the first ten files? (Without doing something stupid like a loop) Apr 24, 2019 at 3:01
  • 1
    @Wowfunhappy replace [1] with [1,10] Apr 24, 2019 at 5:44

In addition to Stéphane Chazelas' response, I wanted to add the following notes (I am unable to comment):

  1. To get the smallest regular file, use o instead of O:
ls -ld -- **/*(.DoL[1])
  1. You can filter by a concrete (or several) extensions by appending it/them to the **/* wildcard:
ls -ld -- **/*.png(.DoL[1])
ls -ld -- **/*.{png, jpg}(.DoL[1]) # this will give two results, one per extension

For a pretty-print listing of the top 10 largest files:

find . -type f -print0 | xargs -0 -r du -sh | sort -h | tail -n10


273M    ./x86_64/release/python-pyqt5/python-pyqt5-debuginfo/python-pyqt5-debuginfo-5.15.6-1.tar.xz
344M    ./x86_64/release/clang/clang-debuginfo/clang-debuginfo-8.0.1-1.tar.xz
359M    ./x86_64/release/mysql/mysql-debuginfo/mysql-debuginfo-10.3.14-1.tar.xz
374M    ./x86_64/release/qt5-webkit/qt5-webkit-debuginfo/qt5-webkit-debuginfo-5.9.3-0.3.20180115git.tar.xz
378M    ./x86_64/release/webkitgtk/webkitgtk-debuginfo/webkitgtk-debuginfo-2.0.4-5.tar.xz
414M    ./x86_64/release/mingw64-i686-gcc/mingw64-i686-gcc-debuginfo/mingw64-i686-gcc-debuginfo-11.4.0-1.tar.zst
416M    ./x86_64/release/mingw64-x86_64-gcc/mingw64-x86_64-gcc-debuginfo/mingw64-x86_64-gcc-debuginfo-11.4.0-1.tar.zst
546M    ./x86_64/release/cmake/cmake-debuginfo/cmake-debuginfo-3.28.3-1.tar.xz
633M    ./noarch/release/texlive-collection-fontsextra/texlive-collection-fontsextra-20240312-1.tar.xz
682M    ./noarch/release/texlive-collection-latexextra-doc/texlive-collection-latexextra-doc-20240312-1.tar.xz

Some variations to try:

  • Only look at .tar.xz and .tar.gz files: find . -type f \( -name '*.tar.xz' -o -name '*.tar.gz' \) -print0 | xargs -0 -r du -sh | sort -h | tail -n10
  • Prevent recursing into other file systems: find . -xdev -type f -print0 | xargs -0 -r du -sh | sort -h | tail -n10
  • Search multiple folders: find /usr /home /var -type f -print0 | xargs -0 -r du -sh | sort -h | tail -n10
  • Suppress read errors: find . \! -readable -prune -o \( -type f -print0 \) | xargs -0 -r du -sh | sort -h | tail -n10
  • Limit to a max depth: find . -maxdepth 5 -type f -print0 | xargs -0 -r du -sh | sort -h | tail -n10
  • Output all to file: find . -maxdepth 5 -type f -print0 | xargs -0 -r du -sh | sort -rh > largest-file-list.txt

NOTE that this requires GNU extensions and may not work in Alpine/BSD/MacOS unless you install the GNU tools.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .