untitled folder 2 untitled folder 20 untitled folder 21 ... untitled folder 3 untitled folder 30 ...The Finder accounts for the numbers when it sorts in, say, column view, but ls doesn't. Output is sorted in a dictionary-style listing (lexicographical sorting), and can be fairly annoying or useless in some situations. The OS 7 Finder suffered from this problem, too, for those who remember it (an INIT called "Natural Order" solved it). Read the rest of the hint for the solution...
With the sort utility, we can get things looking nice again:
ls | sort -n +2 untitled folder 2 untitled folder 3 untitled folder 20 untitled folder 21 ...Woot! You'll noticed I used +2. A quick glance in the man pages for sort reveals:
A position has the form f.c, where f is the number of the field to use and c is the number of the first character from the beginning of the field (for +pos) or from the end of the previous field (for -pos).It's fairly easy to understand, but for some reason, well, this is where the Really Hardcore Unix Gurus have to lend additional explanation. The first field is field 0, the second is 1, etc. So to sort on the eighth character of the second field, you'd use sort -n +1.8.
In ideal situations, you could just name your files and folders with leading zeros (001, 010, 100), but this is a great solution to the problem if you can't do that or don't want to for other reasons. Also note that ls -n doesn't work unless the file or folder name starts with a number.
An example of how folder and file name formats can alter how you execute the command can be seen like this:
[thermodynamics:~/Desktop] mikey% ls *folder | sort -n +0.8 untitled3folder: untitled5folder: untitled20folder: untitled40 folder:If any of you Unix gurus can provide more detail on sort, please do so!

