Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any established order for 'ls' arguments?

I'm a student and as part of my cursus I must recode the lscommand and reproduce its behaviour the best possible.

On Mac (El Capitan 10.11.6), using the terminal iTerm 2(zsh), I get:

user> ls . -R
ls: -R: No such file or directory

And on Arch (latest version), using the default text interface(bash) I get:

user> ls . -R
<current directory content>

Although I'd rather trust Arch, is it correct for ls to refuse its option after a directory has been specified ? And is there any documentation declaring the arguments order ?

like image 334
Meri The'Badger Avatar asked Jul 26 '17 21:07

Meri The'Badger


1 Answers

See POSIX utility syntax guidelines, entry #9:

Guideline 9:

All options should precede operands on the command line.

Thus, the usage that POSIX guarantees will be supported is ls -R ., as -R is an option, and . is an operand.

GNU tools don't generally enforce this guideline (which is why Arch lets you put your arguments in the opposite order) -- but for maximum portability it's wise to write your software assuming it to be enforced.

If you want to be certain that -R will be treated as an operand rather than an argument, whatever your platform, you can use -- to separate the two categories: ls -l -- -R will treat -l as an option and -R as an operand, and thus will look for a file or directory named -R and provide a long-format listing; this is defined by POSIX utility syntax guideline #10.

like image 184
Charles Duffy Avatar answered Nov 15 '22 09:11

Charles Duffy