# Command Line

## The Directory Structure of an Operating System

The **OS** (Operating System) on your computer organizes your files and folders in a hierarchy, all bundled up for each user. You may notice that you are the only user on your laptop, which is indicated by a folder that has the name of which you go by on your computer (in the diagram below, there happens to be four users) in some sort of `Users` folder.&#x20;

A **directory** is just a fancy word for folder, and **files** could mean anything with extensions like `.zip`, `.json`, .`txt`, `.mp3`, etc.

![](https://3452473873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lu9Zw53dmTuhqAq-8K8%2F-LxPODGD4eJWChFJPY-r%2F-LxPRCZOfoD1cJfqtIsa%2Fdirectory.png?alt=media\&token=72491f77-11af-4fd9-a854-b6193fe8375b)

The command line comes in handy because it is a tool that allows you to jump in and out of directories (aka traversing this tree diagram), see what stuff is in them, and do cool things with just some commands you type!&#x20;

## Why Use Command Line?

#### It's Fast

It can help you navigate your computer in much faster ways than clicking around on your mousepad on the graphical user interface. We highly recommend you get familiar with this throughout the course!

#### It's Meant For Developing

For example, if you want to be able to develop software in a virtual environment (which you can think about as some mode that you activate that allows you to get access to libraries that are relevant to the software you are currently developing), you do it through the command line! But don't worry about this yet, let's get through the basics first...

## The Basics of Command Line

Although this tutorial is done with MacOS commands, it's quite similar to those of Windows and Linux.

### Traversing directories

As mentioned earlier, the file system on your laptop is organized in a tree-like hierarchy, with files nested within folders within folders, all bundled up in a big folder that corresponds to a specific user.&#x20;

Let's assume that `user1` is the root directory. This means that if you are using `user1` on your laptop, when you open command line, you will automatically be in the directory `user1`.

You can see what's in the directory that you are currently in with `ls` (this is the Windows equivalent to `dir`):

![](https://3452473873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lu9Zw53dmTuhqAq-8K8%2F-LxPYAg79r8g52gInF0m%2F-LxPZRmQz-HDVuD0CBhn%2FScreen%20Shot%202019-12-30%20at%2010.06.22%20PM.png?alt=media\&token=e327d94c-5954-4c56-a999-b5f346f7b94d)

You can step further into directories (going down the tree in the diagram) with `cd <folder name>` (folder name must be one of those that were listed when you typed `ls` previously). Once you've entered `D1`, you can then type `ls` to see what's in `D1`:

![](https://3452473873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lu9Zw53dmTuhqAq-8K8%2F-LxPYAg79r8g52gInF0m%2F-LxPZkvv6FbPAg3NJsKZ%2FScreen%20Shot%202019-12-30%20at%2010.06.43%20PM.png?alt=media\&token=78dbc190-bd87-471a-b74c-402d8afba5fc)

Oh, it's a text file named `f1`! However, I'm not ready to see this file yet. Let's try stepping back up the tree with `cd ..` (we just went from `user1` to `D1`, but let's go from `D1` to `user1`):

![](https://3452473873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lu9Zw53dmTuhqAq-8K8%2F-LxPYAg79r8g52gInF0m%2F-LxP_N0TzK8VVwqMggVz%2FScreen%20Shot%202019-12-30%20at%2010.06.59%20PM.png?alt=media\&token=1a96bfa1-fe92-486c-86fa-292903cc6779)

And of course, when I type `ls`, we see that being back in `user1` properly gives us the folder `D1` that we saw when we first typed `ls`!&#x20;

And if I go further and further deeper down and go through more nested directories within `user1`, because we established earlier that `user1` is the root directory, I can always jump back to `user1` easily with simply: `cd`.

### Looking at file contents

Now I'm ready to go back into `D1` and see what that `f1.txt` has with `cat <file name>`:

![](https://3452473873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lu9Zw53dmTuhqAq-8K8%2F-LxPYAg79r8g52gInF0m%2F-LxP_fHGoKgBF846xlrY%2FScreen%20Shot%202019-12-30%20at%2010.07.33%20PM.png?alt=media\&token=285464dd-dcf6-4883-881d-80b2425b0d47)

Great! Looks like `f1.txt` just contains a string `yeetaki mushroomz`. Now I want to see it with my default text editor instead of on the command line, and I can do that with `open <file name>`:

![](https://3452473873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lu9Zw53dmTuhqAq-8K8%2F-LxPYAg79r8g52gInF0m%2F-LxP_uaCceA6YS2uqKGs%2FScreen%20Shot%202019-12-30%20at%2010.07.55%20PM.png?alt=media\&token=278175a3-3ef6-41c5-bb39-8e98d7fd1f06)

And a new window should open with the file opened:

![](https://3452473873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lu9Zw53dmTuhqAq-8K8%2F-LxPYAg79r8g52gInF0m%2F-LxPa-Vc5MviU2cGJzUd%2FScreen%20Shot%202019-12-30%20at%2010.01.47%20PM.png?alt=media\&token=e5fda5ee-7b9c-4d21-aab7-86fd19f43db7)

## **MacOS & Linux Terminal**

### **The Basics**

* look at what’s in the directory (aka folder) you are currently in: `ls`
* see the path that leads you to the directory you are currently in: `pwd`
* go into a directory: `cd <folder name>`
* step back out of a directory: `cd ..`
* jump to the root directory: `cd`

### Other helpful commands

* make a directory: `mkdir <folder name>`
* delete a directory: `rm -rf <folder name>`
* delete a file: `rm <file name>`
* output contents of a file: `cat <file name>`
* to copy a file: `cp <name of file to be copied> <path you want the copy to be in>/<the name of the copy>`
* open Finder from where you are currently: `open .`
* open Visual Studio Code from where you are currently: `code .`

### Helpful tips to be faster

* click on the up arrow to go through your history of commands
* if you want to find a previous command and only know part of it (you don’t remember it completely), you can do:
  * `ctrl + r` and start typing to see the most recent command that matches what you’ve typed
  * to scroll through more suggestions, hold `ctrl` and hit `r` for each suggestion
* to get your cursor to the beginning of what you just typed: `ctrl + a`
* to get your cursor to the end of what you just typed: `ctrl + e`&#x20;
  * remember it as `e` for “end”
* delete line of text where your cursor is: `ctrl + u`
* while you are in the middle of typing a folder name, or git branch name, you can press `tab` to autocomplete
  * if there are multiple suggestions, you can go through each suggestion by pressing `tab` each time

## Windows Command Prompt

### **The Basics**

* look at what’s in the directory (aka folder) you are currently in: `dir`
* see the path that leads you to the directory you are currently in: `pwd`
* go into a directory: `cd <folder name>`
* step back out of a directory: `cd ..`
* jump to the root directory: `cd`

### Other helpful commands

* make a directory: `mkdir <folder name>`
* delete a directory: `rd /s /q "<path>"`
* delete a file: `rm <file name>`
* output contents of a file: `cat <file name>`
* to copy a file: `cp <name of file to be copied> <path you want the copy to be in>/<the name of the copy>`
* open Windows Explorer from where you are currently: `start .`
* open Visual Studio Code from where you are currently: `code .`
