Introduction
The terminal is one of the most useful tools on a UNIX-based Operating System that you will ever use. If you are in Computer Science, you'll be using it for classes such as CS140 CS102 and up. This guide is designed to help get you accommodated to the terminal.

Note: I will assume that you have a terminal emulator of some kind. There are many terminals available and which one you use depends on your Operating System.
  • For Windows, UTK recommends MobaXTerm. I use git-bash (MINGW64 / MinGW). There are a few options so pick whatever works.
  • For Mac, you have the terminal built in. But iTerm2 is pretty nice from what I heard.
  • For Linux, you have many options. gnome-terminal, xterm, st, konsole, yakuake, etc. Pick whichever one you like. Hydra and Tesla come with gnome-terminal. Personally I use Konsole.
Table of Contents
This is a huge guide, I know. It's constructed so you can go back to certain parts of it if you need quick notes. Click any of these sections to go to a part of the guide:
  1. Introduction
  2. Table of Contents
  3. The Terminal
  4. Tutorial: Compiling and Executing a program
  5. Tutorial: Remote connecting to a computer
  6. Tutorial: Vim, the almighty text editor
  7. Tutorial: Copying files to and from a remote computer
The Terminal
Behold:


I ripped this straight off of my CS130 Fall 2017 Lab 9 guide.

The terminal is rather... "lacking" of a GUI (Graphical User Interface). That doesn't mean that you won't be productive on it though. Chances are that, once you get used to the terminal, you'll be more productive on it than you were without the terminal.

The basis of the terminal can be intimidating at first, but it's relatively simple. You just type in a (valid) command and it does work. Your GUI actually executes these commands in the background for you, but now you're going to be doing it yourself.

The Cheatsheet

Feel free to explore around and try out various commands. Here are a few that I recommend you to be familiar with (Again, ripped off from the same lab guide):

Command Description Usage(s) Example
ls Lists files in current directory... or a directory specified ls
ls FOLDER_NAME
ls
cd Change Directory (literally).
You start out in your "~" directory (your home directory) and use "cd" to move to a different one.
cd FOLDER_NAME cd FOLDER
cd ../
mkdir Make Directory (literally).
This will allow you to make a folder.
mkdir FOLDER_NAME <-
rm Remove (literally)/Delete.
This will allow you to delete a file or a folder. If you try it on a folder, you must add "-r" as a parameter.
WARNING: Once you delete a file, it's gone. You're not getting it back.
rm FILE_NAME
rm -r FOLDER_NAME
<-
mv Move (literally).
This will allow you to move a file or a folder. It also serves to allow you to rename a file to another name.
mv OLD_NAME NEW_NAME <-
man Manual (literally).
Type this along with another command and (usually) a page will appear telling you how to use that command. It is, as the name implies, a manual page that is a quick resource for help. Press "q" to quit "man" when it's open.
man COMMAND_NAME man ls
man gcc
diff Difference (literally).
Will tell you the difference between two files. If there is no difference, nothing is printed out.
diff FILE1 FILE2 <-
nano Nano (literally) Text Editor.
Dr. Marz's preferred Terminal Text Editor. Commands are shown at the bottom of the screen (e.g. Ctrl+X = Exit).
nano FILE <-
vi/vim Vim (literally) Text Editor.
My preferred (and clearly superior) text editor. Vim is very powerful but it also takes some time to get used to compared to Nano. You'll use it in CS140 and CS302.
vi FILE
vim FILE
<-
gedit Gedit (literally) Text Editor.
This one actually isn't a terminal application. It will execute a graphical text editor kind of like TextEdit on Mac and Notepad on Windows. However, you can not use your terminal while Gedit is open.
vi FILE
vim FILE
<-
g++ A C++ Compiler.
You will be using this for pretty much your entire time here. It will make an executable from your C++ source code files.
g++ -o EXECUTABLE SOURCE g++ -o test test.cpp
ssh Secure Shell.
Allows you to remotely connect to other machines. i.e., connect to UTK's Hydra machines from your own personal computers.
ssh USERNAME@ADDRESS ssh cnguyen@hydra0.eecs.utk.edu

Demonstration

Here's a live demonstration of how to use the terminal. Here, I make a directory named "cs130", then a directory named "lab0" inside of it. Then inside of "lab0", I make 2 C++ programs named "test1.cpp" and "test2.cpp". I also show how to compile and run them in the Terminal.


Tutorial: Compiling and Executing a program
Okay so you've written your first program and you want to run it. In an IDE of some kind, you simply hit the "Compile & Run" button and it just works for you. However, how does that IDE actually do that? It runs a series of commands behind-the-scenes which compiles the program for you and then launches it. Doing this yourself, though, may not be so trivial.

Compilation

In our command cheatsheet above, we actually have a command that is a C++ compiler... g++.

Let's say that you have "main.cpp" and you want to make "main"... okay that's simple.
g++ -o main main.cpp
This will make "main", your executable file, from "main.cpp", your source file. Pay attention to the "-o" argument!!! If you put "main.cpp" right after -o instead of "main", your source code will be obliterated! Whatever argument comes right after the "-o" parameter is the executable name. Anything else is source code files.

In other words, do NOT do this!!!
g++ -o main.cpp main
Ahem, now that's off my chest, let's move on.

Execution

Okay so you have your shiny new "main" executable. You can try the naive approach of just typing "main" in the terminal if you like... however, it will fail (in most circumstances). The valid approach is to put "./" in front of the executable name. Therefore:
./main
This is telling the terminal "Run "main" that exists in this current directory" (remember that "." means "current directory"). There are many reasons for this, but the first one that comes to my mind is that you can write an executable with the same name as a linux command (i.e. ls), and have to distinguish between Linux's ls and your own ls.

Executing with arguments

I'm not sure how it was with an IDE (ahem, CodeLite) to put in Command Line Arguments, but let's keep this plain and simple. Let's say I had the following C++ program ("main.cpp"... again):
#include <iostream>

using namespace std;

int main(int argc, char* argv[]) {
	for (unsigned int i = 0; i < argc; i++)
		cout << argv[i] << endl;
}
This program will print out arguments passed in to it. That doesn't sound bad... but how do we actually pass those in?
./main arg1 arg2 arg3...
Yeah... you literally just put the arguments right after the executable name. And the output:
./main
arg1
arg2
arg3
Tutorial: Remote connecting to a computer
If you had me as your TA in lab, you'll probably remember all of those times that I remotely connected to school computers to do certain tasks. UTK gives their EECS students access to Hydra and Tesla lab computers, and gives them accounts that they can log in either locally, or remotely. Logging in remotely will allow you to do work on those machines from anywhere.

The best part is convenience. You can be at home, or even in another country and you can still remote in to that computer with either it's IP Address or an address that resolves to an IP Address (Considering I SSH into my server in Paris sometimes).

So let's get into it. The command is:
ssh USERNAME@ADDRESS
Sounds simple right? Let's say that someone by the username of "cnguyen" (me) wanted to sign in to "hydra0.eecs.utk.edu".
ssh cnguyen@hydra0.eecs.utk.edu
It will prompt you for your password (which will not show. It'll be blank). Once you type it in, you're good to go. Any commands you type will be run on that computer.

"So I can get into any computer and hack it? Muhahaha"

Nope. If you noticed in my lab sessions, my IP Address shows at the bottom right of my screen. However, good luck signing in to it.
  1. That machine must have SSH set up (if it's Windows, you don't stand a chance).
  2. If it is set up, you have to figure out which port I use to accept SSH connections.
  3. You have to have a valid user account on that machine... or know a valid user on that machine.
  4. If that machine doesn't accept RSA, you have to know my password. If it does have RSA, there's no password prompt, and you'll never get in without some ridiculously huge key file.
Hydra and Tesla labs at UTK have SSH set up so that each user can sign in. Therefore, it's easy for you to get in because you just put in your NetID and password. It's all set up for you, but you won't get your "Hollywood epicz ez haxor" moments from SSH.
Tutorial: Vim, the almighty text editor
In a very specific lab session (CS130 Fall 2017, Lab 1), we asked you to type in gedit to open up a graphical text editor. Unfortunately, whenever you do work remotely, everything is via the terminal. You will not have the choice to use the graphical user interface. So gedit won't work. Thankfully, there are terminal based text editors that will help you get the job done... and they are typically more powerful than the graphical ones anyways.

Startup

Vim can be very intimidating at the start, because it's unorthodox to the average user. But it has some true power behind it. To begin, let's open it up. Type the following command:
vi test.cpp
And yes, on Hydra/Tesla, vi is aliased to vim. You can also type vim and it'd do the same thing. The original vi is just bleh.

So you opened up Vim, but now you're presented with a blank screen!


"My Vim doesn't look like yours! What gives?"

Someone asked me about this in lab so I figured I'd mention it here. Applications such as Vim allow the user to customise how it looks. I wrote my own theme for it, but the functionality is the exact same as yours.

Control Cheatsheet

Vim isn't like a normal text editor, but its own controls have become popular enough that other text editors have a "Vim mode". Your controls are based on what "mode" you are in. Here is a list of common modes:
  • Normal Mode: This is the mode you start out in. You may use the arrow buttons (or Vi's HJKL setup)
  • to navigate around. You may also press the colon button ":" ("Shift + ;" on USA keyboards) in order to input a command (more on that below). Press "i" to enter Insert Mode, etc.
  • Insert Mode: Pressing "i" while in Normal Mode triggers this mode. This is where Vim acts like a text editor that you're used to. Type stuff on your keyboard and it types. Press Esc to exit Insert Mode and go back to Normal Mode.
In Normal Mode, you have commands which you can execute by typing in the ":" character. The only essential ones you need to know are "w", "q" and "q!".

Command Description Usage(s)
:w Writes a file to the disk. :w
:q Quits Vim. If you made changes to a file, you must put "!" after in order to quit without saving changes. Otherwise it'll refuse to quit. :q
:q!
:wq Writes to disk, and then Quits Vim. :wq

When in Normal Mode, you can also perform copy and paste of lines easily. There are a few other things you can do too such as deleting, and repeating actions done in Insert Mode. I showed these in lab too. Note: These do not use ":". They are commands bound to buttons in Normal Mode that do not require it.

Command Description
i Triggers Insert Mode.
v Triggers Visual Mode.
V Triggers Visual Block Mode.
yy Yank. Grabs a line and puts it in clipboard (For Copy and Paste).
y2y Yank. Grabs 2 lines and puts them in clipboard (For Copy and Paste).
cc Cut. Grabs a line and puts it in clipboard (For Cut and Paste). It will also delete that line.
c2c Cut. Grabs 2 lines and puts them in clipboard (For Cut and Paste). It will also delete those lines.
dd Delete. Deletes the current line.
d2d Delete. Deletes 2 lines starting from the current one.
p Paste. Pastes whatever was inside the clipboard from the above commands.
u Undo. Undoes the last action performed.
C-r (Ctrl+R) Redo. Redoes the last action performed if undone.
. Any changes done in Insert Mode are done again.

You can also do string replacement via regular expression commands, but I do not expect you to remember these. I showed these in lab.

Command Description
:s/old/new Substitution. Replaces one case of old with new.
:%s/old/new Substitution. Replaces one case of old with new... for each line (%).
:s/old/new/g Substitution. Replaces ALL (g) cases of old with new in a line.
:%s/old/new/g Substitution. Replaces ALL (g) cases of old with new in a line... for ALL lines (%).

I'll make a more in-depth Vim guide whenever the time comes. Since this is the basics for the terminal, I won't focus much more on Vim.
Tutorial: Copying files to and from a remote computer
Tip: Before you read all of this... If you really don't want to copy files over through the terminal, you can find tools like Filezilla or even MobaXTerm to copy the files over for you. But there are those (me) who prefer to do everything without needing separate applications, or may not have a choice but to use the terminal. So it's good to know of a terminal-only solution.

Okay, so I taught you how to remote connect to Hydra and Tesla above. But there is a command (which is not actually listed in the command cheatsheet above) which will allow you to copy files to and from remote places easily. It isn't in the list above because it's a bit... special. Introducing scp.
scp FROM_PATH TO_PATH
In either the FROM_PATH or the TO_PATH, you may either specify a file locally by just typing the name, or by using a special format string to tell scp you want to point to a directory or file on another remote machine. That special format string is:
username@server_address:path

How it works

You can use scp just like cp, except it also allows you to copy files from remote places such as Hydra. The best way is to teach by example.

Example: Copy a file from a remote computer to your own

Let's say I have a file on hydra0.eecs.utk.edu, in my home directory (~), named "test.cpp" and I wanted it moved over to whatever directory I was currently in.
scp cnguyen@hydra0.eecs.utk.edu:~/test.cpp .
Let's break this down:

Text Description
scp The name of the command...
cnguyen@hydra0.eecs.utk.edu:~/test.cpp The address we are connecting to (just like ssh). Followed by the path to a file in our home directory. "~" is a shortcut to "/home/USERNAME/" so this expands out to "/home/cnguyen/test.cpp".
. (Yes, that's a dot.) Path to current directory we are in (on our local machine).

So as you noticed, there's a special format going on there. It's ADDRESS:PATH for something that is remote. So for hydra0.eecs.utk.edu and ~/test.cpp, you combine them with : in between... making hydra0.eecs.utk.edu:~/test.cpp to tell scp "I am pointing to this file on hydra0".

Example: Copy a file from your machine to a remote computer

Let's reverse the previous example. Say I have a file in my folder named "test.cpp" and wanted to move it to my home directory at "hydra0.eecs.utk.edu"... worry no more.
scp test.cpp cnguyen@hydra0.eecs.utk.edu:~/
This is saying "Copy test.cpp to the machine at cnguyen@hydra0.eecs.utk.edu and put it in our home directory there.". The path breakdown in the previous example holds true here too.
Guide Information
Basic Information
Name: Introduction to the Terminal
Description: Because you'll need it to do cool stuff
ID: terminal_intro
File Information
File Size: 24.87 KB (25470 bytes)
Last Modified: January 06 2019 06:23:32
Version: 1.1.2
Translators
en-gb Clara Nguyen (iDestyKK)