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

terdon
  • 54,564
artsel
  • 13

1 Answers1

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

glenn jackman
  • 27,524