Introduction to the Linux Shell

This lecture is based on the [Unix Shell Lesson](http://swcarpentry.github.io/shell-novice/) by [Software Carpentry](https://software-carpentry.org/)
  • When you go to 127.0.0.1:9999 from your browser for the first time, it brings up the content of your home directory in the VM.
  • From now on, we will refer to this as the Jupyter Home

The Linux Shell

In the Jupyter Home Page

  • Click New on the top right corner
  • Click Terminal
  • You will be presented with a new tab
  • This is your Linux terminal into the CentOS VM
  • A terminal is a command-line interface that captures your keyboard interactions and sends them to the Linux shell.
  • A Linux shell accepts, inteprets, and sends the appropriate commands, as interpreted from your keyboard inputs, to the Linux operating system for actual execution.
  • There can be many different terminal programs.
  • There can be many different shell programs.
  • They vary in term of capabilities and visual presentations.
  • Switch back to the terminal tab
  • Hit Enter
  • Type ls -F / and hit Enter
$
$ ls -F /
bin@  boot/  dev/  etc/  home/  lib@  lib64@  media/  mnt/  opt/  proc/  root/  run/  sbin@  srv/  sys/  tmp/  usr/  var/
$
  • The first line is a prompt, indicating that the shell is waiting for input
  • There might be many different texts for the prompt, depending on individual users' configurations
  • A common notation for the prompt in literature is the \$ sign
  • In the second prompt, we entered a statement: ls -F /
  • This statement has three components:
    • ls is the command
    • -F is the flag, which represents the option selected for the ls command
    • / is the argument for the command
  • The components are separated by spaces
  • After the statement is evaluated and the results are printed to the terminal, we are returned to the third prompt.
  • Each command is a program.
    • We will deal with how these programs are located by the Linux OS later
  • Linux shell inteprets statements by following the REPL (read-evaluate-print-loop) strategy

REPL for ls -F /

  • Read the entire statement that was typed in (in this case ls -F /)
    • The shell uses spaces to separate the components of the statement
  • Evaluate
    • Find a program called ls
    • Pass the flag and argument to this program
    • Execute the program together with the flag and argument
  • Print the output produced by the program
  • Loop back and present a new prompt to the user for the next statement

Excercise

  • Switch to the terminal tab
  • Execute git clone https://github.com/linhbngo/Distributed-and-Cluster-Computing.git
  • Traverse to the Jupyter Home Page and open this notebook
  • The operating system executes the programs.
  • The actual contents (programming instructions and data) are stored in the file system.
  • The file system organized your data in directories (or folders) and files.
  • To execute the right program, you need to let the OS knows there the program is located in the file system (and where the data is located).
  • Where you (your terminal) currently are in the file system?
  • Switch to the terminal tab, type the command pwd and hit Enter.
  • pwd stands for path to working directory
  • Typically, users start out in their home directory.
  • Safe and secure space (most of the time!!!).
  • Users have full modification control over items (files and folders) in their home directory.
  • /home/<USER_NAME>
  • A statement (a string) describing the location of files or directories is called a path.
  • The forward slash (/) has two meanings:
    • If it stands alone, or if it starts as the first position in a path, then it represents the root directory.
    • The root directory is the master directory of a file system, which holds everything else in the file system.
    • If it appears in the middle of a path, then it acts as a separator.
  • First navigation command: ls
  • Stands for listing
  • Shows all files/directories in a location
  • Switch to the terminal tab and run ls from the prompt.
  • ls has many options.
  • To view the options of ls, run ls --help
  • --help is a standard flag in the majority of standard commands in the Linux OS. It shows the general options available to the commands.
  • If you want more in-depth discussion of this option, type `man ls.
  • Stands for manual.

Exercise

  • What does ls do when used with -l and -h flags?
  • What flag should I use if I want to
    • list the contents of nested directories?
    • list the contents of nested directories and sort them by order of time of last change?
  • Command cd is used to change directory
  • Shortcuts: ~: your home directory ..: directory one level above the current directory
  • Relative path: Room 147
  • Absolute path: Room 147, UNA Building, West Chester University, West Chester, Pennvylvania, USA, Earth, Solar System ...

Working with Files and Directories

  • mkdir: Make new directory
  • cp: Copy files or directories
  • rm: Remove files or directories
  • mv: Move files or directories to a new location
  • In-terminal editor: nano
    • ^: Ctrl
  • Anaconda-based editor:
    • From Jupyter Home Page, click New and then Text
    • The editor is in browser, supports keyword-hightlighting for the majority of programming languages
    • The file is created in the directory that the Home Page currently points to.
  • Remember to save!

Writing and compiling C codes in a Linux environment

Rough analogies:

  • Java versus C: English language versus German language
  • GUI IDE versus command line IDE: spoken language versus written language
  • Create a new text file from Anaconda text editor
  • Change the file name to hello.c
  • Copy the C content in the next cell and save:

In [ ]:
#include <stdio.h>
int main()
{
   // printf() displays the string inside quotation
   printf("Hello, World!");
   return 0;
}

In [ ]:
// This is for comparison against the previous cell only, do not save this into your hello.c file
public class HelloWorld {

    public static void main(String[] args) {
        // Prints "Hello, World" to the terminal window.
        System.out.println("Hello, World");
    }

}

To compile and run:

$ gcc -o hello hello.c
$ ./hello