Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using the following command to list files including those in sub-directories ordered by name.

dir /b/s/on

The files are correctly ordered within their given directories but the directories are not ordered. The directories appear to be ordered by last modified date.

Is dir able to also sort directories?

Here's a screenshot of a segment of the output (obfuscated):

enter image description here

share|improve this question
That commands displays sub-dirs and files all intermixed and sorted alphabetically for me. – Karan Dec 24 '12 at 16:32
@Ofiris I'm using FAT32 and NTFS on W7. – Paul Fleming Dec 24 '12 at 16:35
Ok , noticed the tag now, ':' is indeed optional. – Ofiris Dec 24 '12 at 16:42
Is the obfuscated string exactly the same for all lines listed above in the screenshot? – Karan Dec 24 '12 at 16:54
@Karan. Yes, the obfuscated directory name after the drive letter is the same for all entries. It's actually inside that directory that I'm running the dir command. – Paul Fleming Dec 24 '12 at 17:34
up vote 3 down vote accepted

dir is tree-walking the directory but only sorting entries in the leaf directories. This is a problem because it's a FAT filesystem. On a FAT filesystem, the entries in any given directory are unsorted, causing the tree-walk to be unsorted. NTFS directories, by contrast, are always sorted.

You have a couple solutions. The easiest would be to pipe the output through sort:

dir /b/s | sort

(If you're using sort to do the ordering, there's no need for the /on option to dir.)

Your other alternative would be to use a Unix-style ls, which certainly would get this right. Examples would be the ls in either Cygwin or my own Hamilton C shell.

share|improve this answer
Can you demonstrate DIR with the sort command? – Paul Fleming Dec 24 '12 at 17:33
Sure. Added. Hope that helps. – Nicole Hamilton Dec 24 '12 at 17:51
dir is tree-walking the directory but only sorting entries in the leaf directories. I don’t follow, are you saying that the directories in the current directory are not sorted? I just tried it in a folder with lots of files and folders on a FAT32 drive and it worked correctly from the CWD, down. – Synetech Dec 25 '12 at 15:41
FAT and FAT32 directory entries are unordered, unlike NTFS. They're in the order in which they were added. (If you copy a directory from NTFS, they will be ordered.) In the old DOS days, people used to use directory sorters to sort the entries on disk so they'd be in order when listed. – Nicole Hamilton Dec 25 '12 at 15:58
Yes, I know all that, but it does not answer the question. Your answer implies that dir /o* does not sort subdirectories in the CWD which does not seem to be the case (and would be very odd anyway). – Synetech Dec 25 '12 at 17:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.