Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

1 Answer 1

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
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 at 11:23
    
Works in Linux. find on OSX does not support this syntax :( –  Marcin Jan 9 at 15:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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