-1

I created 5 files in Debian Version 9(Stretch) like below: touch a touch b touch c touch d touch e

When i did "ls -f" then output is "d a b c e . .."

Note: the output order in debian is in zigzag order, neither ascending nor descending.

Similar test i did in RHEL7, the outputs are like below: "ls -f" then output is ". .. a b c d e"

Can you suggest me how to make the output as ". .. a b c d e" in debian when "ls -f" is performed over debian.

Thanks in advance.

syam
  • 47
  • 2

2 Answers2

1

In my case, ls -f writes:

b   c   a   d   e

ls -f does not sort what it finds. If someone wants to have the list sorted, he can use ls, or ls -f | sort, or whatever is better for the concrete problem.

Asensi
  • 159
  • 7
  • Thanks for the reply, using sort won't help in solving the problem. At least I want to know on what basis the order "d a b c e . .. " is arrived up on, So that I can write the logic accordingly in my code. – syam Aug 22 '19 at 08:01
  • In fact why different outputs are given for the same command "ls -f" between debian & rhel – syam Aug 22 '19 at 08:02
  • The _"Can you suggest me how to make the output as ". .. a b c d e" in debian when "ls -f" is performed over debian"_ question was answered. Another subject is that when you save a file in a device, its particular place in that device is not guaranteed, therefore your programs (like `ls -f`) can find a file before they find another one. Sorting the output of `ls -f`, or using `ls` will show files in an expected way. – Asensi Aug 22 '19 at 08:20
  • Hi! The standard procedure in StackOverflow is to upvote (or accept the answer clicking on the ✓ that the answer has), and create another question if you have another one (where you can put a link to the present one). – Asensi Aug 24 '19 at 10:05
0

ls -f give you the file in disk order, so unsorted.

If you want to have a b c d e with ls -f, you need to know which filesystem do you use, and check how filesystems store file names in directories. There is no generic rules.

One less advanced filesystem, the order you create the file is the order you get with ls -f. It is possible that kernel cache will change the order on next call.

So: the order is a internal API, which is not supposed that we should know, and it is supposed we assume nothing about order. Kernel and filesystem are free to change order at any time, maybe during an internal optimisation run.

ls -f exists because it doesn't store filenames, and it doesn't order them, so it is faster and less resource intensive (on very very large directories).

Giacomo Catenazzi
  • 8,519
  • 2
  • 24
  • 32