1

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

1 Answer 1

5

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".

2
  • 1
    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: superuser.com/questions/80033/…
    – artsel
    Jan 9, 2014 at 11:23
  • Works in Linux. find on OSX does not support this syntax :(
    – Marcin
    Jan 9, 2014 at 15:13

You must log in to answer this question.

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