---
# System prepended metadata

title: Terminal Commands
tags: [resources]

---

---
tags: resources
---

# Terminal Commands

Two of the most useful tools you should get familiar with are **terminal** and **git**. Familiarizing yourself with how to maneuver through the terminal and tools like git will be very helpful throughout CS200 and beyond.

## Terminal
### 1. Accessing the terminal
* To open the terminal using macOS:
Go to *Applications* -> *Utilities* -> Open *Terminal* 
    - Alternatively, you can open *Spotlight Search* (keyboard shortcut is 'Command SpaceBar'), search for 'terminal', and open the first application listed
* If you are using a a Windows machine:
Search for *Command Prompt* 

### 2. Basic Terminal Commands

This is just a list of basic useful commands; feel free to skim through now then refer back to them as you need.

| Command            | Explanation                                                                                                                                                                                                                                                                                                                                                                                                                    |
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `man <command>`    | `man` prints the manual of a given command. For all of these commands and any new commands, use `man` to find out more about them, e.g. `man pwd` |
| `<command> --help` | Adding the `--help` tag after any command allows you to access the help menu for a specific command if available, e.g. `pwd --help` |
| `pwd`              | `pwd` or "print working directory" will print the full (absolute) path to the folder the user/terminal is currently at. |
| `cd <directory>` | `cd` or "change directory" moves the user to the indicated directory if it exists inside the current directory. |
| `cd <absolute-filepath>` | putting a `/` before a filepath indicates that it's absolute, i.e. that it starts from the root directory instead of from the current working directory (meaning it doesn't matter where you are now). |
| `cd ..` | `..` is a link within every directory to the previous directory that contains the current directory, `cd ..` lets you move back one folder |
| `cd ~ OR cd` | These commands will move you to your home directory (noted with a ~), which is usually your user name (e.g doing `cd` then `pwd` I get `/Users/anniecimack`). |
| `ls` | `ls` prints the contents of the current directory. |
| `mkdir <directory>` | `mkdir` creates a new empty directory named `<directory>` in the current directory.  |
| `rmdir <directory>` | This command removes an **empty** directory. It must be called in the directory that contains what a user wants to remove.  |
| `rm -r <directory>` | This command removes any directory and all of its contents.  |
| `cat <file>` | `cat` prints the contents of `file` to your terminal |
| `vim/nano <file>` | `vim` or `nano` are basic text editors that work within the terminal; we'll talk more about them later |
| `mv <filepath1> <filepath2>` | `mv` moves what file is at filepath1 to filepath2. for example, you could put a file into a folder with `mv file folder`. `mv` will also rename the file if you include a non-folder name at the end, e.g. `mv file folder/newname` |

___
**Practice Using your Terminal:**
**Task:** Within your own terminal, we will make a cs200 directory and re-setup your file system to be based in your home directory and easily accessible. Feel free to change steps (e.g. disclude the browncs folder) or not use this, but at least go through the motions so you know how. First, cd home and make a new "browncs" folder to put everything in (I would also definitely just make a shortcut to this folder to put on your desktop and/or pin it in your Finder/File Explorer).
```t
cd ~
mkdir browncs
```
Next, we'll hop inside and make a cs200 folder.
```t
cd browncs
mkdir cs200
```
Now we'll make directories for different assignments.
```t
cd cs200
mkdir hws
mkdir labs
mkdir projs
```
Finally, let's reclone the lab01 repo to our new labs folder. Let's also use a key trick! Whenever you're dealing with files or folders that exist, you can use tab to autofill what you've typed so far. Try typing `cd l` then pressing tab to complete the command. 
```t
cd labs
```
Then, click this url: https://classroom.github.com/a/hUL5du-F and clone the repo there.

Now, let's also make a `lab99` directory to show off how tab autocomplete works. If we press tab and there are multiple options to select from with the same prefix we've given, like if we only type l but there are multiple labs, pressing tab again will show us all of them. Then, once we see our options, we can type the necessary amount (lab) then press tab to make it work.
```t
cd l
cd lab
cd lab01
```
Now, if you use `ls` to list what's contained in lab01, you'll see that there is a "just-for-fun" folder. Get in there yourself and play around with your new pet using the commands we've learned above. Feel free to also mess around with other files included or skip them until we actually get to the vim editor section. 

Create a home for the cat by making a new directory and moving the cat inside to protect it from the scary eagle. Then rename it also using! I've secretly included a few typos in the last parts so you'll have to type the interesting `mv` commands yourself (remember to use tab!)
```t
cat cat-this-sample-text.txt
cat scary-predator-eagle.txt
mkdir new-cat-home
mv cat-this-sample-typo.txt new-cat-home
mv new-cat-home/cat-typo-sample-text.txt new-cat-home/Kryptonite
```

## Basic Text Editing with Vim
:::spoiler **If you have Windows, click here!**
Windows unfortunately does not come with Vim pre-installed. You will have to install it manually. Follow [this guide](https://www.freecodecamp.org/news/vim-windows-install-powershell/#:~:text=Vim%20is%20a%20powerful%20code,it%20running%20on%20your%20PC.) to get it set up.

**Note:** Don't worry about the 'PowerShell' bit - as long as you can use it in the basic command prompt application.
:::

Sometimes you might find yourself in a position where you're working in the terminal and can't easily open or access a file with an application that has an easy-to-understand interface (e.g. Intellij). In these cases, it will be helpful to work with a terminal-level text editor, like Vim or nano.

**Task:** Open your terminal and navigate to the folder that contains your code for lab 1. Remember to use `ls` to see what directories you can move into and `cd` to move into one of those directories! Once in that folder, navigate to the `src` folder.

Opening a file with Vim is very simple. All you have to do is type `vim` followed by the file you want to open. So, if you want to open a file called `ILovecs200.java`, once you navigated to the proper directory, you would type the following command:
```
vim ILoveCS200.java
```

**Task:** Open `Leaf.java` with Vim.

Once you've opened a file in Vim, your terminal will display all of the text that appears in the file. Vim has a lot of functionality, but we're only going to worry about insert mode right now. You are free to explore how the other commands work if you so wish. For a complete overview of everything you can possibly do with Vim, [look at this document](https://devhints.io/vim).

Opening a file with Vim only displays it, so in order to edit any text in the file, we'll need to enter insert mode.

**Task:** Enter insert mode by typing `i`. At the bottom of the terminal window, you should now see `---INSERT---`.

Once you're in insert mode, you can make changes to a file and save them without having to open an application like TextEdit or IntelliJ.

**Task:** Navigate around `Leaf.java` by using the arrow keys. Get to the last line and then hit enter. This should create a new line at the bottom of the file. Once you've done that, write yourself a comment (remember that comments start with a double backslash, i.e. `//`).
**Note:** You can also scroll using a trackpad (though sometimes this can be a little finicky). 

Now that you've made a change to the document, you'll need to exit insert mode and save it! To exit insert mode, press the `esc` button on your keyboard. Once you've exited insert mode, you can save a file by typing `:w`. You can save and close a file by typing `:wq`.

**Task:** Exit insert mode, then save and close `Leaf.java`.

Those are the absolute basics of Vim! Feel free to play around and change the actual text in any of the lab 1 files to see how replacement works. You likely will not need to use this during this class, but you may find it helpful for the future!

Now you know how to use some basic tools in the terminal! Hopefully, these skills give you a better sense of the inner workings of your computer.

---
*Please let us know if you find any mistakes, inconsistencies, or confusing language in this or any other CS200 document by filling out the (optional) [anonymous feedback form](https://forms.gle/JipS5Y32eRUdZcSZ6)!