Find open files in linux using lsof
Deleting a file that has been opened by another process in linux does not free up disk space. Running the df or du commands will indicate conflicting results. Closing / killing the process that opened the files will release the space on the disk. The lsof command can help you track, say the top ten open files in your OS sorted by disk space. If you ever run into trouble with large open files, use the following command
Top ten open files:
lsof / | awk ‘{if($7 > 1048576) print $7/1048576 “MB” ” ” $9 }’ | sort -n -u | tail
Output:
3.8054MB /usr/lib/libgtk-x11-2.0.so.0.2200.0
4.28024MB /usr/share/icons/hicolor/icon-theme.cache
8.17912MB /usr/lib/locale/locale-archive
8.86022MB /var/lib/apt/lists/lk.archive.ubuntu.com_ubuntu_dists_maverick_main_binary-i386_Packages
11.4047MB /usr/lib/flashplugin-installer/libflashplayer.so
14.6893MB /usr/lib/firefox-3.6.10/libxul.so
15.6504MB /var/cache/apt/pkgcache.bin
27.4744MB /var/lib/apt/lists/lk.archive.ubuntu.com_ubuntu_dists_maverick_universe_binary-i386_Packages
34.6615MB /usr/share/icons/gnome/icon-theme.cache
44.1719MB /home/user/.mozilla/firefox/tnrqzpro.default/urlclassifier3.sqlite
You can also lookup open files based on pid / port number. I hope the script saves you some time, should you ever find yourself in this situation.
This is one of the most useful one-liners I’ve ever used. Thank you!
Thanks a lot for this. Absolutely brilliant. Saved my day
Cheers.
use -g instead of -n at sort command for better float-sorting!