Getting Started with the Command Line
Tags: commandline, productivity, macos, windows
Published: 2019-10-25 23:43:39

If you're an avid computer user who wants more *granular* control over what your operating system offers, via windows you can open or buttons you can click, chances are you ned to work with the command line. Sometimes called the *shell*, it comes in many forms: `cmd.exe` and `powershell.exe` on Windows or `bash` on Linux and macOS. Nevertheless, what we are doing here applies to both shells.

### Directory navigation

Suppose we have the following directory structure:

```bash
<user directory>
...
+-- Desktop/ (we are here)
    +-- program.exe
...
```

On Windows, you would likely see the following prompt:

```cmd
C:\Users\<username>\Desktop> _
```

and on Linux and macOS (probably):

```bash
~/Desktop $ _
```

What you type will automatically appear at the underscore.

Now, let's say you want to execute `program.exe`. On Windows, make sure you are using `cmd.exe`; then executing the program would be a simple matter, **just type `program.exe` and hit Enter**.

In PowerShell and on Linux and macOS, however, things get more complicated. You will notice that `program` alone does not work, and *will likely result in an error about a program not found*.

**You actually have to use `./program.exe`.** But *what does this mean?*

### `.` means "the current directory"

That's right, you have to *explicitly* tell the shell that `program.exe` is in the current directory, and only then will it execute the program. This is for security reasons. What if you have 2 `program.exe`s, one that's already installed in your system, and the other located in `Desktop`?

Once again, `./program.exe` means "I want to execute `program.exe` and that it is in the current directory." And did I tell you that `.` means "the current directory"?

----------------------------------------------------------

Let's expand our directory structure for a bit:

```bash
<user directory>
+-- Documents/
    +-- assignments.pdf
+-- Downloads/
    +-- archive.zip
+-- Desktop/ (we are here)
    +-- program.exe
```

and we are still in `Desktop`. Now suddenly you feel the urge to extract the archive `archive.zip` in `Downloads`. But we're in `Desktop`, so what do we do?

### `..` means "the parent directory"

The reason I'm introducing this is because there's no direct way to jump from `Desktop` to `Downloads`, and you have to go through the parent directory to access `Downloads`.

So now, from `Desktop`, we want to:

- get to the parent directory
- go to `Downloads` from the parent directory
- access `archive.zip` in `Downloads`

The file, *relative to `Desktop`*, is accessed using the following syntax: `../Downloads/archive.zip`. Let's break this down.

- we need to get to the parent directory, so we used `..`
- we add a forward slash (`/`), which acts as a delimiter for directories
- we need to get to `Downloads`, so we append that to get `../Downloads`
- we add a forward slash (`/`)
- access the `archive.zip` file by appending that, resulting in `../Downloads/archive.zip`.

Simple, isn't it? Now to extract the file, Linux has the command `unzip` that does it for us. So the prompt should look like this in Linux:

```bash
~/Desktop $ unzip ../Downloads/archive.zip
```

----------------------------------------------------------

Now what if we actually want to *change where we are at?* Staying in `Desktop` all day seems boring, no?

### `cd` means "change directory"

It does exactly what it says: give it a proper directory location and it will transport you there. Say you are still in `Desktop` and want to get to `Downloads`.

- You can first go to the parent directory, so `cd ..` does that.
- You can then get to `Downloads`, so use `cd Downloads`.
- Or you can get there directly by using `cd ../Downloads`. Remember, `/` delimits parent-child directories.

And that's some brief command line usage tips. This helps every one of us inch closer and closer to being a power user, but we'll take it one step at a time. :)

----------------------------------------------------------