During Linux files issues, we found that some process is consuming the directory size like anything. that would be related to file which is drastically increase the files size, that’s mean file size is increasing as soon as process related to the operation which filling logs data or another operating data into file. So as a Linux Engineer, this would be very challenging to find the top largest files in directory and take necessary action.

Find the top 5 largest files in directory, We can get the result in many way to write the linux command either with du or find popular linux command. Lets consider the file below in Linux machine.

Top 5 largest files in directories on Linux/Unix

Top 5 largest files using du (Disk usages command)

Linux Command – du -a | sort -n -r | head -n 10

 

Vicky@pwr-VirtualBox:~/test_dir$ ls -lrt
total 24
-rw-rw-r-- 1 Vicky Vicky 866 Aug 14 00:49 test01.txt
-rw-rw-r-- 1 Vicky Vicky 600 Aug 14 00:50 test01.sql
-rw-rw-r-- 1 Vicky Vicky 13752 Aug 14 00:54 test03.sql
Vicky@pwr-VirtualBox:~/test_dir$ du -a | sort -n -r | head -n 10
28 .
16 ./test03.sql
4 ./test01.txt
4 ./test01.sql

Vicky@pwr-VirtualBox:~/test_dir$ du -a | sort -rh | tail -10
28 .
16 ./test03.sql
4 ./test01.txt
4 ./test01.sql

 

Linux Command – du -hsx * | sort -rh | head -10

Vicky@pwr-VirtualBox:~/test_dir$ du -hsx * | sort -rh | head -10
16K test03.sql
4.0K test01.txt
4.0K test01.sql

Linux Command – du -hsx — * | sort -rh | head -10

Vicky@pwr-VirtualBox:~/test_dir$ du -hsx — * | sort -rh | head -10
16K test03.sql
4.0K test01.txt
4.0K test01.sql

if you interested to know about SQL interview question and answer : please follow up below link.

How to write script to get the same result Top 5 largest files in directories on Linux/Unix.

vicky@pwr-VirtualBox:~/test_dir$ cat >> scri.sh
for i in V K Y
do
du -ah | grep [0-9]$i | sort -nr -k 1
done | head -n 11

vicky@pwr-VirtualBox:~/test_dir$ chmod 777 scri.sh

vicky@pwr-VirtualBox:~/test_dir$ sh scri.sh
32K .
16K ./test03.sql
4.0K ./test01.txt
4.0K ./test01.sql
4.0K ./scri.sh

vicky@pwr-VirtualBox:~/test_dir$ cat scri.sh
for i in V K Y
do
du -ah | grep [0-9]$i | sort -nr -k 1
done | head -n 11

Top 5 largest files using Find (global utilities find tool Linux command).

Linux Command – find . -printf ‘%s %p\n’| sort -nr | head -10

vicky@pwr-VirtualBox:~/test_dir$ find . -printf ‘%s %p\n’| sort -nr | head -10
13752 ./test03.sql
4096 .
866 ./test01.txt
600 ./test01.sql
77 ./scri.sh

Linux Command – find /home/vicky/test_dir -printf ‘%s %p\n’| sort -nr | head -10

vicky@pwr-VirtualBox:~/test_dir$ find /home/vicky/test_dir -printf ‘%s %p\n’| sort -nr | head -10
13752 /home/vicky/test_dir/test03.sql
4096 /home/vicky/test_dir
866 /home/vicky/test_dir/test01.txt
600 /home/vicky/test_dir/test01.sql
77 /home/vicky/test_dir/scri.sh

vicky@pwr-VirtualBox:~/test_dir$ find /home/vicky/test_dir -type f -printf ‘%s %p\n’| sort -nr | head -10
13752 /home/vicky/test_dir/test03.sql
866 /home/vicky/test_dir/test01.txt
600 /home/vicky/test_dir/test01.sql
77 /home/vicky/test_dir/scri.sh

vicky@pwr-VirtualBox:~/test_dir$ find /home/vicky/test_dir -type f -iname "*.sql" -printf '%s %p\n'| sort -nr | head -10
13752 /home/vicky/test_dir/test03.sql
600 /home/vicky/test_dir/test01.sql

3 thoughts on “How to find top 5 largest files in directories on Linux/Unix”

Leave a Reply

Your email address will not be published. Required fields are marked *

Netflix’s Overall Architecture. Gold is money every thing else is credit