0
[xxx]$ mkdir dir{1..10}
[xxx]$ ls
dir1  dir10  dir2  dir3  dir4  dir5  dir6  dir7  dir8  dir9

When I make 10 directories like this, why does dir10 appear right after dir1? Shouldn't it be the last directory?

Also, if I try the following command:

[xxx]$ mkdir -v dir{1..10}
mkdir: created directory 'dir1'
mkdir: created directory 'dir2'
mkdir: created directory 'dir3'
mkdir: created directory 'dir4'
mkdir: created directory 'dir5'
mkdir: created directory 'dir6'
mkdir: created directory 'dir7'
mkdir: created directory 'dir8'
mkdir: created directory 'dir9'
mkdir: created directory 'dir10'
[xxx]$ ls
dir1  dir10  dir2  dir3  dir4  dir5  dir6  dir7  dir8  dir9

It seems like now directories are created in correct order, but still when I list them, directory 10 appears to be second again.

How can I make directory 10 to appear after directory 9? Is there some way to sort these directories?

Kusalananda
  • 333,661

1 Answers1

5

Because ls sorts files alphabetically. You can list the files with ls -v (in linux) if you want them to be sorted in numerical order

-v natural sort of (version) numbers within text

binarysta
  • 3,032