3

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

14
  • That commands displays sub-dirs and files all intermixed and sorted alphabetically for me.
    – Karan
    Dec 24, 2012 at 16:32
  • @Ofiris I'm using FAT32 and NTFS on W7. Dec 24, 2012 at 16:35
  • Ok , noticed the tag now, ':' is indeed optional.
    – Ofiris
    Dec 24, 2012 at 16:42
  • Is the obfuscated string exactly the same for all lines listed above in the screenshot?
    – Karan
    Dec 24, 2012 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. Dec 24, 2012 at 17:34

1 Answer 1

6

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.

6

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .