Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to find the biggest file

  1. Only one file should be listed
  2. Search should work in given directory and subdirectories
  3. Output should display absolute path to the file with filename

    find "$PARAM" -type f | xargs ls -1S | head -n 1

works but gives me errors like

ls: cannot access Over: No such file or directory

share|improve this question
up vote 5 down vote accepted

Don't parse ls. Let find do that work for you:

find "$PARAM" -type f -printf "%s\t%p\n" | sort -n | tail -n 1 | cut -f 2- 

Without find, we can use bash's recursive globbing:

shopt -s globstar nullglob
stat -c $'%s\t%F\t%n' ** \
| awk -F'\t' '$2 == "regular file"' \
| sort -n \
| tail -n 1 \
| cut -f 3-

The stat on OSX will have different but equivalent options for stat, and may spit out a different string for "regular file".

share|improve this answer
Good. I also found a solution with ls:find $PARAM -type f -print0 | xargs -0 ls -1S | head -n 1 The errors were because of spaces:… – artsel Jan 9 '14 at 11:23
Works in Linux. find on OSX does not support this syntax :( – Marcin Jan 9 '14 at 15:13

You must log in to answer this question.

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