Key Takeaways
- Linux commands provide usage info on how to use them, including the options and parameters they support.
- Put “man” in front of a command for reliable info on its usage.
- Usage syntax typically includes the command’s name followed by its short options, long-form options, and parameters.
The command line can be a daunting place to hang out in, even if you’re familiar with a few commands. However, almost every Linux command can tell you how to use it—if you know how to ask.
What Does the Command Usage Tell Me?
Some Linux commands do something useful all on their own. For example, pwd prints the working directory:
Other commands need more information. Take rmdir, the command to delete a directory; it needs to know which directory to delete. You can also run rmdir in different modes. For example, to report each directory as it’s removed, run rmdir -v:
Linux calls everything after the command’s name an operand. Letters that begin with a “-” and words that start with “–” are called options and they usually alter the command’s behavior. Words after options are parameters that pass data to the command.
A command’s usage will tell you which options it supports and which parameters you can pass it. It also explains which of these are optional and how you can combine them.
How Do I See Usage for a Command?
The most reliable way of finding a command’s usage is with the man command. Most commands will tell you their usage in a “SYNOPSIS” section at the top of their manual. Take the which command as an example:
Here, the usage reads:
which [-as] filename ...
Most commands will also tell you their usage on error, including if you run them incorrectly. Commands that require parameters to do something useful will usually give usage if you run them without any:
In this case, the grep command prints its usage because it needs at least one regular expression pattern to take any useful action.
Finally, there are a few commands that take an action without any parameters but don’t have a man page, so neither of these approaches works. With luck, however, such a command will support a –help option so that you can see usage like this:
The cd command prints its usage on the first line of output when you call it with the “–help” option.
Each command is different and even the same command can behave differently across systems. For example, mkdir will report its usage on macOS but you’ll need to run mkdir –help or man mkdir on Linux to view it.
An Explanation of Usage Syntax
Once you have a command’s usage, you’ll need to understand it. There are a few separate parts, and some symbols that explain how everything fits together. Commands can, in theory, explain their usage however they want, but these conventions are broadly observed, particularly by the most common commands.
Usage usually includes some, or all, of the following:
- The name of the command at the beginning.
- Single-letter options come next, usually in square brackets ([ and ]) to denote they are optional. They are usually shown together because you can write them that way as a shorthand—”ls -lrt” is the same as “ls -l -r -t”.
- Options that take arguments and long-form options follow. Long-form options are full words beginning with a double-dash (–). Some long-form options may have an optional argument so, for example, “[–color[=when]]” means that –color is optional and, when present, it can stand alone or be followed by an equals sign (=) and a value referred to with the name “when.”
- Finally, parameters depend on the nature of the tool. They will often be files and you might see “file …” which means “one file or more separated by spaces.”
Take the GNU alias command as an example. Its usage is:
alias: alias [-p] [name[=value] ... ]
This usage indicates that alias accepts one short option (p) and any number of name/value pairs. So you can run it in any of these forms:
- alias
- alias -p commit
- alias commit=”git commit” add=”git add”
Meanwhile, usage for the BSD version of mkdir (as used by macOS) looks like this:
usage: mkdir [-pv] [-m mode] directory_name ...
This usage shows that mkdir supports two standalone short options (p and v) and a short option, m, that requires an argument. The command requires at least one directory name, but can accept more than one. So these forms are all valid:
- mkdir docs
- mkdir one two three
- mkdir -p src
- mkdir -v -p src
- mkdir -vp -m 755 one two
I’ll leave you with a parting note, though: Some modern GNU tools simplify their usage e.g. “ls [OPTION]… [FILE]…” In this form, you’ll need to read further into the manual to see exactly which options the command supports and how they work.