% Dear author,
% Please ensure ITC, Debian, Gold building, and Canvas are still relevant.

\documentclass[11pt]{cselabheader}

% Define title and author
\newcommand{\thelabnumber}{0}
\newcommand{\thetitle}{An Introduction to Linux}
\newcommand{\theauthor}{CSE/IT 107L}

% Write title and author
\title{\thetitle}
\author{\theauthor}
\date{NMT Department of Computer Science and Engineering}

\fancyhead[R]{Lab \thelabnumber: \thetitle}
\fancyhead[L]{\theauthor}

\makehyperref
\makeindex[title={Index of New Terminology, Programs, and Linux Commands}, intoc]

\begin{document}

\pagenumbering{roman}
\maketitle
\hrule

\begin{quotation} ``...the Linux philosophy is 'laugh in the face of danger'. Oops. Wrong one. 'Do it yourself'. That's it.'' \end{quotation}

\begin{flushright} --- Linus Torvalds (inventor of Linux) \end{flushright}\begin{quotation} ``Avoid the Gates of Hell. Use Linux.'' \end{quotation}

\begin{flushright} --- Unknown \end{flushright}

\vspace{1em}
\hrule

\begin{figure}[H]
  \centering
  \includegraphics[width=0.65\textwidth]{img/xkcd_272.png}
  \caption{\url{http://xkcd.com/272}}
\end{figure}

\begin{figure}[H]
  \centering
  \includegraphics[width=0.75\textwidth]{img/xkcd_456.png}
  \caption{\url{http://xkcd.com/456}}
\end{figure}

\hrule
\newpage

\section*{Introduction}

This lab will introduce you to the Linux operating system and its command line
interface. You will learn log in, navigate the file system, and create files
and directories.  Part of this lab involves reading The Linux Command Line, a
book available online.  You'll complete the lab after you finish some
exercises, create a compressed file called a tarball and submit it to Canvas.
It is important that you understand the concepts behind the actions you take
--- you will be repeating them throughout the semester. If you have any
questions, ask the instructor, teaching assistant, or lab assistant.

\tableofcontents

\newpage
\pagenumbering{arabic}

\section{Log In}

If your lab computer is displaying a Windows login screen or you have logged
into Windows, restart it and select Debian (a Linux distribution) as your
operating system. Please note that you are required to use \texttt{Linux} tools
for all labs in this class.  To log into\index{log in} an ITC machine, you must
use your ITC username and password.  If you do not have an ITC account, you
must visit the ITC office (Gold building) to have one activated.

You may also use the Windows program PuTTY\index{PuTTY} to use your Linux
system. Enter ``rainbow.nmt.edu'' as the hostname and click ``Open.'' This
should open a Linux terminal on your Windows desktop.  If you are logged in to
Windows on an ITC machine you may access your files by clicking on ``Computer''
in the Start menu and clicking on the \texttt{U:} drive.

Please be aware that we will use the Python modules Turtle and
matplotlib in later labs, these require graphics which are not
available through PuTTY.

\section{Terminal}

In Linux, you will perform most actions from the command line. In order to do
this, you must first open a terminal emulator, or terminal\index{terminal}.
The following is just one of many methods for doing this.

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\tightlist
\item
  Click on the \texttt{Menu} button at the bottom left of the screen.
\item
  In the text box that pops up as part of the menu, type in
  \texttt{Terminal} and click the application named ``Terminal.''
\end{enumerate}

The terminal provides access to a command interpreter, also known as
a shell\index{shell}. It is where you type text-based commands that are used to
control the computer and run programs.
On other operating systems you would use your mouse and the graphical
user interface (GUI) to use the same features that these commands
provide.

When you open the terminal prompt, you will probably see something like
this:

\begin{bashcode}
[username@machinename ~]$ youcantypehere
\end{bashcode}

This is where you type commands. We will be shortening this prompt to
just a single \$ to denote a terminal prompt for the rest of this
semester's labs.

Throughout this semester's labs, we will be using specially formatted
text to aid in your understanding. For example,

\begin{bashcode}
$ text in a console font within a gray box
\end{bashcode}

is text that either appears in the terminal or that you should type into
the terminal (almost) verbatim. Even the case is important, so be sure to keep it the
same! However, a \$ is used that to indicate that the code is from a
terminal prompt. You do not type \$, only everything after it.

Text in a \texttt{simple console font (like this)}, without a frame or background,
indicates the name of a file, a function, or some action you need to
perform.

\section{The Linux Command Line}

For this lab, we will refer to the Chapters 1 to 6
of The Linux Command Line by William Shotts. This book is available online
as a website at:

\begin{center}
  \url{http://linuxcommand.org/lc3_learning_the_shell.php}
\end{center}

All commands that you type in a shell are structured in the following
way:

\begin{bashcode}
$ commandname -options arguments
\end{bashcode}%$

The command name comes first, followed by options that modify its behavior,
and the arguments appear last. We will explore commands that take files
(including Python programs) and directories as arguments.
Remember that the \$ just denotes a terminal prompt.

There can be any number of options and arguments, separated by spaces.
For example, if the command is \texttt{python3} and you want it to run
the Python program \texttt{hello.py} and you want to enter interactive mode after
the file is run, you would type

\begin{bashcode}
$ python3 -i hello.py
\end{bashcode}%$

in your shell. Options and arguments vary by the specific command. (Do
not worry, we will explain what this specific command does later.)

\subsection{Navigation}

\subsubsection{\textbf{P}rint the \textbf{W}orking
  \textbf{D}irectory with the \texttt{pwd} Command}

The directory you are currently in is called your ``working'' directory\index{working directory}.
The command called \bashindex{pwd} \textbf{p}rints the \textbf{w}orking
\textbf{d}irectory. Here is an example of running this command:

\begin{bashcode}
$ pwd
/u/yourusername
\end{bashcode}%$

\subsubsection{\textbf{C}hange the Working
\textbf{D}irectory with the \texttt{cd} Command}

\bashindex{cd} allows you to change your working directory to another
location.

This is how it is used:

\begin{bashcode}
$ cd <path_name>
\end{bashcode}

In the following example, we will change the working directory to
the directory \texttt{/usr/bin}. Again, we type the command and the
path we would like to go to and then press ``enter.''
We also use the previously introduce command to make sure we have
changed directories.

\begin{bashcode}
$ cd /usr/bin
$ pwd
/usr/bin
\end{bashcode}

Using a special argument \bashindex{cd -} returns you to your previous
working directory. 

The \texttt{-} is called a command line argument or option.

To move up one directory (one node) level use dot-dot:

\begin{bashcode}
$ cd ..
\end{bashcode}

Where are you?

To move up two levels use dot-dot slash dot-dot:

\begin{bashcode}
$ cd ../..
\end{bashcode}%$

To go to your home directory you can type

\begin{bashcode}
$ cd /u/yourusername
\end{bashcode}

You can check that you are in your home directory by using the
\texttt{pwd} command.

\begin{bashcode}
$ pwd
/u/yourusername
\end{bashcode}

Alternatively, use a tilde (\bashindex{cd ~})

\begin{bashcode}
$ cd ~
\end{bashcode}

Even easier than all these options is to just type \texttt{cd} without
any options. This always takes you to your home directory.

\begin{bashcode}
$ cd
\end{bashcode}

\begin{longtabu}[c]{@{}ll@{}}
\toprule
Argument to \texttt{cd} & Meaning\tabularnewline
\midrule
\endhead
path & change directory to the supplied path\tabularnewline
.. & change directory to parent directory\tabularnewline
\(\sim\) & change directory to home directory\tabularnewline
- & change directory to previous working directory\tabularnewline
no argument & change directory to home directory\tabularnewline
\bottomrule
\end{longtabu}

Read Chapter 2 of The Linux Command Line to learn more about the \texttt{pwd}
and \texttt{cd} commands.

\subsubsection{File Hierarchy}\label{file-hierarchy}

Most of the information stored on your computer appears in
files\index{files} and directories\index{directories} (directories are
called folders\index{folders} on Windows.)  In Linux, they are all stored somewhere
within the root directory \texttt{/}.  The files and directories on
Linux machines are known as the file system tree\index{file system
tree}.

A path\index{path} consists of the names of directories and is a way to navigate
from the root file system to a specific directory or file. If you include the
root directory in the path this is called an absolute path. Another way
to navigate the tree is to use relative paths, which navigate the tree
based on the current directory.

An absolute path\index{path, absolute} starts at the root directory \texttt{/} and gives an
absolute description of which directory or file you are refering to. For
example, \texttt{/u/sgott/cse107/lab0/} refers to the directory as seen
in the file tree below. A relative path always references your current
directory. When you are in a shell, you always have a
\textit{current working directory} from which the relative path will go.

A relative path\index{path, relative} leaves off the first \texttt{/} and uses \texttt{..} to
refer to the parent of the current working directory and \texttt{.} to
refer to the current working directory. For example, if my current
directory is \texttt{/u/sgott/cse107/}, then \texttt{lab0} refers to the
absolute path \texttt{/u/sgott/cse107/lab0/}, while \texttt{../} refers
to the absolute path \texttt{/u/sgott/}, and \texttt{../../tcecil}
refers to the absolute path \texttt{/u/tcecil/}.

For example, this is a file system tree that resembles what you
might find on the school's ITC computers.
Absolute file paths are shown on the right, and a tree
representation of the entire file system is shown on the left.

\tikzstyle{every node}=[draw=black,thick,anchor=west]
\tikzstyle{selected}=[draw=red,fill=red!30]
\tikzstyle{optional}=[dashed,fill=gray!50]

\begin{center}
\begin{tikzpicture}[
grow via three points={one child at (0.5,-0.7) and
two children at (0.5,-0.7) and (0.5,-1.4)},
edge from parent path={(\tikzparentnode.south) |- (\tikzchildnode.west)}]
\node {/}
child { node [label={[xshift=4.5cm, yshift=-0.58cm] /bin}] {bin} }
child { node [label={[xshift=4.5cm, yshift=-0.58cm] /dev}] {dev} }
child { node [label={[xshift=4.5cm, yshift=-0.58cm] /tmp}] {tmp} }
child { node [label={[xshift=4.5cm, yshift=-0.58cm] /u}] {u}
child { node [label={[xshift=3.95cm, yshift=-0.58cm] /u/sgott}] {sgott}
  child { node [label={[xshift=3.4cm, yshift=-0.58cm] /u/sgott/cse107}] {cse107}
    child { node [label={[xshift=2.85cm, yshift=-0.58cm] /u/sgott/cse107/lab0}] {lab0} }
  }
}
child [missing] {}
child [missing] {}
child { node [label={[xshift=3.95cm, yshift=-0.58cm] /u/tcecil}] {tcecil}}
child { node [label={[xshift=3.95cm, yshift=-0.58cm]
/u/yourusername},optional] {yourusername}}
}
child [missing] {}
child [missing] {}
child [missing] {}
child [missing] {}
child [missing] {}
child { node [label={[xshift=4.5cm, yshift=-0.58cm] /usr}] {usr}
child { node [label={[xshift=3.95cm, yshift=-0.58cm] /usr/bin}] {bin} }
}
child [missing] {}
;
\end{tikzpicture}
\end{center}



\subsection{Looking Around}
Read Chapter 3 of The Linux Command Line to learn more about the
\texttt{less} command for viewing a file, and the \texttt{file} command for
classifying a file's contents.

\subsubsection{\textbf{L}i\textbf{s}ting the Contents of a
Directory with the \texttt{ls} Command}

The command \bashindex{ls} allows you to list the contents of a directory,
that is, the files and directories that are located within.

\begin{bashcode}
$ cd
$ pwd
/u/yourusername
$ ls
<a list of file names and directories contained
with your working directory, which is currently
your home directory>
\end{bashcode}

Adding some command-line options lets you see other file information:

\begin{bashcode}
$ ls -aF
.emacs.d/ Downloads/ hello.py
\end{bashcode}

In Linux, any file whose name begins with a dot is a ``hidden'' file\index{hidden file}.
In the example, the directory \texttt{.emacs.d} is a hidden directory.
Most commands that list files in some way will hide files that begin
with a dot.

The \texttt{a} option shows hidden files (a.k.a. dot-files),
directories and the \texttt{F} option adds a \texttt{*} to the end of a
file if is an executable file and a \texttt{/} if it is a directory.
format.

\section{Recording Your Sessions with the \texttt{script} Command}\label{%
sec:script}

The \bashindex{script} command creates a file and writes the commands you
type and their output.  This is useful for keeping track of which
commands you've been using and their output.
For example,
\begin{bashcode}
$ ls
other files 
$ script test_output
Script started, file is test_output

$ pwd
/u/yourusername
$ cd /
$ ls
bin cdrom dev etc lib root sbin sys usr var
$ exit
Script done, file is test_output
$ ls
other files commands
\end{bashcode}%$

Note \texttt{exit} is used to end the recording session and save the
script file. Only use this command if you have started recording using
\texttt{script}.

\subsection{Manipulating Files and Directories}
Read about the \texttt{cp} command to copy files, the \texttt{mv} command to
move files, and the \texttt{rm} command to remove files in Chapter 5 of the
Linux Command Line.

\subsubsection{\textbf{M}a\textbf{k}e \textbf{Dir}ectories
with the \texttt{mkdir} Command}

Directories are very important for organizing your work, and you should
get in the habit of creating a new directory for every lab this
semester.

To create a directory, use the \bashindex{mkdir} command as such:

\begin{bashcode}
$ mkdir newdirectorypath
\end{bashcode}%$

For example, if you are in your home directory \texttt{/u/yourusername}, you
can create the directory \texttt{cse107} in two ways:

\begin{description}
  \item[Absolute path] \hfill

    \begin{bashcode}
$ mkdir /u/username/cse107
    \end{bashcode}

  \item[Relative path] \hfill

    \begin{bashcode}
$ mkdir cse107
    \end{bashcode}
\end{description}

\subsection{Working With Commands}

\subsubsection{Read the \textbf{Man}ual with the \texttt{man} Command}

To view the manual for any command, use \bashindex{man}

\begin{bashcode}
$ man commandname
\end{bashcode}

For example,

\begin{bashcode}
$ man ls
NAME
       ls - list directory contents

SYNOPSIS
       ls [OPTION]... [FILE]...

DESCRIPTION
       List  information  about the FILEs (the current directory by default).
       Sort entries alphabetically if none of -cftuvSUX nor --sort is  speci‐
       fied.
       ...
\end{bashcode}

This shows the documentation for the \texttt{ls} command. A man page will
usually include a \textit{synopsis} that tells you all the possible
options and usages of the command followed by a detailed
\textit{description}, which is followed by a section that explains all
the \textit{options}. There can be many other sections in a man page,
but most man pages will have at least these three.

Please try to make use of man pages before you ask for help --- our first
suggestion regarding anything that involves Linux commands will typically be
for you to use the man page.

\subsection{Habits}
It's a good idea to have a directory for each lab inside a directory for the
class. For example:

\begin{tikzpicture}[
grow via three points={one child at (0.5,-0.7) and
two children at (0.5,-0.7) and (0.5,-1.4)},
edge from parent path={(\tikzparentnode.south) |- (\tikzchildnode.west)}]
\node {/}
child { node [label={[xshift=4.5cm, yshift=-0.58cm] /u}] {u}
child { node [label={[xshift=3.95cm, yshift=-0.58cm] /u/sgott}] {sgott}
  child { node [label={[xshift=3.4cm, yshift=-0.58cm] /u/sgott/cse107}] {cse107}
    child { node [label={[xshift=2.85cm, yshift=-0.58cm] /u/sgott/cse107/lab0}] {lab0} }
    child { node [label={[xshift=2.85cm, yshift=-0.58cm] /u/sgott/cse107/lab1}] {lab1} }
    child { node [label={[xshift=2.85cm, yshift=-0.58cm] /u/sgott/cse107/lab2}] {lab2} }
  }
}
}
child [missing] {}
child [missing] {}
child [missing] {}
child [missing] {}
child [missing] {}
child [missing] {}
;
\end{tikzpicture}

\section{Editing Files}

An easy to use graphical file editor is \textit{gedit}\index{gedit}.
To open it, click on ``Menu'' in the lower-left hand corner of
you screen and type ``gedit''. Press enter to run the program.

\begin{multicols}{2} 
\begin{figure}[H]
  \centering
  \includegraphics[width=0.48\textwidth]{img/gedit_editing.png}
  \caption{An example editing session.}
\end{figure}
\begin{figure}[H]
  \centering
  \includegraphics[width=0.48\textwidth]{img/gedit_pref.png}
  \caption{Useful settings for editing Python scripts using \texttt{gedit}.}
\end{figure}
\end{multicols}

\textit{notepad++} is an editor similar to \textit{gedit} is available
exclusively on Windows.  You may also type the commands \texttt{vim} or
\texttt{emacs} to edit files from the terminal.
There are many other learning resources for both Vim and Emacs. Ask a
TA, grader or tutor if you want to learn more about these editors.

\begin{longtabu}[c]{@{}lll@{}}
\toprule
Text editor & Download & Learning Resource\tabularnewline
\midrule
\endhead
\textit{gedit} & & \tabularnewline
\textit{Vim} & \url{http://www.vim.org} & \url{https://www.openvim.com} \tabularnewline
\textit{Emacs} & \url{https://www.gnu.org/software/emacs} & \url{https://emacs.sexy} \tabularnewline
\textit{notepad++} & \url{https://notepad-plus-plus.org} & \tabularnewline
\end{longtabu}

\newpage

\section{Exercises}\label{exercises}
See Section~\ref{sec:tar} to learn how to compress your files and submit them.

\begin{ex}[commands]
  This exercise is easier to perform if you use the \texttt{script}
  command for recording your commands.
  
\begin{enumerate}
\item Start recording your commands and all their output to a file
named \texttt{commands}. See Section~\ref{sec:script} for
instructions on using \texttt{script} to record your commands.
\item Print your current working directory.
\item Within this directory, create a new directory called \texttt{a/}.
\item Create empty files called \texttt{a1} and \texttt{a3} within
the directory \texttt{a/}.
\item Create the hidden and empty file called \texttt{.b2}.
\item Remove the file \texttt{a3}.
\item Copy the file \texttt{a1} to a new file \texttt{a2}.
\item Create a new directory called \texttt{b/} within \texttt{a/}.
\item Move the file \texttt{.b2} to this new directory.
\item List the files and directories in the directory \texttt{a}, including hidden files.
\item Finish recording your commands.
\end{enumerate}
\end{ex}

\begin{ex}[tour.txt]
See Chapter 4 in The Linux Command Line.
Explore at least three of the directories located in \texttt{/} and listed
as an ``Interesting directory'' in the textbook.
and write a short description of the contents of at least three files or
directories within. For example:

\begin{verbatimcode}
1. /etc/passwd is a plain text file that contains information for each user
2. /usr/share/man is a directory that stores man pages for installed software
3. /proc/cpuinfo is a special file that contains information related to the CPU 
\end{verbatimcode}
\end{ex}

\begin{ex}[info.txt]

Use gedit or any other text editor to create the file \texttt{info.txt}
and type your answers to the following questions:

\begin{enumerate}
\item How can you see more command line options for the \texttt{ls}, \texttt{man}, and \texttt{mkdir} commands?
\item What is the \texttt{ls} option to sort contents of a directory by file size?
\item Suppose you want to create the directory \texttt{~/a/b/c/} but the directory
      \texttt{a} does not exist. There's a way to create the directory using an
      option for \texttt{mkdir} that makes it create parent directories as needed.
      What is this \texttt{mkdir} option?
\end{enumerate}

Here's a sample question and answer: 

\begin{verbatimcode}
What is the man option for searching for a name in the manual page names?
-f
\end{verbatimcode}

\end{ex}

\section{Prepare Exercise Files: File Compression With \texttt{tar}}\label{%
sec:tar}

You need to submit your work as a tarball file with extension
\bashindex{.tar.gz}, which is a compressed file (like a \texttt{.zip}
file). It should contain all files used in the exercises for this lab.
All required files and the filename of the tarball are listed at the end
of the lab.

To create a tarball file, we will use the Tar (\bashindex{tar}) program. This command has
a similar function to zipping utilities on other operating systems: the
program combines many files and/or directories into a single, smaller file.

\subsection{Create a Tarball}
Ensure you are in your \texttt{lab0} directory, then type the following
command, replacing \texttt{firstname} and \texttt{lastname} with your
first and last names:

\begin{bashcode}
$ tar czvf cse107_firstname_lastname_lab0.tar.gz commands tour.txt info.txt
\end{bashcode}
% $

For other labs, replace \texttt{lab0} with \texttt{labX} and list all of the
files you need to submit instead.

This creates the file
\texttt{cse107\_firstname\_lastname\_lab0.tar.gz} in the
directory. The resulting archive includes the files named
\texttt{commands}, \texttt{tour.txt}, and \texttt{info.txt}.
The archive file is called a \emph{tarball}\index{tarball}.

\subsection{Check the Contents of a Tarball}
To check the contents of your tarball, run the following command:

\begin{bashcode}
$ tar tvf cse107_firstname_lastname_lab0.tar.gz
\end{bashcode}
% $

You should see a list of the output files that you compressed.

\subsection{Command Line Options}\label{command-line-options}

\begin{longtabu}[c]{@{}ll@{}}
\toprule
Option & Purpose\tabularnewline
\midrule
\endhead
\texttt{-c} & create an archive\tabularnewline
\texttt{-x} & extract an archive\tabularnewline
\texttt{-t} & list files in an archive\tabularnewline
\texttt{-z} & compress using gzip\tabularnewline
\texttt{-f FILENAME} & specify the archive name\tabularnewline
\texttt{-v} & list which files are processed\tabularnewline
\bottomrule
\end{longtabu}

\subsection{Uploading to Canvas}\label{uploading-to-canvas}

Now that you have your tarball ready, you may submit it to Canvas.
Navigate to \url{https://nmt.instructure.com}, login, and select the
CSE 107L class.
You should see ``CSE/IT-107L'' in the upper-left hand corner of the webpage.
Click on ``Assignments'', find the correct assignment and upload your tarball.
Please note that graders will post the rubric on your assignment, so
check the assignment once you have received a grade to read the rubric.

% Describes files to submit
\newpage
\printindex
\vfill
\input{submitting}

See Section~\ref{sec:tar} to learn how to compress your files.

\end{document}
