0

When providing a list of files as arguments to ls, should ls list them in the same order as they are specified as arguments?

For example, why does the third command list the files in the same order as the fourth command ls -l, but not in the same order of first the output of the first command, and then the output of the second command?

$ ls \[Untitled\]_?-*  -l
-rw-rw-r-- 1 t t 1867153 Dec  1 23:02 [Untitled]_1-18.pdf
-rw-rw-r-- 1 t t 1104215 Dec  1 23:03 [Untitled]_2-18.pdf
-rw-rw-r-- 1 t t 1287357 Dec  1 23:03 [Untitled]_3-18.pdf
-rw-rw-r-- 1 t t 1015360 Dec  1 23:03 [Untitled]_4-18.pdf
-rw-rw-r-- 1 t t 1133440 Dec  1 23:04 [Untitled]_5-18.pdf
-rw-rw-r-- 1 t t 1230646 Dec  1 23:04 [Untitled]_6-18.pdf
-rw-rw-r-- 1 t t 1070296 Dec  1 23:04 [Untitled]_7-18.pdf
-rw-rw-r-- 1 t t 1248518 Dec  1 23:04 [Untitled]_8-18.pdf
-rw-rw-r-- 1 t t 1096371 Dec  1 23:04 [Untitled]_9-18.pdf

$ ls  \[Untitled\]_??-*   -l
-rw-rw-r-- 1 t t 1310164 Dec  1 23:05 [Untitled]_10-18.pdf
-rw-rw-r-- 1 t t 2031810 Dec  1 23:05 [Untitled]_11-18.pdf
-rw-rw-r-- 1 t t 1043840 Dec  1 23:05 [Untitled]_12-18.pdf
-rw-rw-r-- 1 t t 1180514 Dec  1 23:05 [Untitled]_13-18.pdf
-rw-rw-r-- 1 t t 2058009 Dec  1 23:05 [Untitled]_14-18.pdf
-rw-rw-r-- 1 t t 2075104 Dec  1 23:06 [Untitled]_15-18.pdf
-rw-rw-r-- 1 t t 2092823 Dec  1 23:06 [Untitled]_16-18.pdf
-rw-rw-r-- 1 t t 2015672 Dec  1 23:06 [Untitled]_17-18.pdf
-rw-rw-r-- 1 t t 1929560 Dec  1 23:06 [Untitled]_18-18.pdf

$ ls \[Untitled\]_?-* \[Untitled\]_?\
?-*   -l
-rw-rw-r-- 1 t t 1310164 Dec  1 23:05 [Untitled]_10-18.pdf
-rw-rw-r-- 1 t t 2031810 Dec  1 23:05 [Untitled]_11-18.pdf
-rw-rw-r-- 1 t t 1867153 Dec  1 23:02 [Untitled]_1-18.pdf
-rw-rw-r-- 1 t t 1043840 Dec  1 23:05 [Untitled]_12-18.pdf
-rw-rw-r-- 1 t t 1180514 Dec  1 23:05 [Untitled]_13-18.pdf
-rw-rw-r-- 1 t t 2058009 Dec  1 23:05 [Untitled]_14-18.pdf
-rw-rw-r-- 1 t t 2075104 Dec  1 23:06 [Untitled]_15-18.pdf
-rw-rw-r-- 1 t t 2092823 Dec  1 23:06 [Untitled]_16-18.pdf
-rw-rw-r-- 1 t t 2015672 Dec  1 23:06 [Untitled]_17-18.pdf
-rw-rw-r-- 1 t t 1929560 Dec  1 23:06 [Untitled]_18-18.pdf
-rw-rw-r-- 1 t t 1104215 Dec  1 23:03 [Untitled]_2-18.pdf
-rw-rw-r-- 1 t t 1287357 Dec  1 23:03 [Untitled]_3-18.pdf
-rw-rw-r-- 1 t t 1015360 Dec  1 23:03 [Untitled]_4-18.pdf
-rw-rw-r-- 1 t t 1133440 Dec  1 23:04 [Untitled]_5-18.pdf
-rw-rw-r-- 1 t t 1230646 Dec  1 23:04 [Untitled]_6-18.pdf
-rw-rw-r-- 1 t t 1070296 Dec  1 23:04 [Untitled]_7-18.pdf
-rw-rw-r-- 1 t t 1248518 Dec  1 23:04 [Untitled]_8-18.pdf
-rw-rw-r-- 1 t t 1096371 Dec  1 23:04 [Untitled]_9-18.pdf

$ ls -l
total 26268
-rw-rw-r-- 1 t t 1310164 Dec  1 23:05 [Untitled]_10-18.pdf
-rw-rw-r-- 1 t t 2031810 Dec  1 23:05 [Untitled]_11-18.pdf
-rw-rw-r-- 1 t t 1867153 Dec  1 23:02 [Untitled]_1-18.pdf
-rw-rw-r-- 1 t t 1043840 Dec  1 23:05 [Untitled]_12-18.pdf
-rw-rw-r-- 1 t t 1180514 Dec  1 23:05 [Untitled]_13-18.pdf
-rw-rw-r-- 1 t t 2058009 Dec  1 23:05 [Untitled]_14-18.pdf
-rw-rw-r-- 1 t t 2075104 Dec  1 23:06 [Untitled]_15-18.pdf
-rw-rw-r-- 1 t t 2092823 Dec  1 23:06 [Untitled]_16-18.pdf
-rw-rw-r-- 1 t t 2015672 Dec  1 23:06 [Untitled]_17-18.pdf
-rw-rw-r-- 1 t t 1929560 Dec  1 23:06 [Untitled]_18-18.pdf
-rw-rw-r-- 1 t t 1104215 Dec  1 23:03 [Untitled]_2-18.pdf
-rw-rw-r-- 1 t t 1287357 Dec  1 23:03 [Untitled]_3-18.pdf
-rw-rw-r-- 1 t t 1015360 Dec  1 23:03 [Untitled]_4-18.pdf
-rw-rw-r-- 1 t t 1133440 Dec  1 23:04 [Untitled]_5-18.pdf
-rw-rw-r-- 1 t t 1230646 Dec  1 23:04 [Untitled]_6-18.pdf
-rw-rw-r-- 1 t t 1070296 Dec  1 23:04 [Untitled]_7-18.pdf
-rw-rw-r-- 1 t t 1248518 Dec  1 23:04 [Untitled]_8-18.pdf
-rw-rw-r-- 1 t t 1096371 Dec  1 23:04 [Untitled]_9-18.pdf

Thanks.

3

ls always sorts its output. POSIX defines that it

shall sort directory and non-directory operands separately according to the collating sequence in the current locale.

This will generally result in numbers within names being sorted lexicographically, as in your examples, but the overall order depends on the rules specified in your locale.

The same result will be achieved from specifying files on the command line and naming a directory that contains exactly those files.

  • 1
    ... unless the -f option is given (this isn't fully specified in POSIX; GNU ls -f disables sorting entirely). – Stephen Kitt Dec 2 '16 at 7:59
  • Thanks. Why are these files listed in the order of [Untitled]_10-18.pdf [Untitled]_11-18.pdf [Untitled]_1-18.pdf [Untitled]_12-18.pdf? – Tim Dec 5 '16 at 4:55
  • Because that's the collation order your locale defines, presumably. I don't know how it comes to that decision. It does seem a strange choice. – Michael Homer Dec 5 '16 at 6:40
  • How can I find out my local? – Tim Dec 5 '16 at 14:44
  • @Tim, locale, not local. See the output of locale. That sorting order where the - is ignored in the first pass is typical iso14651 rule. On a GNU system, see /usr/share/i18n/locales/iso14651_t1_common – Stéphane Chazelas Apr 17 at 6:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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