Monday, June 1, 2015

Finding Largest Files in Bash

I often need to find the largest files in a directory tree.

I have used

du -a | sort -nr | head -n 10

but it includes directories.

To exclude all directories, I tried using find.

find -type f -exec ls -l {} \; | tr -s ' ' | cut -d' ' -f 5,9- | sort -rn | head -n 10

Explanation
  1. -type f means only look at files
  2. -exec ls -l {} \; means to show a long listing for each matched file
  3. tr -s ' ' replaces multiple occurrences of space with one space
  4. cut -d' ' -f 5,9- sets the delimiter to space and displays the columns 5 and everything after 8. Column 5 is the size, everything after 8 is the file name (with possible embedded spaces)

No comments:

Post a Comment

Followers