Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Possible Duplicate:
List files sorted numerically

How can I sort this according to the number of last field? I tried with sort -t ' ' -k9,10n:

-rw-r--r-- 1 root   root    440 May 14 08:52 test.txt.6
-rw-r--r-- 1 root   root    470 May 14 08:52 test.txt.8
-rw-r--r-- 1 root   root   2.6K May 14 08:52 test.txt.14
-rw-r--r-- 1 root   root    449 May 14 08:52 test.txt.7
-rw-r--r-- 1 root   root    434 May 14 08:52 test.txt.13
-rw-r--r-- 1 root   root    554 May 14 08:52 test.txt.4
-rw-r--r-- 1 root   root    426 May 14 08:52 test.txt.12
-rw-r--r-- 1 root   root   1.6K May 14 08:52 test.txt.5
-rw-r--r-- 1 root   root   7.2K May 14 08:52 test.txt.11
-rw-r--r-- 1 root   root    444 May 14 08:52 test.txt.3
-rw-r--r-- 1 root   root    927 May 14 08:52 test.txt.9
-rw-r--r-- 1 root   root    681 May 14 08:52 test.txt.15
-rw-r--r-- 1 root   root    427 May 14 08:52 test.txt.2
-rw-r--r-- 1 root   root    458 May 14 08:52 test.txt.16
-rw-r--r-- 1 root   root   2.4K May 14 08:52 test.txt.10
-rw-r--r-- 1 root   root    423 May 14 08:52 test.txt.17
-rw-r--r-- 1 root   root    424 May 14 08:52 test.txt.1
share|improve this question

marked as duplicate by l0b0, Mikel, manatwork, jasonwryan, Michael Mrozek May 15 '12 at 17:25

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Tried ls -v as suggested in List files sorted numerically? – manatwork May 14 '12 at 10:50
@manatwork: if you have the answer, why not post it as an answer? – ams May 14 '12 at 12:50

Well, part of the problem is that -t ' ' will not collapse the separators so your ninth column will be the 4-char sizes and empty on the three-chars. Leave it out and you get collapsed whitespace like you want. The other part of the problem is, as others have said, is that the fields are not numeric. Fortunately, they resemble version numbers closely enough you can use the version sort (-V) to order them. Further, if they are going to be contiguous (i.e. 1-17, not missing any), you can use the braces as I mentioned in the other thread you were directed to (test.txt.{1..17}).

ls -l | sort -Vk9

echo test.text.{1..17}
share|improve this answer

you can use the following command and you will get them sorted the way you want: ls -l -v

share|improve this answer

A common mistake using sort it to not to use the "-n" argument when sorting by numerical values.

share|improve this answer
@user18818 did specified the n. The problem is the given field is not numeric. – manatwork May 14 '12 at 12:37
how can i solve "space problem" – user18815 May 14 '12 at 13:43
@user18815, call NASA? Seriously, if this is a different question, create a new question. – glenn jackman May 14 '12 at 14:06

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