I know that these two commands do the same thing:
ls -la
ls -al
but I'm wondering if there is any preference between them. Is it better practice to order them in a certain way?
I know that these two commands do the same thing:
ls -la
ls -al
but I'm wondering if there is any preference between them. Is it better practice to order them in a certain way?
It does not really matter* unless a command explicitly mentions. I have came across no reference that gave anything like a guideline how to put the options one after another. So I think for the sake of readability the options can be arranged alphabetically.
*There is a very very important cavaet that is if any option needs a filename or any other input right after it you need to put it accordingly.
For example, the
-f
option ofgrep
takes a file as input to take the pattern to look for from the input file. You need to give the command as e.g.:If you use it like:
it will fail as it will use
c
as a file name.The POSIX conventions define in Guideline 11 and 12 of the Utility Syntax Guidelines that the order "should not matter" unless it's not documented for the utility itself.
The GNU C Library Reference Manual also honors those POSIX conventions in 25.1.1 Program Argument Syntax Conventions.
But notice, there are several utilities that give in some situations strict orders how to perform the parameters (for example find). From the find manpage:
Sometimes the utility complains about the ordering.
With ls you can hardly go wrong with ordering of options.
But be mindful though that other commands can have different responses to options depending on the order.
An example is the find command. You will get different results for example between:
And
If what you want to practice is alphabetizing short strings, perhaps. I find two strategies useful: making words (reduces typos) and making sentences (improves understanding).
For ls, for instance, I usually use
ls -halF
because, even with the strange capitalization, I am better at typing halF than something like Falh that isn't a word.For tar, on the other hand, I type
tar czvf filename
and say (in my head) "tar Create and Zip, Verbosely For filename" because that's how I learned what the different letters meant.Like others have answered, it doesn't matter (unless documented otherwise or option arguments take further arguments).
But is it preferable to order options alphabetically is debatable.
Personally, I find that mnemonics provide more value than strict alphabetic order. It is easier to remember a set of options when they form a memorable word, for example, consider:
vs:
So if you find yourself repeating a certain combo of options for a command, you may want to try and rearrange the letters to an order that is easiest for you to remember.