# Terminal Reference

## Common Terminal Commands
This reference will give you a simple primer on some basic Terminal commands.  These commands should work on any Mac or Linux computer.  (Windows uses a different system.)  More information can be found at the [MIT Missing Semester](https://missing.csail.mit.edu/) webpage or at a Wellesley Missing Semester Workshop.

### Basic Commands
Instructions can be given to the computer by using the following format (where some commands have 0 arguments, others have 1, etc.):
` command argument1 argument2...`
* `cd` - (Change Directory) moves you to a different location within the file structure.  Works with both absolute and relative paths.  (An absolute path is one that starts at a particular location (usually root or home).  A relative path generally starts from your current location.)  Different levels of the path are separated with slashes. `cd` takes either 0 or 1 argument.  The 0 argument version returns you to your home directory (equivalent to `cd ~`).  The 1 argument version takes a path and relocates you wherever that path points.
    * `cd ..` - moves you to the parent directory of your current location.
* `cp` - (CoPy) creates a copy of the referenced file or directory (first argument) and puts it in a new location (second argument).  `cp` takes 2 arguments. The first argument is the file to be copied.  The second argument is a path to the location you wish to copy the file to.
    * `cp -r` - copies recursively. works exactly like `cp` but the first argument should be a folder instead of a file
* `ls` - (LiSt directory contents) shows you all the (visible) files and folders in your current directory. `ls` can take either 0 or 1 argument. The 0 argument version shows you the contents of your current directory (equavlent to `ls .`).  The 1 argument version takes a path (to a directory) and shows you the contents of that directory.
    * `ls -a` - the `a` flag here tells the command to also show "hidden" files and folders.  Hidden files/folders begin with `.`.  (This should not be confused with the location `.`, which stands on its own.) 
* `man` - (MANual) this command brings up the "manual" page for any command in the system.  You can also find "man pages" online by searching the web.  These pages contain descriptions of what the command does as well as what flags/arguments are available to use.
* `mkdir` - (MaKe new DIRectory) creates a new folder (either in the current location or at the path given).
* `mv` - (MoVe) moves the file/folder specified (first argument) to the location given (second argument).  You can also use `mv` to rename a file.
* `pwd` - (Print Working Directory) gives the absolute path (from root) to your current location. (`pwd` takes no arguments)
* `rm` - (ReMove) removes the specified file.  Be careful when using it as it is irreversible.  (Unlike moving an item to the Trash in a GUI desktop, you cannot recover an item that has been removed via commandline.)
    * `rm -r` - the `r` flag here tells the command to remove recursively (ie go into each directory and also remove its contents).  You must use the `r` flag to remove a directory.  (There's a famous story about an errant `rm -r *` command during the production of Toy Story 2.)
* `touch` - can be used to create a new file without opening a text editor.  Can also be used to modify permissions and metadata on a file. `touch` requires at least one argument (the name of the file to be created or modified)

### Important Symbols
* `.` (on its own) means "here".
* `..` means "the parent directory of `.`
* `/` on its own (or at the start of a path) means "root".  Root is the start of your file tree.  Everything on the computer can be gotten to via a path that starts at root.  `/` is *also* used to write multi-step paths. (eg `~/Desktop/Homework` )
* `~` means "home".  Home is the directory that contains your user account.  Generally, you'll work within the home subsection of the file tree.  By default, a new Terminal should open with you at home.
* `*` is the wildcard symbol.  It represents all possible strings.
* `|`, `>`, and `<` are redirection operators.  They allow you to take the output from a one command/location and redirect it somewhere else.
    * `|` (pipe) takes the output from one command and makes it the input to another command
    * `>` takes the output from a command which would usually go to the standard output and allows you to redirect it to a file.  (`>>` appends instead of ovewriting the file.)
    * `<` takes the contents of a file and sends it to a command.

### Text Editors
If you want to edit text on the commandline, you can do so with a text editor.  The most commonly used are `emacs` and `vim`.  (You may also occasionally see `nano`.)  Each has their own quirks.  Most computer scientists have a favorite.  Often commandlines will default to `emacs`, which is more powerful but (IMO) less intuitive than `vim`.  It can be helpful to look up a reference document for the keyboard shortcuts for whichever text editor you're using.

Vim Commands:
* (open `vim` with the file name as the first argument)
* `i` will put you in "Insert" mode
* `Esc` (the key, not the letters) will get you out of insert mode
* `:wq` will give vim the commands to Write and Quit (save/quit)
* if you get stuck in Vim with the wrong file open (or other problem) you can use `:q!` to quit without saving

### More Commands
* `cat` - (conCATenate) sends the contents of one or more files (or other forms of data) to standard output to be read.
* `clear` - clears the commandline window so all that's left is a single new command prompt at the top.  Very helpful if you've got a long swath of text and are having trouble keeping track of where you are.  (In the Mac Terminal, you can still scroll up to see what you cleared away.) `clear` takes no arguments.
* `grep` - (Global/RegularExpression/Print) is your go-to text search tool.  grep looks for a "search pattern" (often in the form of a regular expression) in one or more files or locations.
* `echo` - writes the arguments given to standard output
* `less` - displays the contents of a file in the terminal in a scrollable way (not directly in standard output)
* `python` (or `python3`) - opens a python interpreter (basically, the shell from Thonny). It can be called without any arguments or with argument(s) of python files to run.  You can also run files from within the python interpreter.  (You can use `which` to determine whether `python` is connected to a version of python 2 or python 3 and therefore whether you need to use the python3 command.)
* `which` - used with any other command/application, it gives you the absolute path to that tool (thus explaining which version is being used by default). which requires 1 argument (the command you're trying to look up)