Home > Software design >  generating human-readable file sizes from a find command
generating human-readable file sizes from a find command

Time:06-21

I'm pulling list of largest files recursively with this statement:

sudo find 11 -type f -printf "%s\t%p\n" | sort -n | tail -10

Business folks would like this in a human readable format (similar to the content that ls -lrth provides) -- I've tinkered with syntax but can't quite get it to provide similar results.

CodePudding user response:

This might help:

sudo find 11 -type f -printf "%s\t%p\n" | sort -n | tail -10 \
  | awk '{$1=""; print}' | sudo xargs -I {} ls -lh {}

With awk I remove first column.

See: man xargs

CodePudding user response:

GNU core utilities - numfmt

numfmt: Reformat numbers numfmt reads numbers in various representations and reformats them as requested. The most common usage is converting numbers to/from human representation (e.g. ‘4G’ → ‘4,000,000,000’).

sudo find 11 -type f -printf "%s\t%p\n" |
  sort -n |
  tail -10 |
  numfmt --to=iec-i --suffix=B --format="%.3f"

To show some examples demonstrating how this works:

$ numfmt --to=iec --suffix=B --format="%.3f" 49532
48.372KB

$ numfmt --to=iec --suffix=B --format="%.3f" 49532058
47.238MB

# iec-i with i "GiB"
$ numfmt --to=iec-i --suffix=B --format="%.3f" 4953205800
4.614GiB

# iec without i "GB"
$ numfmt --to=iec --suffix=B --format="%.3f" 4953205800
4.614GB
  • Related