Intro to the Terminal

Introduction

  • The terminal is a critical tool within UNIX-based Operating Systems, particularly useful for Computer Science students.
  • This guide is structured to familiarize users efficiently with the terminal, assuming users have a terminal emulator available.

Terminal Options by Operating System

  • Windows: Recommended terminal is MobaXTerm; git-bash (MINGW64 / MinGW) is also an option.
  • Mac: Built-in terminal, or iTerm2 is a popular alternative.
  • Linux: Built-in terminal with various options available including gnome-terminal, xterm, st, konsole, and yakuake.
  • Personal choice for Linux usage is Konsole, which comes with gnome-terminal on UTK's Hydra and Tesla.

Table of Contents

  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

  • Nature of Terminal:
    • The terminal lacks a GUI (Graphical User Interface).
    • Users can be highly productive once accustomed to using the terminal.
    • It executes commands directly that a GUI would process in the background.

Terminal Cheatsheet

  • Familiarity with essential commands is advised:
    • ls: Lists files in current or specified directory.
    • Usage: ls, ls FOLDER_NAME
    • cd: Change Directory. Move to specified directory from home (~).
    • Usage: cd FOLDER_NAME, cd FOLDER or cd ../
    • mkdir: Make Directory. Create a new directory.
    • Usage: mkdir FOLDER_NAME
    • rm: Remove/Delete a file or directory. Use -r flag to delete folders.
    • Usage: rm FILE_NAME, rm -r FOLDER_NAME
    • Warning: Files deleted are not recoverable.
    • mv: Move/Rename files or directories.
    • Usage: mv OLD_NAME NEW_NAME
    • man: Manual for command usage. Type alongside command to access help page.
    • Usage: man COMMAND_NAME
    • diff: Compares two files and reports differences.
    • Usage: diff FILE1 FILE2
    • nano: Terminal text editor preferred by Dr. Marz. End commands noted at bottom of screen.
    • Usage: nano FILE
    • vi/vim: Advanced text editor (Vim is enhanced version). Used in CS140 and CS302.
    • Usage: vi FILE, vim FILE
    • gedit: Graphical text editor (not terminal-based).
    • Usage: gedit FILE
    • g++: C++ Compiler utilized for compiling C++ source files into executables.
    • Usage: g++ -o EXECUTABLE SOURCE, e.g., g++ -o test test.cpp
    • ssh: Secure Shell for remote machine connectivity.
    • Usage: ssh USERNAME@ADDRESS, e.g., ssh cnguyen@hydra0.eecs.utk.edu.

Demonstration

  • An example of creating folders and compiling C++ programs within the terminal was elaborated. This includes creating a directory structure and the process of compiling and executing C++ code.

Tutorial: Compiling and Executing a Program

Steps for Compilation and Execution:

  1. Compilation: Use g++ to compile a C++ source file into an executable.
    • Command: g++ -o main main.cpp
    • Important note about the -o parameter:
      • This specifies the output executable file name. Misplacing source file names can lead to accidental overwriting of them.
  2. Execution: Run the executable via the current directory.
    • Command: ./main to execute the program. The prefix ./ indicates the execution from the current directory.

Running Programs with Arguments

  • Pass command line arguments to the executable directly after its name.
    • Example C++ program snippet and execution:
   #include <iostream>
   using namespace std;
   int main(int argc, char* argv[]) {
       for (unsigned int i = 0; i < argc; i++)
           cout << argv[i] << endl;
   }
  • Execution: ./main arg1 arg2 arg3...

Tutorial: Remote Connecting to a Computer

  • Remotely connecting to university computing resources (Hydra and Tesla).
  • Use SSH command to establish a connection to the remote server.
    • Command format: ssh USERNAME@ADDRESS
    • Example: ssh cnguyen@hydra0.eecs.utk.edu
    • Password input will be required, but will not display on the screen.
  • Important notes on connectivity security and access control:
    1. SSH setup is required on the target machine.
    2. Must have valid user account for access.

Tutorial: Vim, the Almighty Text Editor

  • Introduction to Vim: Powerful terminal-based text editor, essential for remote work where GUI applications (like gedit) are unavailable.
  • Initial command to start Vim: vi test.cpp (note: vi may alias to vim on various systems).
  • Modes in Vim:
    • Normal Mode: Initial mode for navigation and command input.
    • Insert Mode: For typing and inserting text. Triggered by pressing "i" from Normal Mode.
    • Visual Mode: For text selection. Triggered by pressing "v" from Normal Mode.
    • Visual Block Mode: Triggered by pressing "V" for block select.
  • Essential Commands in Normal Mode:
    • Save: :w
    • Quit: :q
    • Forced Quit: :q!
    • Save and Quit: :wq

Vim Control Cheatsheet

  • Basic Commands in Normal Mode:
    • i: Enter Insert Mode.
    • v: Enter Visual Mode.
    • V: Enter Visual Block Mode.
    • yy: Yank (copy) a line.
    • dd: Delete (cut) a line.
    • p: Paste.
    • u: Undo.
    • C-r: Redo.
    • Substitution Commands:
    • :s/old/new - Replace the first occurrence in a line.
    • :%s/old/new - Replace first occurrence across all lines.
    • :s/old/new/g - Replace all occurrences in a line.
    • :%s/old/new/g - Replace all occurrences across all lines.

Tutorial: Copying Files to and from a Remote Computer

  • Usage of scp (Secure Copy Protocol): Facilitates file transfer between local and remote computers.
    • Command Structure: scp FROM_PATH TO_PATH
    • Local files: Simply use the name.
    • Remote Path Syntax: username@server_address:path.
  • Example Transfers:
    • Copying from a remote computer to local:
    • scp cnguyen@hydra0.eecs.utk.edu:~/test.cpp .
    • Copying from local to a remote computer:
    • scp test.cpp cnguyen@hydra0.eecs.utk.edu:~/

Conclusion

  • Each section provides users quick access notes and demonstrations tailored to UNIX command execution and essential programming operations through the terminal, enhancing productivity and efficiency in a programming environment.