# The command line You may not have worked with the command line before, but if you continue in computational research, it's destined to be something you spend a lot of time with. And probably - something you'll eventually enjoy using! ## Getting started on the command line ### Using the command line on a Mac On a Mac, you have very few steps before you're up and running on the command line! All you need to do is to open up `Terminal`. This is a program on MacOS that communicates directly with the computer, so that you can send commands and run programs.

Terminal Emulators

A terminal emulator is an application that provides text-based commands to the computer, without some graphical user interface (GUI). In the case of your Mac, the terminal emulator takes the place of the clean graphics of the ribbon at the bottom of your screen and the icons arranged on your desktop. Rather than a double click starting a program, you explicitly send the command of what to do to the machine!
### Using the command line on Windows In Windows, you can run commands through the **Command Prompt**, which you may have used before. You can access the command prompt using the `Run` application by typing "cmd", or through any search bar via typing "cmd" or "Command Prompt". However, you'll probably come to an impasse pretty quickly when using the Command Prompt as a terminal emulator. It doesn't have a lot of the features that a program like `Terminal` does, so you'll probably want to download something to make your life easier. One great option for that is `Cmder`, with a download available [here](https://github.com/cmderdev/cmder/releases/download/v1.3.11/cmder.zip), although you might be able to get by with `Powershell` as loaded on your Windows machine. ### Logging on to Poseidon or another HPC system Once you're using the terminal emulator of your choice, you can start directly executing commands, sending them to your computer without further middlemen. One of the commands you'll probably run the most often on your local computer is `ssh`. `ssh` is typically referred to as "Secure Shell" (though acronyms can be a bit ambiguous in computer science - my favorite example of this is *GNU*, the most famous example of a **recursive acronym**...look it up!). What `ssh` is doing is establishing a secure connection to a computer located somewhere else. You can think of `ssh` as a virtual administrator that allows you a pass into a computer that isn't your own. Once you're in, you can run commands on this _new_ computer the same way that you do so locally. If you're part of the Alexander Lab, you'll be `ssh`-ing into `Poseidon`, the nautically-named compute cluster, which may also be so named due to the undue influence of **P**hysical **O**ceanographers. You can log into `Poseidon` using: ``` ssh @poseidon-l1.whoi.edu ``` After the "l", you can place either a 1 or a 2. This just corresponds to the fact that we have two different login nodes ("login-1" and "login-2"). Because I do some work in interactive sessions, it makes it easiest for me to always log in to the same login node. In general, this doesn't matter so much. #### Using `vim` Because the next exercise will involve a little bit of text editing, we'll take a second to talk about `vim`, one of the most popular command-line text editors (and our tool of choice!). It turns out that while the original tool, `vi`, stands for "Visual Improved", `vim` is also a bit of a recursive acronym, standing for "vi improved". There is a **lot** that you can do with `vim`. To start out with, we'll just talk about how to open and save. To open a text file: ``` vim ``` Which will take over your terminal with the canvas of the newly opened document. The document you specify *needn't exist previously* (you can create brand-new documents this way), and it can have *any extension* (including ones that you've never heard of). Once you are inside the document, you can type a colon (`Shift+;`) to tell `vim` that you wish to change modes. After you type the colon, your cursor should show up at the bottom of the panel. Next, you can type "i" for `INSERT`. This will change you to editing mode. After editing the document, you can again type a colon. To save the file, you can just type ":w" then hit enter. To both save and close, ":wq" is the sequence. To close without saving, you can just use ":q", but this will complain if you have edited the document. To force `vim` to ignore your edits, you can use ":q!" (but keep in mind, this is not recoverable if you make a mistake, and it won't ask for any more confirmation!).

Exercise: Writing a Document with Vim

Try out vim by creating a document called test.txt in your current working directory. Type a sentence in the document, then close it out.
When you type and enter ls after editing the document, what do you see? What about with the -l flag?
#### Editing your `bash_profile` Before you log in, you might want to do something immediately that can be super handy. We can *alias* a command to tell our shell to run a longer command in place of the shorter one that we enter. These aliased commands can be stored in our `bash_profile`. To edit your bash profile, you can use: ``` vim ~/.bash_profile ``` Which will also create the document if you don't have one. A useful line to insert into this document: ``` alias whoi="ssh @poseidon-l1.whoi.edu" ``` This will make it so that all you need to type is `whoi`, and you'll instantly be sent to the `Poseidon` login. #### Setting up an ssh key When you first log in to Poseidon, you'll be required to enter your WHOI account password (keep in mind: different from the _network_ password that you've used to access the VPN, WiFi, or both). This can be annoying, especially if your password is long and/or complex (read: **secure**!). To get around having to do this when you log in from the local computer that you use most often, you can type ``` ssh-keygen -t rsa ``` cat ~/.ssh/id_rsa.pub | ssh user@hostname 'cat >> .ssh/authorized_keys' On your *local* machine. You should accept the defaults, and enter a passphrase to use if prompted. Now, type: ``` cat ~/.ssh/id_rsa.pub ``` And copy the text that gets printed below your prompt. Now, log in to Poseidon using `ssh`. Once on `Poseidon`, type: ``` ssh_publickey="" ``` Make sure that you have pasted *exactly* the printed text from your local computer and *nothing extra*. Also make sure to put quotes before or after the pasted text. Now, type: ``` echo $ssh_publickey >> .ssh/authorized_keys ``` This should make it so that the remote server recognizes you as an authenticated user when you attempt to log in from this computer, so that typing `whoi` (if you've set up the alias) gets you straight to Poseidon, no password required. ### Commands that you'll use most often Working on the command line is most useful for opening and parsing files quickly and easily. For that reason, we provide a list below of a few of the commands you'll use most frequently when working with the command line. With the exception of the last command on this list, this set of commands is also an excellent group of commands to practice with. - `ls` - print out the contents of the current directory. - `cd` - change directories. - `pwd` - print the current working directory. - `mkdir` - make a new directory. - `cp` - copy files between two locations. - `mv` - move or rename a file or folder. - `rm` - **warning**: doing this is _permanent_. There is no "Recycle Bin" or "Trash". This removes a file or folder.

Command Line Flags & Arguments

Every command has some set of flags that you can use in order to achieve the output you're looking for. Among the most useful flags is --help on Linux, which shows you all of the flags that are available for a command.
As opposed to flags, arguments are inputs that the command expects when you run it. These may be either optional or required, but you'll offer these to the command without any subheading. For example, the cp command requires that you give it two pieces of information, every time you run the command. It needs what you want to copy and where you want to copy it to. If you leave these details out, you'll get an error. The arguments to a command are separated ("delimited") by spaces.
The key connection to make when working with files and directories on the HPC is to understand the file structure the same way you have understood it for years graphically (which can be tricky!). This is why it's essential to get some practice with navigating, opening, and closing files before going much further.