A few CLI tools are involved:
find
to traverse the target directory to get all the filesdu
to get size of each filesort
to rank the files by sizehead
to pick the top onesSuppose you want to get the top 5 biggest files in directory /var/log/
:
find /var/log/ -type f -print0 | du -h --files0-from=- | sort -h --reverse | head -n 5
The output looks like:
17M /var/log/supervisor/supervisord.log
288K /var/log/lastlog
28K /var/log/apt/eipp.log.xz
12K /var/log/wtmp
8.0K /var/log/dpkg.log.2.gz
Now let's explain the above command in detail:
find /var/log/ -type f -print0
-type f
: Only search files. Child directories are ignored.-print0
: Print file names on the standard output, followed by a null character, so that other programs can correctly process the output.du -h --files0-from=-
-h
: Print sizes in human readable format (e.g., 1K 234M 2G).--files0-from=-
: Read names from standard input.sort -h --reverse
-h
: Correspond to du -h
.--reverse
: Sort from big to small.head -n 5
Pick the top 5.If you want the top smallest files, replace sort -h --reverse
with sort -h
.
find /var/log/ -type f -print0 | du -h --files0-from=- | sort -h | head -n 5