How to get script to create a new file that lists folder content sorted by size?

I have a script that sorts and processes unsorted files to newly created directories.

Its working great, but I am trying to understand the leanest method to get the script to create an additional file within each newly created directory that:

Contains a list of all files in the directory sorted by size order.

#!/bin/bash
read -p "Good Morning, Please enter your file type name for sorting [ENTER]:" all_extensions
if cd /Users/christopherdorman/desktop
  then  while read extension
      do    destination="folder$extension"
        mkdir -p "$destination"
        mv  -v unsorted/*."$extension" "$destination"
      done   <<< "${all_extensions// /$'\n'}"
        mkdir -p foldermisc 
        if mv  -v unsorted/* "foldermisc"
      then  echo "Good News, the rest of Your files have been successfully processed"
        fi
fi

What is your OS and version. If your ls happens to know the -S option, you could try adding something like this at the end of your script

for i in folder*/; do
  ls -S "$i" > "${i}filelist"
done

---
Note: It helps readability to properly indent your script, for example that a fi and a then start at the same horizontal position as its if counterpart and that while starts on a new line:

if foo
then
  while bar
  do
    ...
  done
    ...
  if baz
  then
    ...
  fi
fi

Thank you for the steering, my Mac OS is Yosemite 10.10.5.

That control flow statement you wrote, how would I nest that in the code. appreciated.

Your Welcome. Then your ls should know -S

I would suggest at the end within the

if cd /Users/christopherdorman/desktop

block

1 Like

Thank you for that steering. that worked lovely. regards