Command Line Basics
Tags: beginners, tutorial, terminal, ubuntu
Published: 2023-08-05 13:29:06

## The Prompt

When we open up our terminal, we'll see our prompt which will likely include your **username@machinename**, followed by a ~ and then a dollar sign. You may see this pattern or not depending on whether you are using Ubuntu or WSL(Windows Subsystem For Linux)

This prompt is what we'll see whenever the shell is ready to accept new input. All we need to do is type some commands and hit Enter

```bash
mustafa@Mustafa:$
```

## Our First Command!

The date command may not be the most useful command of all time, but it's a great place to start.

Try typing `date` and then hit enter. You should see the current date printed out!

Commands are case-sensitive, so `Date` is NOT the same thing as `date`.

If you're using OS X, some commands are not case-sensitive, but others are. It's safest to assume all commands are case-sensitive.

```bash
date
Fri Aug  4 16:06:37 PKT 2023
```

## Another Simple Command

Try typing `ncal` into your prompt. Hit enter and you should see the current month's calendar printed out.

`ncal` stands for "new cal". There is also a "cal" command that does the same thing that will print the calendar in a horizontal format, but `ncal` adds some fancier functionality.

You may need to install it using sudo `apt install ncal`

You can clear your terminal using the `clear command`

```bash
ncal
    August 2023       
Su     6 13 20 27   
Mo     7 14 21 28   
Tu  1  8 15 22 29   
We  2  9 16 23 30   
Th  3 10 17 24 31   
Fr  4 11 18 25      
Sa  5 12 19 26
```

## Use the Arrow Keys

In the terminal, we can use the left and right arrow keys to move through a line of text, one character at a time.

Use the up arrow to access previously entered commands, which can save you tons on typing!

## Command Structure

* Most commands support multiple options that modify their behavior. We can decide which options to include, if any when we execute a command.
    
* Similarly, many commands accept arguments (the things that the command acts upon or uses)
    
* Not every command operates this way but many do
    
    ```bash
    command -options arguments
    ```
    
    ### Arguments
    
    The terms "argument" and "parameter" are often used interchangeably to refer to values that we provide to commands.
    
    The `echo` command is extremely simple. It takes the arguments we provide to it and prints them out. It echoes them back at us.
    
    ```bash
    echo mustafa
    mustafa
    ```
    
    For eg, the ncal command accepts values to control the specific month(s) and the year it displays.
    
* If we specify only a year, ncal will print out the calendar for that entire year.
    
* If we specify a month and a year, ncal will print only that month's calendar.
    
    ```bash
    ncal 2021
    ncal july 2021
    ```
    
    If we have a text file named `colors.txt` which contains the names of colors, then executing the command below will print the colors after sorting them in ascending order
    
    Keep in mind that `colors.txt` should be in the same directory as your present working directory. You can find your present working directory by executing `pwd`
    
    ```bash
    sort colors.txt
    blue
    green
    indigo
    orange
    red
    violet
    yellow
    ```
    
    ### Options
    
* Each command typically supports a host of options that we can choose to use when executing the command. These options modify the behavior of the command in predefined ways.
    
* Options are prefixed by a dash, as in -h or -3.
    
    ```bash
    command -option
    ```
    
* We can pass several options to `ncal`
    
    * By default, the ncal command highlights today's date in the output. However, if we want to remove the highlighting we can execute `ncal -h`
        
* The **\-j option** tells `ncal` to display a calendar using Julian days (days are numbered starting from jan 1st)
    
    ```bash
    ncal -j
        August 2023
    Su     218 225 232 239    
    Mo     219 226 233 240    
    Tu 213 220 227 234 241    
    We 214 221 228 235 242    
    Th 215 222 229 236 243    
    Fr 216 223 230 237        
    Sa 217 224 231 238
    ```
    
* The -3 option tells `ncal` to display the previous, current, and next month
    
    ```bash
    ncal -3
        July 2023         August 2023       September 2023    
    Su     2  9 16 23 30     6 13 20 27        3 10 17 24   
    Mo     3 10 17 24 31     7 14 21 28        4 11 18 25   
    Tu     4 11 18 25     1  8 15 22 29        5 12 19 26   
    We     5 12 19 26     2  9 16 23 30        6 13 20 27   
    Th     6 13 20 27     3 10 17 24 31        7 14 21 28   
    Fr     7 14 21 28     4 11 18 25        1  8 15 22 29   
    Sa  1  8 15 22 29     5 12 19 26        2  9 16 23 30
    ```
    
    ### Combining Options
    
* We can provide multiple options at once. This example uses the -3 option to display the previous, current, and next month AND the -h option to turn off the highlighting of the current date.
    
* When we provide multiple options to a single command, we can use a shorter syntax where we only need a single dash (-) character and we chain the options together
    
* So executing either `ncal -3 -j` or `ncal -3j` would give the same result which would be the last 3 months in Julian days
    
    ```bash
    ncal -3j
        July                    August                  September
    Su     183 190 197 204 211     218 225 232 239         246 253 260 267    
    Mo     184 191 198 205 212     219 226 233 240         247 254 261 268    
    Tu     185 192 199 206     213 220 227 234 241         248 255 262 269    
    We     186 193 200 207     214 221 228 235 242         249 256 263 270    
    Th     187 194 201 208     215 222 229 236 243         250 257 264 271    
    Fr     188 195 202 209     216 223 230 237         244 251 258 265 272    
    Sa 182 189 196 203 210     217 224 231 238         245 252 259 266 273
    ```
    
    ### Long Form Options
    
* Some options also support equivalent long format options that are usually full words and are prefixed with two dashes instead of just one.
    
* For example, the `date -u` option is used to print the date in Coordinated Universal Time (UTC). We can instead use `date --universal` to accomplish the same result.
    
    ```bash
    date -u
    date --universal
    Sat Aug  5 02:18:04 UTC 2023
    ```
    
* The `sort -r` option will sort a file's contents in reverse. If we prefer, we can use the longer form `sort --reverse` to accomplish the same thing.
    

* Not every command operates this way but many do
    
    ```bash
    command -options arguments
    ```
    
    ### Arguments
    
    * The terms "argument" and "parameter" are often used interchangeably to refer to values that we provide to commands.
        
    * The `echo` command is extremely simple. It takes the arguments we provide to it and prints them out. It echoes them back at us.
        
    * The `sort` command prints out the sorted contents of that file. For example, sort `colors.txt` prints out each line of the `colors.txt` file, sorted in alphabetical order.
        
        ```bash
        echo hello
        hello
        sort colors.txt
        blue
        green
        indigo
        orange
        red
        violet
        yellow
        ```
        
    
    ### Options
    
    * Each command typically supports a host of options that we can choose to use when executing the command.
        
    * These options modify the behavior of the command in predefined ways.
        
    * Options are prefixed by a dash, as in -h or -3.
        
    * For eg, the `-j` option tells `ncal` to display a calendar using Julian days (days are numbered starting from jan 1st)
        
        ```bash
        command -option
        ncal -j
        ```
        
    
    ### Combining Options
    
    * We can provide multiple options at once. This example uses the -3 option to display the previous, current, and next month AND the -h option to turn off the highlighting of the current date
        
    * When we provide multiple options to a single command, we can use a shorter syntax where we only need a single dash (-) character and we can chain the options together.
        
        ```bash
        ncal -3 -h or ncal -3h
        ncal -3 -h -j
        ```
        
    
    ### Long Form Options
    
    * Some options also support equivalent long format options that are usually full words and are prefixed with two dashes instead of just one.
        
    * For example, the `date -u` option is used to print the date in Coordinated Universal Time (UTC). We can instead use `date --universal` to accomplish the same result.
        
        ```bash
        date -u
        date --universal
        Sat Aug  5 02:18:04 UTC 2023
        ```
        
    * The `sort -r` option will sort a files contents in reverse. If we prefer, we can use the longer form `sort --reverse` to accomplish the same thing.
        
    
    ### Options with Parameters
    
    * Some options require us to pass in an additional value. For example, ncal's -A option is used to display a certain number of months **AFTER** a specific date. We need to tell it how many months to display as a parameter.
        
    * In this example, `ncal -A 1` prints out the current month with one month after.
        
    * **Note**: this can also be written as `ncal -A1` (no space between A and 1)
        
        ```bash
        ncal -A 1
        August 2023       September 2023    
        Su     6 13 20 27        3 10 17 24   
        Mo     7 14 21 28        4 11 18 25   
        Tu  1  8 15 22 29        5 12 19 26   
        We  2  9 16 23 30        6 13 20 27   
        Th  3 10 17 24 31        7 14 21 28   
        Fr  4 11 18 25        1  8 15 22 29   
        Sa  5 12 19 26        2  9 16 23 30
        ```
        
    * There is also a -B option to print a number of months **BEFORE** the specific date. We need to pass it a number of months.
        
    * In this example, `ncal -B2` prints out the current month with the two previous months.
        
        ```bash
        ncal -B2
        June 2023         July 2023         August 2023       
        Su     4 11 18 25        2  9 16 23 30     6 13 20 27   
        Mo     5 12 19 26        3 10 17 24 31     7 14 21 28   
        Tu     6 13 20 27        4 11 18 25     1  8 15 22 29   
        We     7 14 21 28        5 12 19 26     2  9 16 23 30   
        Th  1  8 15 22 29        6 13 20 27     3 10 17 24 31   
        Fr  2  9 16 23 30        7 14 21 28     4 11 18 25
        Sa  3 10 17 24        1  8 15 22 29     5 12 19 26
        ```
        
    * This example uses both the -A and -B options to print out 1 month before the current month AND one month after.
        
        ```bash
        ncal -A1 -B1
        ```
        

## All Together

* This example prints out the calendar for October 2001, with one month before and one month after.
    
    ```bash
    ncal -B1 -A1 october 2001
    ```
    
* So how would one figure out which options and parameters should be passed to a command?
    

## Getting Help

* You saw someone using `ncal -w` but you have absolutely no idea what it does.
    
* Here comes the manual pages which are a built-in form of documentation available on nearly all UNIX-like operating systems. The specific contents vary from one operating system to another, but at a bare minimum, the man pages include information on commands and their usage.
    
* To read the specific piece of documentation associated with a given command, run `man command`
    
* For example, to learn more about the `ncal` command we could run `man ncal` This displays a bunch of information on ncal that we can scroll through. Type "q" to exit.
    
* You can move up and down using the arrow keys and move between pages using space
    
* You can search for something specific by typing `/`, after which a prompt will be shown where you can search. For example to search for `ncal -w` , first open the manual using `man ncal` then `/-w` .
    
    ```bash
    man ncal
    ```
## Help!

- Some commands do not have man pages written for them, because they are commands that are directly built into the shell.
- We can find documentation for those commands using the `help` command. For example, to learn more about the cd command we would run `help cd`
        
```
help cd     
```