9

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.

1
  • 1
    Side note: you might want to try using lsof +d /var instead of grepping. – user22a6db72d7249 Aug 10 '17 at 1:57
20

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).

2
  • This is amazing! Can't wait until every server has this (no RHEL6)!! – Elijah Lynn Aug 2 '17 at 22:29
  • 1
    --field=7 is what you want for lsof, which is the SIZE of file that the OP was looking for. Thanks. – mfink Mar 29 '20 at 19:12
3

Try this:

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

or shorter:

| awk '{$7=$7/1048576 "MB"}1'
1
  • This sounds interesting.. seems minimized. +1\ – rɑːdʒɑ Jan 5 '16 at 7:40
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.

1
  • 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 – rɑːdʒɑ Jan 5 '16 at 6:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.