Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a directory with files named "1", "2",...,"9", "10", "11", ..., "20", "21", ... inside.

Can I list them in the order of "1", "2",...,"9", "10", "11", ..., "20", "21", ...?

I have tried ls -ls. It doesn't list the files that way, but instead "1", "10", "11", ..., "19", "2", "20", "21", .... I guess a feature like that will be helpful sometimes.

share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted

With GNU ls (as found on most Linux distributions), you can use ls -v, where -v, from the man page

 -v     natural sort of (version) numbers within text
share|improve this answer
add comment

GNU ls -v has already been mentioned, but for other commands, if your shell is zsh, you can affect the order wildcards are expanded with globbing qualifiers.

For instance:

printf '%s\n' *(om)

Would list the files sorted by modification time (from the newest to the oldest), *(Om) to reverse the sort order (from the oldest to the newest).

*(n) would sort the files numerically. So if your ls is not the GNU ls, you could still do:

ls -fdl -- *(n)

To get the list numerically sorted.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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