3. Command options
Let's see some options of the command ls
.
-
List only some files:
ls /bin
ls /bin/b* /bin/c*
We are listing only those files that start with
b
and those that start withc
, on the directory/bin
. -
Long listing:
ls -l /bin/b* /bin/c*
The option
-l
stands for long listing, where each file is printed on its own line, with more details. -
Long and short options.
Notice that the middle column shows the size of the file (in bytes). To make the size more readable we can use the option
--human-readable
:ls -l --human-readable /bin/b* /bin/c*
Instead of this long option we can use its short equivalent
-h
, which is more convenient to write:ls -l -h /bin/b* /bin/c*
Tip: In order to modify the previous command, you can use the up-arrow key on the keyboard to display the previous command, use left-arrow and right-arrow keys to locate the cursor, modify the command, and then press [Enter].
-
Merging short options.
We can also merge the short options like this:
ls -lh /bin/b* /bin/c*
By default files are listed alphabetically, but we can sort them by modification time, using the option
-t
:ls -lht /bin/b* /bin/c*
-
With the option
--reverse
or-r
we can reverse the order of display:ls -lt --reverse /bin/b* /bin/c*
ls -lh --reverse /bin/b* /bin/c*
ls -ltr /bin/b* /bin/c*
ls -lhr /bin/b* /bin/c*
Usually the options have a long version (like --reverse
or
--human-readable
) and a short one (like -r
or -h
). But not
all of them. For example the options -l
or -t
don't have a long
version.
It seems like the short options are more convenient when writing commands. In your opinion, why do we have long options as well? Why they might be useful?