2

I use an underscore at the beginning of file and folder names to mark them as something "special". But this sorting doesn't work for the most programs under Linux. I tried LC_COLLATE=C ls and other locales but the results are something strange. Let's say I have these folders:

2
A
_F
G
K
S
x4

This is the sort order of ls. When I use LC_COLLATE=C ls, I have this:

2
A
G
K
S
_F
x4

I have this under Xubuntu 18.04 using the xfce-terminal-emulator on 2 machines.

What is wrong here?

  • 3
    How do you expect them to sort? – George Udosen Dec 22 '18 at 7:30
  • 1
    @GeorgeUdosen I think OP wants the special characters to placed first/ higher in list. – Sourav Ghosh Dec 22 '18 at 7:32
  • 1
    Even at that we are still guessing as OPs results could be any thing! – George Udosen Dec 22 '18 at 7:41
  • i want to sort the files/folders like dolphin (kde file manager) sorts them. all folder first, the all files. folders with underscore at top of al folders, the same with files with underscore at the beginning. – fmeier Jan 1 at 17:23
1

You can sort files with leading underscores to the end with --sort=version or -v which is equivalent to sort -V (natural version sort) (which is most useful for sorting files with numbers).

$ ls -v1
2
A
G
K
S
x4
_F

Details on version sorting are in info ls... note that:

‘LC_COLLATE’ is ignored, which means ‘ls -v’ and ‘sort -V’ will
 sort non-numeric prefixes as if the ‘LC_COLLATE’ locale category
 was set to ‘C’.

Perhaps this is adequate for your purpose.

  • 1
    Just to mention that Dolphin (Kubuntu's file manager) sorts them, from top to bottom, like this: _F, 2, A, G, K, S, x4. It's using something called "Natural" sorting. I have a question on it here: askubuntu.com/questions/1040499/… – DK Bose Dec 22 '18 at 9:57
  • 1
    Cleaned up my answer. – DK Bose Dec 23 '18 at 6:52
1

@Zanna's answer provides one solution. Another solution is this:

$ ls -lfr -1 _* [!_]*
_F
2
A
G
K
S
x4
  • This shows also all sub folders and files that are stored in the folder. – fmeier Jan 1 at 17:24

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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