 Fall   2024                       August 22, 2024
--------------------------------------------------

If you are not familiar with linux, go to the
Course web page and navigate to section B
subsection B3 "Other Reference Material".
The last two documents were prepared by the 
instructor. Linux machine connectivity has been
fluid at NJIT in 2024 so things change and can
change.


A. What is a shell ?

Although Linux is a distinct flavor of Unix (as in
the Unix operating system) several times we would
be using the terms Unix and Linux interachangeably.
Beware of this.

The Linux kernel is the part of the Linux Operating
System (OS) that runs all the time and maintains the 
data structures required for the proper operation of 
the OS.  There are additional services other than 
the Linux kernel provided by the Linux OS: 
program execution, compilation tools or 
toolchains, file management, etc.

A shell wraps around the linux kernel and provides
an interface to users of (some) of the facilities 
of the linux kernel. 
There are multiple shells available
in the linux OS but by default the shell that we
will be using would be the bash shell.

The bash shell runs when a user logs on a Linux OS 
system through the command line (eg by using a secure
shell client)  or by opening a Terminal window.  
It prompts the user to provide input to it 
(shell).
The shell indicates so by providing a command prompt 
(a character or sequence of characters) that indicates
that the shell is attentive to the user and next to
the prompt where user input will be typed a cursor 
might appear in the form of a (bright) rectangle. 
The command prompt is user modifiable: my command prompt that 
i have selected to have is the percent sign %.

A user types in next to (to the right of) of the 
command prompt starting at the cursor a command, 
its options, if any, and the arguments of the command. 
Options are usually English alphabet letters preceded by a
dash (-) and arguments follow the options. The
three or more parts define the command line and are
white space separated (by one or more spaces but 
also tabs are ok several times). 

********************************************
When the command is formed the user presses 
the Enter key to tell the shell that is done
typing and is requesting the 'execution' of
the user typed command.
********************************************

This tells the shell that the text of the command
has been typed in full, and directs the shell
to interpret the command's line and execute the
command with the given options and 
on the arguments provided.


B. En example


 An example of a command is given below


  % ls -lS file1 file2

    ^  ^   ^     ^
    |  |   |     |
    |  |   |- two argument to ls
    |  |      (file1 and file2)
    |  |
    |  |- option indicator is -
    |     (options typed are l and S)
    |- Command  is ls short for list

In this example % or % followed by a space is 
the command prompt and are typed by the bash
shell, not the user.
The user starts the typing with the ell of ls
and ends the typing with the 2 of file2.

When the user verifies visually that all is 
syntactically correct,
the user types/presses the Enter button after the
argument file2.

The command typed is ls short for list. 
It lists the contents of the file arguments of ls.
There are two file arguments and these are
file1 and file2. Note the space or spaces
between ls, -lS, file1, and file2. The number
of space is irrelevant. It is not clear whether
there is or are spaces after file2 or other
non visible characters such as the Tab in between
the four parts of the command. 
Another invisible character is the Enter button.

The character - indicates the presence of
letter options following it for command ls:
these options are l and S in this example.
Option l means a long listing of information
is requested  and ls interprets this by
printing one full line of information about each
one of its (file) arguments.
Option S is a request that the file arguments are
sorted/ordered by file size.

The two arguments of command ls are file1 and
file2. If the files with aliases (some call
them filenames) file1 and file2 do not exists
an error message will be reported when the 
command is processed by the shell.


A simple version of this command is
 %  ls

In this case ls PRINTS short information about
all the files that reside in the CURRENT DIRECTORY.
The current directory is the directory associated
with the location of the execution of command ls.
When we login to Linux or Unix our default
location is our home directory.

If one types
 % ls .

the output would be the same. In linux a period is
an alias for CURRENT DIRECTORY. Thus ls and
ls .  do the same thing, they list the contents
(files) of the current directory.
An

 % ls ..

lists the contents (files) of the parent directory
to the current directory. And

 % ls ../..

prints the contents (files) of the parent of the
parent to the current directory. There is a
hierarchical structure in the files system
in the form of a tree


 Finally, an

 % ls ~

prints the contents(files) of the HOME directory.

  .  : alias for CURRENT WORKING directory
 ..  : alias for parent of CURRENT WORKING directory
  ~  : alias for HOME DIRECTORY

If you do not know where you are, type

 % pwd
where pwd is short for print working (current) directory

 % cd
i.e.
 % cd ~
changes directory (cd): in both cases we move to
the user's home directory.

Other variations
  % ls
  % ls .
  % ls -l
  % ls -a
  % ls ~


C. Other examples

 A simpler command is

  %  date

Type it in and press Enter when you are done with
typing. Explore also

  %  who

  %  w

If you have questions about a command type

  % man ls
or 
  % man who
or
  % man date
or just
  % man man

By now you understand that man is short for 
manual.



D. Executing a command

  % date

Above, I direct you to  type and execute date. 

This means following the command prompt of the 
bash shell (in the examples above my command prompt 
is the percenet % sign) you type date and after you 
complete the typing you press the Enter button. 

If you do not press the Enter button of the
keyboard nothing will happen.

The shell accepts your command along with its
options and arguments, interprets it and if no
errors are found it runs a program named after
the command (program date)
by feeding to them the options
and argument information as needed. 

In OS terminology a program in execution is called a 
process. (Nowadays linux prefers the more 
general term task that includes a
process but also a  thread which is a lightweight 
process.)


E. Unix connectivity

If you plan to use NJIT resources
connect to one of afslogin0.njit.edu or
afslogin1.njit.edu

This assumes you are alread inside NJIT. If you
are outside of NJIT, download install and activate
the CISCO VPN client and connect to NJIT.

From the command line of
the Microsoft Windows command prompt or Powershell terminal,
or the window of a Linux terminal,
or the window of a Mac OSX terminal, you
type

    ssh  UCID@afslogin0.njit.edu

where UCID is your specific NJIT UCID.
Note that your myUCID credentials consist of 
a login name (also known as UCID) and your
corresponding UCID password (or just password).

If for example your UCID is ab45 then you yould type

    ssh  ab45@afslogin0.njit.edu

Note that 
  (a) first you will be prompted to type you UCID password,
  
then
  (b) you  might be prompted for DUO 2FA 
      (two factor authentication),
and then 
  (c) an additional window might pop up requesting your
      UCID password again,
and
  (d) an additional window might pop up requesting a 
      DUO 2FA again.

The last two are if you want to be able to transfer files
to/from the Linux remote machine from/to you local machine
(Windows, or Apple, or even Linux).


F. List of items to practice with

Even if you are familiar with Unix and Linux,
it does not hurt to go through this list to
confirm familiarity with the commands
and their options listed.


1. Connect with an ssh client to Linux machine

   Identify the bash shell (shell to the kernel).
   It is one of possible shells that interact with
   the kernel. 

2. Identify the prompt of the shell
   Though mine is % yours might be different!

3. Type date and observe its output
   This means do the following or observe the
   output generated

 % date
Tue Dec 26 15:40:11 EST 2023

4. Type which date 
   and observe its output
5. Type man date 
   for a manual on the command date
6. Type man man 
   for the obvious!
7. Type w
8. Type ps
9. Type ps  -ef
    ps is the command  and
    e,f are its two options that will be used.
10. Type  ps  -ef | more
11. Type  ps  -ef | less
   more or less control the flow of the output of
   ps -ef. The pipe symbol | defines an UNNAMED
   PIPE in Unix. The output of ps -ef goes through
   the pipe and is sent to become the input of
   more or less. ps -ef maps to a Unix process;
   mor or less also maps to a Unix process. Thus
   the unnamed pipe allows the communication of 
   two Unix processes!

   One screen-full of ps -ef output  at a time 
   is displayed.  

   Control the flow with b (back) or SPACE (forward).
   Keyboard q allows you to quit i.e. stop the flow.

12. Type pwd
    pwd stands for print working directory

13. Type cd ~
    cs stands for change directory

14. Type pwd
15. Type cd ..
16. Type pwd
17. Type cd .. ; pwd
17. Type cd .. ; pwd
18. Type cd    ; pwd
19. Type cd /  
20. Type pwd
    By now you know you may type two 'commands' 
    in a line if you separate them with a 
    semicolon ;
21. Type  cd .  ; pwd
22. Type ls
     ls stands for list
23. Type ls -l
24. Type ls -al
25. Type ls -ial
     Option l is long listing
     Option a prints hidden files (usually . and ..)
     . is current directory
     .. is parent directory
     Option i prints the inode number (inode#) of 
     a file.  The inode# is the filename 
     (or namefile) of the file according to the 
     filesystem.

Example.
--------
  (in my file area)

% ls -ali example
315164982 -rw------- 1 alexg users 0 Dec 21 15:56 example

 315164982 : is the inode# ; the filesystem refers to 
             a file using this inode#.
             An inode# is an index into the inodeTable
             maintained on the disk where the file is
             stored. Index or offset 315164982 of the
             inodeTable contains metadata about the
             file in question.
 example  : is MY alias for 315164982 (we call it filename!)
 1        : The number of aliases to 315164982 is 1 
            indicated by the number one the left side of 
            alexg
  alexg   : the USER OWNER of the file (u)
  users   : the GROUP of the USER OWNER of the file (g)

26.  I have just created a second alias for the file.
     If one could type
% ls -lai example ex2
315164982 -rw------- 2 alexg users 0 Dec 21 15:56 ex2
315164982 -rw------- 2 alexg users 0 Dec 21 15:56 example

 Both ex2 and example are two aliases to the same 
 file 315164982 The number on the left of alexg is 2 now.

27. Type  man rm
    rm stands for remove (an alias) though some think
    that it stands for remove a file.
  % rm example
  % rm ex2
  if files example and ex2 exist for you, you may
  carefully delete them. If they do not exist an
  error will be reported by the bash shell.

28. DO NOT TYPE rm -rf  
    IT IS A DANGEROUS VERSION of rm !!!
    YOU HAVE BEEN WARNED

29. Type  touch afile
   creates a file afile (if it doesnot exist) of size 
   0 bytes.
  % touch example ex2
  will set you up to repeat step 27 without errors.

30. Type cp ex2 ex1
   cp  stands for copy ex2 into ex1. 
   (ex2 still exists.)

31. Type mv ex1 ex3
   ms stands for  mv (rename) ex1 into ex3.
   (ex1 disappears as it re-appears under 
   the alias ex3.)

32. Type gvim afile 
    to edit afile  with the gvim visual editor
33. Avoid using vi or vim instead of gvim.
    The first two use a disorienting non graphical
    interface that takes time to get used to.

34. Type md5sum afile
    It generates a cryptographic fingerprint of 
    file afile

35. Type mkdir aa
    mkdir stands for make directory
36. Type rmdir aa
    rmdir stands for remove (an empty) directory
    if not empty, empty it first and then rmdir
    or do a dangerous  rm -rf aa

37. Type tar  cvf  a.tar  a.txt b.txt
 or
    Type tar -cvf  a.tar  a.txt b.txt
    Archive into a tar (Tape ARchive) file the two files
    a.txt b.txt (if they both exist)
38. Type tar xvf a.tar
 or Type tar -xvf a.tar
    Unarchive i.e. obtain from a.tar all its
    constituent files (eg. a.txt b.txt)
39.  cvxf options of tar
     c : create
     v : verbose
     x : extract
     f : filename follows 

40. more , less, cat
  %  cat x     
    cat stands for catenate i.e. display contents
    of file (e.g. x) no flow control
    more, 
    less  are cat equivalent with flow control.

  % cat x y >>z
    z contains (the contents of) x followed by 
               (the contents of) y

41. Type  echo "mymy"
42. Type  echo $?
43. Type  echo $$
     Process ID of the 'current process running'
     is displayed: it is the Process ID of the bash
     shell. Yes the bash shell is a process just like
     those rm, ps, more, echo you have been running!
44. Type top
    to exit CTRL-Z
    CTRL-Z  means press the CTRL button and with the 
    button depressed press also the Z button of the 
    keyboard.

45. Type echo $path

46. Some fun with bash itself
  % echo $$
  % bash
  % echo $$
  % bash
  % echo $$

  We are running bash within bash!
  % exit 
  % echo $$
  % exit
  % echo $$
  You are back to the original bash

47. If you are done
  type an extra exit
  % exit
and you have terminated the bash shell that was
created by  ssh (secure shell).

48. You should be out of the system.
---------------------------------------------------------
