This question already has an answer here:

I need to find the largest file in the current and subsequent directory. I tried

ls -Rlh | awk '{print $3 " " $5 " " $9}' 

but do not know if it is ok, how to sort and select the largest file.

marked as duplicate by Jeff Schaller, roaima, GAD3R, Stephen Rauch, Stéphane Chazelas shell-script Nov 20 '17 at 17:13

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

up vote 3 down vote accepted

GNU find + sort + head solution (for any directory depth level), assuming file paths don't contain newline characters:

find . -type f -printf "%s %p\n" | sort -nr | head -1
  • %s - format specificator pointing to file size (in bytes)
  • %p - format specificator pointing to file name
  • sort -nr - sort records numerically in reversed order
  • head -1 - print the TOP first line/record

To get a human-readable file size value - extend the pipeline with GNU numfmt command (if supported):

find . -type f -printf "%s %p\n" | sort -nr | head -1 | numfmt --to=si
  • I would need human readable disk usage. Do you have any recommendations? It works perfect so far. – Alex Nov 20 '17 at 16:12
  • @JamesW, se my update – RomanPerekhrest Nov 20 '17 at 16:33
  • Worked perfect! I didn't know there is an actual function for converting to human readable – Alex Nov 20 '17 at 17:19

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.

  • I might do something wrong but it does not work – Alex Nov 20 '17 at 16:12
  • @JamesW, I suspect you missed the "With zsh" part. What shell are you trying it in? In which way does it not work? – Stéphane Chazelas Nov 20 '17 at 16:16
  • I am running bash. So I have to put that in a script, right and execute with zsh. Can I do it straight from the command line? – Alex Nov 20 '17 at 16:29
  • From bash, you can always do zsh -c 'ls -ld -- **/*(.DOL[1])' but if you're often finding yourself looking for the quickest way, you may want to consider switching your shell to zsh (also for safest way , best way and more efficient way). – Stéphane Chazelas Nov 20 '17 at 16:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.