#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0

    List the file size in MB


    Hi all,

    I am using
    find /home -type f -ls | sort -k 7 -r -n | head -10
    to find the largest 10 memory consuming files, but the memory is coming in bytes. How to display the same in MB.

    Thanks!!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,436
    Rep Power
    1688
    Either pipe the output through something like awk to do the conversion, or ...

    Code:
    find /home -type f -exec ls -logdk {} \; | sort -k 3 -r -n | head -10
    Has the mild side affect of being a little more efficient as the -o and -l options prevent looking up group and owner for each file. The -k gives the output in kilobytes. I tossed the -d option in just as an 'in case', Your -type f in the awk should mean it's not required.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0
    Thanks!!

    any chances of getting the size in MB and also with owner and group being displayed ?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,436
    Rep Power
    1688
    No, sort of, and yes. There is not "display this in multiple of MB" option, just the default blocks (512 bytes), KB, and 'human readable' which will (from the man page): "with -l, print sizes in human readable format (e.g., 1K 234M 2G)"
    To get owner and group back, just remove the o and g options from the ls command and readjust the sort key.

    Code:
    find /home -type f -exec ls -ldh {} \; | sort -k 5 -r -n | head -10
    But that, of course, will scupper the sort since 5KB will be seen as being bigger then 1GB.

    Depending on what OS you are using it may be posisble to use:
    Code:
    find /home -type f -exec ls -ld --block-size=1048576 {} \; | sort -k 5 -r -n | head -10
    If not, then we fall back to using a base of KB and alter the output using awk ...
    Code:
    find /home -type f -exec ls -ldk {} \; | sort -k 5 -r -n | head -10 | awk '{$5=$5/1024 ; print}'
    You may want to use int($5/1024) instead, to get an integer, which will truncate, rather than round. So you may want to put a +1 on the end whcih will, at least, give a value for stuff actually under 1MB in size. If the file happens to be exactly a multiple of 1MB it will, of course, give a wrong-ish answer
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  8. #5
  9. No Profile Picture
    Grumpier old Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2003
    Posts
    14,424
    Rep Power
    4539
    ls -alFh shows what you want in my Fedora box.
    ======
    Doug G
    ======
    The man who doesn't read good books has no advantage over the man who can't read them.
    --Mark Twain

IMN logo majestic logo threadwatch logo seochat tools logo