 SPRING 2025                   November 12, 2024
------------------------------------------------

If you are not familiar with linux, go to the
Course web page and navigate to section B3
"Other Reference Material".
The first two links contain tutorial to Linux.
The third link is the YWCC@NJIT Computing 
Requirement document.
The last two links are to documents  prepared 
by the instructor. One discusses connectivity to
a Unix/Linux machine at NJIT. The other is a
compact Unix and Linux tutorial.


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. 
One can thus distinguish between the Linux kernel
and Linux OS. The Android OS uses the Linux
kernel as a kernel!

There are additional services other than 
the Linux kernel provided by the Linux OS. 
These include program execution, 
program 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 and/or
the Linux OS. 
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. 
For example a user logs into a computer hosting
the Linux OS by using a secure shell client. 
Such a client can be MobaXterm or the Windows
provided ssh on a Windows machine, or 
ssh through a Terminal window of a Linux
or Mac OSX computer.

The bash shell 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 (the right of) the 
command prompt 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 and they are white space separated. 
White space is defined to be one or more space
characters or TAB characters or similar 
characters.
The three or more parts define the command line. 

********************************************
When the command is formed the user presses 
the ENTER key to tell the shell that the 
user is done typing and that the user 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, as long as it is
legitimate and within the security authorizations
of the user who typed the command (including
its options and arguments).
If there is any problem, an error or warning
message will be displayed on the screen
(usually of the window or terminal of 
interaction).


B. An 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 displayed 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, then and only then 
does the user types/presses the ENTER 
button after the 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 white 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 exist
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 the
CURRENT (working) DIRECTORY. 
Thus ls and ls .  do the same thing, they list 
the contents (files of any type) of the 
current directory.
An

 % ls ..

lists the contents of the parent directory
to the current directory. And

 % ls ../..

prints the contents  of the parent of the
parent to the current directory. There is a
hierarchical structure in the file 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 current (working) 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 provided and 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

For specific classes such as our, alternatives
host names such as 
 osl1.resource.campus.njit.edu
might be used. And the 1 of osl1 can be replaced
by other digits or numbers in the range 1-32.

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.

More information is provided in section B3 of
the course Web-page link 
"Connecting to Unix/Linux at 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, 
or the window of a MobaXterm 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 would 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 Nov 12 15:40:11 EST 2024

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
    (Effective 2024 Fall this step might not work.)
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.
---------------------------------------------------------
