Linux Commands Structure

All the commands that we type in the terminal, follow a similar type of structure in the way that you type them. By understanding the structure, you will be able to transition from simply just memorizing commands to actually understanding the common language that all Linux commands use. 

All the commands are effectively just little computer programs that are installed somewhere on your computer. For example, date is a program, cal is a program and echo is another program. The command name that you want to use is the first part of the command. Each command has its own unique behavior that we can look up using Manual Pages. Below is the general command structure:
commandName -> options -> input

Shell’s Path

Once the shell knows the name of the program you want to run, it then searches for that program on something called Shell’s Path. Shell’s path is just a path to a list of folders that contain programs. You can actually see your Shell’s path by:
commandName + $PATH. So if you type echo $PATH, it will give you  different folder paths separated by a colon. So when you enter 

So when you enter a command, what shell would do is that it would start at the very left of the path and look inside that folder for a command echo or cal or whatever you called it. If it doesn’t find it there, it will move to the next one. If the shell didn’t find a folder with that name in any of those folders, the shell would give you an error “command not found“. If you want to find exactly in which fold a command exists, you can use which command. So for example, if you enter which echo, you will get the exact folder for the echo command. This is how your shell knows where to find your command. 

Command’s Input

We can customize the way the commands work by giving them different options and input. Let’s start with input! Not all the commands require inputs but most commands do require some form of input which can actually have a fancy name called an operand. To add an input to a command you have to enter the name of the command + space and then type the input. And that’s assuming you don’t have any option.  For example, for the cal command, you can just enter cal and get the current month with the current date highlighted. Or you can modify its behavior by typing an input. You can do cal 2022 and it would give the whole 2022 calendar with the current date highlighted. 
You can also give more than one input. For example, for the call command, you can do cal 12 2022. It would show you the 12th month of 2022

Command’s options

Let’s talk about the cal command, we can give it a ‘y‘ option which results in showing the calendar for the current year. (cal -y). Let’s look at the date command to understand options better. The date command itself gives the date and time in the current time zone which for me is Mountain Time. 

If we want to see the date in universal time we can use the option ‘u

If we want to give a command multiple options, they would be one after another. So for example if we want to give the date command option a, b, c, d we would type date -a -b- c- -d. We can also simplify it by doing date -abcd. 

Sometimes options can have their own input. Let’s take the command cal as an example. -A option requires input after it. So if we type cal -A 1, it would show us the current month with current date highlighted and 1 month after it as well. 

The cal command has another option which is -B, which means one month before current month. So if we do cal -B 1. It would show you the current month with current date highlighted and last month. 

So an option sometimes needs to have an input for the command cal, -A means number of months after and -B means number of months before. And these options need input. We can also do Command + options + optionsInput + commandInput. Let’s type the following command into your terminal:
cal -A 2 12 2017

So we are giving the cal command -A with input 2, which means that shows 2 months after. We are also giving it the inputs to show us the 12th month of the year 2017. So in short we are asking to get the calendar of month December 2017 and 2 months after it. 

  • Command = command-name -options inputs
  • command-names need to be on the shell’s search path
  • commands operate on inputs.
  • options modify a command’s behaviour.