1

I have the following files

$ ls *.md | sort -n
1.pythonPrimer.md
10.mapsHashTablesAndSkipLists.md
11.searchTree.md
12.sortAndSelection.md
13.textProcessing.md
14.graphAlgorithms.md
15.memoryManagementAndBTrees.md
16.Appendix.md
2.Object Oriented Programming.md
3.algorithmsAnalysis.md
4.recursion.md
5.arrayBasedSequence.md
6.stack.md
7.linkedList.md
8.tree.md
9.priorityQueues.md

I want to sort them by the leading numbers in the order 1, 2, 3, 4....,

How could I get it done?

  • 1
    That should work exactly as you wrote it. Maybe your locale settings change the numeric sort order, or you have ls or sort aliased to something weird. Please edit to add the output of locale and type -a ls sort. – wjandrea Dec 3 '18 at 19:57
2

It works for me too. I think it will work for you, if you define a key -k and field separator -t for sort

ls *.md | sort -n -t. -k1

or with find

find * -name "*.md" | sort -n -t. -k1

You should avoid using ls like this in command lines. Things can go wrong for several reasons. See this link (particularly the links in the comments by @Byte Commander).

1

It works for me. Maybe you need to turn off locale?

ls *.md | LC_ALL=C sort -n

Or, use the version sorting in ls:

ls -v *.md

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.