leonormes leonormes - 1 year ago 116
Bash Question

Sort Tree out put by file size

I have a dirs and subdirs that contain images (525 images all together in 15 folders).

I used

tree -Csh
to list them and get;

enter image description here

What I am trying to do now is sort them by size because I am trying to quickly see which files are too small (for a website with high-resolution images). As you can see, in this picture there is a 774K jpg that is too small but sorted like this it is hard to pick them out (the full output is very long).
Is tree the best way to go about this?

Answer Source

Using GNU tools, from the parent directory:

find . -type f -iname '*.jpg' -printf '%s %p\0' | sort -z -k1,1rn | xargs -0 -n1
  • find . -type f -iname '*.jpg' -printf '%s %p\0' finds all .jpg files (case insensitively), and outputs the filesize in bytes as first field and filename as second field, with each record separated by NUL, this is required to handle filenames with newlines in them

  • sort -z -k1,1rn sorts the NUL separated records based on first field numerically, reversed so the filenames with higher sizes will show at the top

  • xargs -0 -n1 prints the filenames with with descending size

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download