13

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.
    – user153612
    Aug 10, 2017 at 1:57

3 Answers 3

27

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)!! Aug 2, 2017 at 22:29
  • 2
    --field=7 is what you want for lsof, which is the SIZE of file that the OP was looking for. Thanks.
    – mfink
    Mar 29, 2020 at 19:12
4

Try this:

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

or shorter:

| awk '{$7=$7/1048576 "MB"}1'
1
  • This sounds interesting.. seems minimized. +1\
    – Raja G
    Jan 5, 2016 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
    – Raja G
    Jan 5, 2016 at 6:03

Not the answer you're looking for? Browse other questions tagged .