1

I am writing a simple helper function to show a sizes of the files:
This is the code I have so far:

find . -type f -size +10M -printf "%f -> %s B\n"

The output I get is:

clay-banks--Ni2fpLUgRI-unsplash.jpg -> 181794593 B
jake-nackos-_kdX2vPc33U-unsplash.jpg -> 448148323 B
73-738467_nature-wallpapers-high-quality-images-hd-desktop-images.jpg -> 131115725 B

However, I want to show the file sizes in MB.

How should I modify this so it works like b

clay-banks--Ni2fpLUgRI-unsplash.jpg -> 173 MB
jake-nackos-_kdX2vPc33U-unsplash.jpg -> 427 MB
73-738467_nature-wallpapers-high-quality-images-hd-desktop-images.jpg -> 125 MB
2
  • Is the exact format crucial? do you always want MB or do you just want "human readable" multipliers KB / MB / GB ? – steeldriver May 3 '20 at 15:39
  • Yes, that should be the exact format - always MB. – Alex May 3 '20 at 15:43
4

At least on GNU-based systems, you should be able to use stat and numfmt to get the desired format, ex.:

find . -type f -size +10M -printf "%f -> " -exec sh -c '
  stat -c "%s" "$1" | numfmt --to-unit=1048576 --format="%.0f MB"
' sh {} \;

Change --to-unit=1048576 to --to-unit=1000000 depending whether you want MB or MiB.

2

If you have access to the ast-open implementation of ls:

find . -type f -size +10M -exec ls -ldh --format='%(name)s -> %(size)dB' {} +

Would use MiB, GiB, TiB... suffixes as appropriate.

With zsh, on any system (using builtin functions only), for MiB only (rounded to nearest):

zmodload zsh/stat
stat -LnA info +size -- **/*(D.LM+10)
for f s ($info) printf '%s -> %.0fMiB\n' $f $(($s / 1048565.))
0

The race is over and @steeldriver won +1 ;-), but I would have gone a different (maybe simpler ?) way:

$ find . -type f -size +10M -printf "%f %s\n" | awk '{printf("%s -> %.0f MB\n", $1,$2/1048576)}'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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