Easy-to-read, recursive, sorted list of largest files

What is the best practice for printing the 10 largest files in a POSIX shell? There must be something more elegant than my current solution:

DIR="." N=10 LIMIT=512000 find $DIR -type f -size +"${LIMIT}k" -exec du {} \; | sort -nr | head -$N | perl -p -e 's/^\d+\s+//' | xargs -I {} du -h {} 

where LIMIT is the file size threshold to limit search results.

+6
linux unix shell posix
source share
1 answer

Awk is used to create additional columns for the sort keys. He only calls du once. The result should look exactly like du .

I split it into several lines, but it can be combined into a single line.

 du -h | awk '{printf "%s %08.2f\t%s\n", index("KMG", substr($1, length($1))), substr($1, 0, length($1)-1), $0}' | sort -r | cut -f2,3 

Explanation:

  • BEGIN - create a row for indexing to replace 1, 2, 3 for K, M, G to group by units, if there is no unit (size less than 1K), then there is no match and zero is returned (excellent!)
  • print new fields - unit, value (to make alpha sorting work with zero, fixed length) and the original line
  • index of the last character of the size field
  • pull out the digital part of the size
  • sort results, discard additional columns

Try without the cut to see what it does.

Edit:

Here's the version that does the sorting in the AWK script and doesn't need a cut:

 du -h | awk '{idx = sprintf("%s %08.2f %s", index("KMG", substr($1, length($1))), substr($1, 0, length($1)-1), $0); lines[idx] = $0} END {c = asorti(lines, sorted); for (i = c; i >= 1; i--) print lines[sorted[i]]}' 
+6
source share

All Articles