# Why does ls ignore command line argument order?



## FKEinternet (May 31, 2018)

I'm using `ls -l` in a `tcsh` shell to get information about a file and a symbolic link.  Independent of the order they are specified on the command line, the file is always listed first.  Why is this? i.e., why do these two commands produce the same output?


```
# ls -l jquery.dataTables.js /web/include/jquery.dataTables.js
-rw-r--r--  1 fkeinternet  fkeinternet  447282 Jul 26  2016 /web/include/jquery.dataTables.js
lrwxr-xr-x  1 fkeinternet  fkeinternet      33 May 31 01:29 jquery.dataTables.js -> /web/include/jquery.dataTables.js

# ls -l /web/include/jquery.dataTables.js jquery.dataTables.js
-rw-r--r--  1 fkeinternet  fkeinternet  447282 Jul 26  2016 /web/include/jquery.dataTables.js
lrwxr-xr-x  1 fkeinternet  fkeinternet      33 May 31 01:29 jquery.dataTables.js -> /web/include/jquery.dataTables.js
```

Operating environment:

```
# pwd
/web/<client_directory>/www/include

# uname -a
FreeBSD Dreamer 11.1-RELEASE-p4 FreeBSD 11.1-RELEASE-p4 #0: Tue Nov 14 06:12:40 UTC 2017     root@amd64-builder.daemonology.net:/usr/obj/usr/src/sys/GENERIC  amd64
# tcsh --version
tcsh 6.20.00 (Astron) 2016-11-24 (x86_64-amd-FreeBSD) options wide,nls,dl,al,kan,sm,rh,color,filec
```


----------



## tobik@ (May 31, 2018)

FKEinternet said:


> Why is this? i.e., why do these two commands produce the same output?


Why does it matter?

Anyway the explanation is in the man page:


> If more than one operand is given, non-directory operands are displayed first; directory and non-directory operands are sorted separately and in lexicographical order.


----------



## SirDice (May 31, 2018)

Because the output is sorted alphabetically by default.


----------



## ralphbsz (May 31, 2018)

From the output of "man ls": If no operands are given, the contents of the current directory are displayed.  If more than one operand is given, non-directory operands are displayed first; directory and non-directory operands are sorted separately and in lexicographical order.

Look at the option "-f", it turns sorting off.


----------



## FKEinternet (May 31, 2018)

tobik@ said:


> Why does it matter?



Because I'd like to understand what's going on, if for no other reason.  I was in the middle of trying to figure out some other odd behaviour and found this confusing when it popped up:  I expected the results to be in the order I entered the filespecs.



> Anyway the explanation is in the man page:



I have to admit, I didn't first look at the man page, I did a Google search for "freebsd ls ignore command line argument order" which didn't yield any useful results.  Clearly I was looking in the wrong place, perhaps if I wasn't so busy trying to figure out why two symlinks originating and ending in the same directories (different files) behaved differently I would have thought to look at the man page.

Thanks all for the RTFM reminder!!


----------



## SirDice (May 31, 2018)

FKEinternet said:


> I did a Google search for "freebsd ls ignore command line argument order"


Note that this isn't specific to FreeBSD, Linux's ls(1) command behaves exactly the same. While the Linux's man page is somewhat limited they're quite clear about this:

```
Sort entries alphabetically if none of -cftuSUX nor --sort.
```
https://www.freebsd.org/cgi/man.cgi...d+Hat+Linux/i386+7.3&arch=default&format=html


----------



## hrenznaet (Jun 2, 2018)

ralphbsz said:


> Look at the option "-f", it turns sorting off.


But for some reason it also enables `-a`, which I'd call a bug.
But, irregardless of common sense, of course it's not a bug if it's mentioned in the docs.


----------



## ralphbsz (Jun 3, 2018)

Actually, it's worse than that: I think the -f and -a flags (and their side effects) are defined by the Posix standard.

Actually, if you want the functionality of the `ls` command without the "-l" flag and without sorting, you can just use echo and command line globbing: "echo c* b* a*" in a directory that contains file adam, bella, bob and charlie would just output the words "charlie bella bob adam", which might be closer to what you want.


----------

