How to find space hogging file on your hard disk with a Linux command.

Posted: January 28, 2014. At: 11:30 AM. This was 4 years ago. Post ID: 6916

This command will list all open files, sort them with awk and list the biggest space hogging files.

lsof / | awk '{if($7 > 1048576) print $7/1048576 "MB" " " $9 }' | sort -n -u | tail

Here is example output of this file on my system. This is a good way to get a picture of what open files are taking up disk space. This is sorted from smallest to largest.

[email protected]:~/Downloads$ lsof / | awk '{if($7 > 1048576) print $7/1048576 "MB" " " $9 }' | sort -n -u | tail
1.53322MB /usr/lib/locale/locale-archive
1.61442MB /usr/lib/x86_64-linux-gnu/
1.65765MB /lib/x86_64-linux-gnu/
1.8548MB /usr/bin/caja
2.06578MB /usr/lib/x86_64-linux-gnu/
2.17364MB /usr/lib/x86_64-linux-gnu/
4.29156MB /usr/lib/x86_64-linux-gnu/
4.29343MB /usr/lib/x86_64-linux-gnu/
6.70585MB /usr/lib/x86_64-linux-gnu/
22.4236MB /usr/lib/x86_64-linux-gnu/

Another way is to install the ncdu package and then run this in the directory to see what the largest file is.

The du command can list files and show the largest file(s) in the directory. This solution will sort all files by size.

[email protected]:~/Downloads$ du -hs * | sort -h
460K	paper-gtk-theme_2.1+r265~daily~ubuntu16.04.1_all.deb
3.6M	Bluecurve-Phenix.tar.gz
16M	macOS Dark
16M	macOS Sierra
40M	paper-icon-theme_1.4+r674~daily~ubuntu16.04.1_all.deb

This version of the command will output file-sizes in kilobytes.

[email protected]:~/Downloads$ du -BK * | sort -h
460K	paper-gtk-theme_2.1+r265~daily~ubuntu16.04.1_all.deb
3632K	Bluecurve-Phenix.tar.gz
15416K	macOS Sierra
15980K	macOS Dark
39952K	paper-icon-theme_1.4+r674~daily~ubuntu16.04.1_all.deb

The example here will sort the output of du to have largest files first.

[email protected]:~/Downloads$ du -hs * | perl -e 'sub h{%h=(K=>10,M=>20,G=>30);($n,$u)=shift=~/([0-9.]+)(\D)/;
return $n*2**$h{$u}}print sort{h($b)<=>h($a)}<>;'
40M	paper-icon-theme_1.4+r674~daily~ubuntu16.04.1_all.deb
16M	macOS Dark
16M	macOS Sierra
3.6M	Bluecurve-Phenix.tar.gz
460K	paper-gtk-theme_2.1+r265~daily~ubuntu16.04.1_all.deb

No comments have been made. Use this form to start the conversation :)

Leave a Reply