How to find biggest (or smallest) files in a directory on linux with CLI commands¶
A few CLI tools are involved:
- Use
find
to traverse the target directory to get all the files - Use
du
to get size of each file - Use
sort
to rank the files by size - Use
head
to pick the top ones
Command to get the top 5 biggest files¶
Suppose 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
Explanation in detail¶
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 todu -h
.--reverse
: Sort from big to small.
head -n 5
Pick the top 5.
Command to get 5 smallest files¶
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
This article is originally created by tooli.top. Please indicate the source when reprinting : https://www.tooli.top/posts/find_biggest_files
Posted on 2022-06-05
Mail to author