Bash – How to sort these directory names numerically

awkbashsedsort

I'm aware that I can somehow sort this output numerically (so cpu1/ follows cpu0/) … I could probably get something to work eventually by splitting up the string various ways with awk, but is there an "easy" way that would be somewhat reusable in the future in other scripts?

> for i in /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq; do echo -n "$i: "; cat $i; done
/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq: 2395000
/sys/devices/system/cpu/cpu10/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu11/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu12/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu13/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu14/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu15/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu16/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu17/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu18/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu19/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu20/cpufreq/scaling_cur_freq: 1064000

Best Answer

The "version sort" seems to work fine with this.

for i in /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq; 
   do echo -n "$i: "; cat $i; done | sort -V