I'm a student and as part of my cursus I must recode the ls
command 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 ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With