Largest Files and Directories in a Linux Filesystem
Simple script with nice formatting.
The results show:
- a summary for the file system (line 6
- a list of files larger than 50 MB (line 16)
- a list of the top 20 largest directories (line 27)
Directory sizes are recursively calculated (the totals include subdirectories).
In line 6, the
grep -Po command allows us to use Perl-like regex syntax (e.g. lookaheads are supported), and to only return the matched portion of the searched text.
Output is formatted. For example, running the script against a Maven repo gives a directory size listing like this:
149,731,531 - . 109,943,053 - ./.m2 109,943,029 - ./.m2/repository 77,870,516 - ./.m2/repository/org 38,947,091 - ./.ivy2 38,947,072 - ./.ivy2/cache 10,494,306 - ./.m2/repository/com/google 8,723,165 - ./.m2/repository/org/apache/maven 8,471,569 - ./.m2/repository/org/hibernate
The directory sizes are right-justified using
%15 and the thousand separators are added using
\' (line 28).