14

I am currently trying to see , all the files which are using /var mount.

With lsof | grep /var* when Its displaying size in bytes. How can I display file size in MB.

Thank you.

Cyrus
  • 12,309
Raja G
  • 5,937
  • 1
    Side note: you might want to try using lsof +d /var instead of grepping. –  Aug 10 '17 at 01:57

3 Answers3

28

Starting with GNU Coreutils version 8.21 (released on Dec-2013), there is a new standard program called numfmt (=number format). It will do exactly what you want.

Example:

lsof | grep /var*  | numfmt --field=8 --to=iec | head

The parameter --to accepts iec (where 1K=1024B) or si (where 1K=1000). There are few additional options, more information here: http://www.gnu.org/s/coreutils/numfmt .

(disclaimer: I wrote the initial implementation of numfmt).

A. Gordon
  • 699
5

Try this:

| awk '{$7=$7/1048576 "MB"; print}'

or shorter:

| awk '{$7=$7/1048576 "MB"}1'
Cyrus
  • 12,309
1

You can use awk to convert bytes to MB.

Something like this should show size in MB.

lsof | grep /var* | awk '{for(i=1;i<=6;i++){printf "%s ", $i}; print $7/1048576 "MB" " "$8" "$9 }'

It will print all fields up to 7th field, which is then divided with 1048576 to get the size in MB, and then is shows remaining two fields.

ralz
  • 1,996
  • 13
  • 17
  • I have this idea , But I am thinking something internally from lsof and manpage no such information. Thank you for your attention and time on this issue. +1 – Raja G Jan 05 '16 at 06:03