diff --git a/HS23_ConsoleToolkit/main.pdf b/HS23_ConsoleToolkit/main.pdf new file mode 100644 index 0000000000000000000000000000000000000000..da9a70c2f4a688563bd86d0410ff57e3206e189e Binary files /dev/null and b/HS23_ConsoleToolkit/main.pdf differ diff --git a/HS23_ConsoleToolkit/main.tex b/HS23_ConsoleToolkit/main.tex new file mode 100644 index 0000000000000000000000000000000000000000..10abbdd9dca870eafcb4985b47ec2da8f268583b --- /dev/null +++ b/HS23_ConsoleToolkit/main.tex @@ -0,0 +1,1292 @@ +\documentclass[aspectratio=169]{beamer} + +% Local Imports +\usepackage{general} +\usepackage{slideStyle} +\usepackage{code} + +\usepackage{hyperref} + +% Global Imports +\usepackage{adjustbox} + +% List commands +\newcommand{\ipro}{\item[+]} +\newcommand{\icon}{\item[-]} +\newcommand{\ides}[1]{\item \textbf{#1}} +\newcommand{\iexa}[1]{\item \textbf{Example:} #1} + +% For permission list +\usepackage{array, threeparttable, booktabs} +\newcommand{\dir}{\textcolor{orange}d} +\newcommand{\syl}{\textcolor{red}l} +\newcommand{\rd}{\textcolor{purple}r} +\newcommand{\wri}{\textcolor{blue}w} +\newcommand{\exc}{\textcolor{teal}x} + +% Insert page for each part +\AtBeginPart { + \begin{frame} + \partpage + \end{frame} +} + +\title{The Console Toolkit} +\author{Jean-Claude Graf} +\institute{TheAlternative, ETH Zürich} +\date{11. October 2023} + +\begin{document} + \begin{frame} + \maketitle + \end{frame} + + \section{Introduction} + + \begin{frame}{Goals of This Course} + \begin{itemize} + \item Getting used to the console + \item Navigating the file system + \item Modifying files + \item Learn some basic commands + \item How to get help? + \item Learn to recognize patterns an apply them out in the wild + \end{itemize} + \end{frame} + + \begin{frame}{What is the Console?} + \begin{itemize} + \item ``Keyboard + Text'' interface to your computer + \item Related Terms + \begin{itemize} + \ides{Terminal (Emulator):} Synonym to console (today) + \ides{Shell:} SW running in terminal, processing I/O + \ides{Bash, ZSH:} Instances of shells + \ides{Command Line (Interface (CLI)), Command Prompt:} Text-only input + \end{itemize} + \end{itemize} + \end{frame} + + \begin{frame}{Why the Console?} + \begin{itemize} + \item Most direct interface to your computer + \item Great for advanced/complex tasks + \item Can easily be automated + \begin{itemize} + \item I.e. batch work + \end{itemize} + \item Similar on all Unix systems + \item The way to interact with other systems + \begin{itemize} + \item Remote servers + \item Containers + \end{itemize} + \item Last thing alive when you break your system + \end{itemize} + \end{frame} + + \begin{frame}[fragile]{Look, a Console!} + \SU{bob} + \begin{shell} +`\StartConsole` + \end{shell} + + \begin{itemize} + \item Displays different information + \begin{itemize} + \item Username, hostname, current working directory, git status, exit code, time, etc. + \end{itemize} + \item Can be customized + \end{itemize} + \end{frame} + + \part{Basics} + \section{File System Basics} + + \begin{frame}[fragile]{The 70s were interesting times, or so I'm told} + \textbf{Unix dogma: \emph{Everything is a file!}} \\ + \begin{columns}[T] + \column{0.5\textwidth} + \begin{itemize} + \item Files + \item Directories (or ``folders'') + \item Storage devices + \item Keyboards + \end{itemize} + \column{0.5\textwidth} + \begin{itemize} + \item Monitor + \item Battery + \item \emph{But not network sockets \ldots} + \end{itemize} + \end{columns} + \bigskip + \begin{itemize} + \ides{File:} Abstraction created by the OS + \item $\implies$ Unified interface + \end{itemize} + \end{frame} + + \begin{frame}[fragile]{The Root} + \begin{columns}[T] + \column{0.4\textwidth} + \SU{pwd=/} + \vspace{-0.6cm} + \begin{shell}[fontsize=\scriptsize] +ls `\StartConsole` +bin -> usr/bin +boot +dev +etc +home +lib -> usr/lib +lib64 -> usr/lib +mnt +proc +root +sys +tmp +usr +var + \end{shell} + \column{0.6\textwidth} + \begin{itemize} + \item File system organized as tree + \item Everything under \texttt{/}, the root directory + \item In the console, you will be at some point in the tree, + the \emph{working directory} + \end{itemize} + \end{columns} + \end{frame} + + \begin{frame}[fragile]{Working Directory} + \begin{columns}[T] + \column{0.5\textwidth} + \begin{itemize} + \item Where am I? \textrightarrow \: \texttt{pwd} + \item \textbf{P}resent \textbf{W}orking \textbf{D}irectory + \item Also sometimes directly shown in the prompt + \end{itemize} + \column{0.5\textwidth} + \SU{alice,pwd=/} + \begin{shell} +pwd `\StartConsole` +/ + \end{shell} + \SU{bob,pwd=/home} + \begin{shell} +pwd `\StartConsole` +/home + \end{shell} + \end{columns} + \end{frame} + + \begin{frame}[fragile]{Listing Files} + \begin{columns}[T] + \column{0.5\textwidth} + \begin{itemize} + \item What is in here? \textrightarrow \: \texttt{ls} + \item \textbf{L}i\textbf{s}t + \end{itemize} + \column{0.5\textwidth} + \SU{bob} + \begin{shell} +ls `\StartConsole` +Desktop +Documents +Downloads +Music +Pictures +Videos + \end{shell} + \end{columns} + \end{frame} + + \begin{frame}[fragile]{Changing Directory} + \begin{columns}[T] + \column{0.5\textwidth} + \begin{itemize} + \item I want to go somewhere else \textrightarrow \: \texttt{cd} + \item \textbf{C}hange \textbf{D}irectory + \end{itemize} + \column{0.5\textwidth} + \SU{bob} + \begin{shell} +cd Documents `\SU{pwd=~/Documents}` +`\StartConsole` + + \end{shell} + \end{columns} + \end{frame} + + \begin{frame}[fragile]{Special Names and Files} + \begin{columns}[T] + \column{0.5\textwidth} + \begin{itemize} + \item Special Names: + \begin{itemize} + \ides{Root (/):} Link to root + \ides{Home ($\sim$):} Link to user's home directory + \begin{itemize} + \item I.e. \texttt{/home/alice} + \end{itemize} + \end{itemize} + \item Special Files: + \begin{itemize} + \ides{Parent (..):} Link to parent directory + \ides{Current (.):} Link to itself + \end{itemize} + \end{itemize} + \column{0.5\textwidth} + \SU{alice} + \begin{shell} +cd / `\SU{pwd=/}` +cd ~ `\SU{pwd=\~}` +cd .. `\SU{pwd=/home}` +cd . +`\StartConsole` + \end{shell} + \end{columns} + \end{frame} + + \begin{frame}[fragile]{Paths} + \begin{columns}[T] + \column{0.45\textwidth} + \begin{itemize} + \ides{Path:} Refers to files + \begin{itemize} + \item Consists of names, separated by \/ + \end{itemize} + \item Two types: + \begin{itemize} + \ides{Absolut Path:} Start with \texttt{/} + \ides{Relative Path:} Relative to CWD + \begin{itemize} + \item I.e. starts with \texttt{.} + \end{itemize} + \end{itemize} + \end{itemize} + \column{0.55\textwidth} + \SU{bob} + \begin{shell} +cd /tmp/ `\SU{pwd=/tmp}` +cd /home/bob/ `\SU{pwd=\~}` +cd Documents/Eth `\SU{pwd=\~/Documents/Eth}` +cd ../Finance `\SU{pwd=\~/Documents/Finance}` +`\StartConsole` + \end{shell} + \end{columns} + \end{frame} + + \section{Command Basics} + + \begin{frame}[fragile]{Command Structure} + \SU{bob} + \begin{shell}[fontsize=\small] +command -shorOption --longOption argument1 argument2 + \end{shell} + \begin{itemize} + \ides{Command:} Name of executable + \ides{Short Option/Flag:} Single letter option + \ides{Long Option:} More memorable name for option + \ides{Arguments:} Additional information passed to the command + \end{itemize} + Not everyone follows this standard... + \end{frame} + + \begin{frame}[fragile]{Advanced Listing} + \begin{itemize} + \item \texttt{ls} Options: + \begin{itemize} + \item \texttt{-a} (\texttt{---all}): show hidden files + \begin{itemize} + \ides{Hidden File:} File whose name start with a dot + \end{itemize} + \item \texttt{-h} (\texttt{---human-readable}): print numbers in human readable format + \item \texttt{-l} (\texttt{---list}): show the long output format + \end{itemize} + \item Combine short options: \texttt{ls -l -a -h} $\Rightarrow$ \texttt{ls -lah} + \end{itemize} + \end{frame} + + \begin{frame}[fragile]{Advanced Listing} + \vspace{-0.3cm} + \SU{alice} + \begin{shell}[fontsize=\small] +ls -lah `\StartConsole` +total 52K +drwx------ 8 alice alice 4.0K Sep 3 23:27 . +drwxr-xr-x 4 root root 4.0K Sep 3 23:26 .. +-rw-r--r-- 1 alice alice 21 Jun 4 10:54 .bash_logout +-rw-r--r-- 1 alice alice 57 Jun 4 10:54 .bash_profile +-rw-r--r-- 1 alice alice 141 Jun 4 10:54 .bashrc +-rw-r--r-- 1 alice alice 0 Sep 3 23:27 bla.jpg +drwxr-xr-x 2 alice alice 4.0K Sep 3 23:27 Desktop +drwxr-xr-x 2 alice alice 4.0K Sep 3 23:27 Documents +drwxr-xr-x 2 alice alice 4.0K Sep 3 23:27 Downloads +-rw-r--r-- 1 alice alice 24 Sep 3 23:28 .hidden_file + \end{shell} + \end{frame} + + \begin{frame}[t,fragile]{Getting Help} + \begin{itemize} + \item Where can I find out what options are available? + \item Two Ways: + \begin{itemize} + \item \verb+--help+ option + \begin{itemize} + \item E.g. \verb+ls --help+ + \end{itemize} + \item \textbf{Man}ual pages + \begin{itemize} + \item E.g. \verb+man ls+ + \end{itemize} + \end{itemize} + \item (Bonus) Shell Completion + \end{itemize} + \end{frame} + + \begin{frame}[t,fragile]{Man Pages} + \begin{minipage}{0.6\textwidth} + % \begin{adjustbox}{height=0.95\textheight} + \begin{shellcomp}[fontsize=\tiny] +NAME + ls - list directory contents +SYNOPSIS + ls [OPTION]... [FILE]... +DESCRIPTION + List information about the FILEs (the current directory by default). Sort entries alphabeti- + cally if none of -cftuvSUX nor --sort is specified. + + Mandatory arguments to long options are mandatory for short options too. + + -a, --all + do not ignore entries starting with . + + -A, --almost-all + do not list implied . and .. + + --author + with -l, print the author of each file + + -b, --escape + print C-style escapes for nongraphic characters + \end{shellcomp} + \end{minipage} + \begin{minipage}{0.35\textwidth} + \begin{itemize} + \item{Search by typing \keys{/}} + \item{Quit by typing \keys{q}} + \item{Sometimes there are multiple manuals! \textrightarrow \: Choose the right section} + \begin{itemize} + \item[1:] Shell Commands + \item[2:] System Calls + \item[3:] Library Calls + \item etc. + \end{itemize} + \end{itemize} + \end{minipage} + \end{frame} + + \section{More on FS} + + \begin{frame}[t,fragile]{Copying Files} + \begin{columns}[T] + \column{0.3\textwidth} + \begin{itemize} + \item \textbf{C}o\textbf{p}y: \texttt{cp} + \item Syntax: \texttt{cp source destination} + \end{itemize} + \column{0.7\textwidth} + \vspace{-1cm} + \SU{alice} + \SU{pwd=~/Downloads} + \begin{shell} +ls `\StartConsole` +dir1 dir2 file1 file2 `\SU{}` +ls dir1 `\StartConsole` +`\SU{}` +cp file1 dir1 +ls `\StartConsole` +dir1 dir2 file1 file2 `\SU{}` +ls dir1 `\StartConsole` +file1 `\StartConsole` + \end{shell} + \end{columns} + \end{frame} + + \begin{frame}[t,fragile]{Moving Files} + \begin{columns}[T] + \column{0.3\textwidth} + \begin{itemize} + \item \textbf{M}o\textbf{v}e command: \texttt{mv} + \item Syntax: \texttt{mv source destination} + \item Use \texttt{mv} to rename files + \end{itemize} + \column{0.7\textwidth} + \vspace{-1cm} + \SU{alice} + \SU{pwd=~/Downloads} + \begin{shell} +ls `\StartConsole` +dir1 dir2 file1 file2 `\SU{}` +ls dir1 `\StartConsole` +`\SU{}` +mv file1 dir1 `\SU{}` +mv file2 file_new `\SU{}` +ls `\StartConsole` +dir1 dir2 file_new `\SU{}` +ls dir1 `\StartConsole` +file1 `\SU{}` + \end{shell} + \end{columns} + \end{frame} + + \begin{frame}[t,fragile]{Creating and Deleting Directories} + \begin{columns}[T] + \column{0.4\textwidth} + \begin{itemize} + \item \texttt{mkdir} creates a new directory + \item \texttt{rmdir} removes a directory + \begin{itemize} + \item Only works for empty directories + \end{itemize} + \end{itemize} + \column{0.65\textwidth} + \SU{alice,pwd=~/Downloads} + \begin{shell} +mkdir new_dir +ls `\StartConsole` +new_dir `\SU{}` +rmdir new_dir +ls `\StartConsole` +`\StartConsole` + \end{shell} + \end{columns} + \end{frame} + + \begin{frame}[t,fragile]{Deleting Files} + \begin{columns}[T] + \column{0.4\textwidth} + \begin{itemize} + \item \texttt{rm} removes files and directories + \item \texttt{-r} removes a directory and everything in it \emph{(recurisive)} + \item Is \textcolor{red}{irreversible!} + \end{itemize} + \column{0.65\textwidth} + \vspace{-1cm} + \SU{bob} + \begin{shell} +ls `\StartConsole` +cat1.jpg cat2.jpg `\SU{}` +rm cat1.jpg +ls `\StartConsole` +cat2.jpg + \end{shell} + \end{columns} + \vspace{0.5cm} + \begin{WarningBox} + \texttt{rm} is a shotgun without safety! There is no trashcan. + You can delete your entire file system with \texttt{sudo rm -rf /}, + or your entire home directory with \texttt{rm -rf \textasciitilde}\:!% + \end{WarningBox} + \end{frame} + + \begin{frame}[t,fragile]{Showing Text Files} + \begin{columns}[T] + \column{0.5\textwidth} + \begin{itemize} + \item Output a file's contents to the console with \texttt{cat} + \item Used to stand for \emph{concatenate} + \end{itemize} + \column{0.5\textwidth} + \SU{alice} + \begin{shell} +cat diary.txt `\StartConsole` +Dear diary, today I learned +how to use the console! + \end{shell} + \end{columns} + \end{frame} + + \begin{frame}[t,fragile]{Reading Long Files} + \begin{itemize} + \item What if the text doesn't fit on the terminal? + \item Use the \texttt{less} file viewer + \begin{itemize} + \item We have seen it be for for the man pages + \end{itemize} + \item Scroll up and down with \keys{\arrowkeyup}, \keys{\arrowkeydown} + \item Search by typing \keys{/} + \item Quit by typing \keys{q} + \end{itemize} + \end{frame} + + \begin{frame}[t,fragile]{Editing Files} + \begin{itemize} + \item Need a \emph{text editor}! + \item \emph{nano, vim, emacs} + \item Simple, intuitive, no learning required? \textrightarrow\:\texttt{nano} + \item Powerful, efficient? \textrightarrow\:\texttt{vim} + \item Obscure, eccentric and even more powerful? \textrightarrow\:\texttt{emacs} + \item Has some advantages to using a big GUI tool + \begin{itemize} + \item Navigation and editing in the same interface + \item Quick and efficient + \item Very powerful tools available + \end{itemize} + \end{itemize} + \end{frame} + + \begin{frame}[t,fragile]{Nano} + \begin{itemize} + \item Syntax: \texttt{nano [filename]} + \item Key bindings shown on the bottom + \begin{itemize} + \item \texttt{\textasciicircum} stands for the \keys{\ctrl} key (universal) + \end{itemize} + \item Save: \keys{\ctrl} + \keys{o} + \item Close: \keys{\ctrl} + \keys{x} + \item Navigate with arrow keys \keys{\arrowkeyleft} + \keys{\arrowkeydown} \keys{\arrowkeyup} + \keys{\arrowkeyright} + \end{itemize} + \end{frame} + + \begin{frame}[t,fragile]{Users \& Permissions} + \begin{itemize} + \item Linux is a \emph{multi-user operating system} + \item There can be many user accounts + \item Different users can even use the computer at the same time! + \item You usually only use your personal user account + \item Users can be put into groups + \begin{itemize} + \item Allows easy assignment of permissions + \end{itemize} + \end{itemize} + \end{frame} + + \begin{frame}[t,fragile]{Users} + \begin{columns}[T] + \column{0.5\textwidth} + \textbf{Personal User} + \begin{itemize} + \item{Home directory in \texttt{/home/alice}} + \item{Can only access files in home directory} + \item{Can only stop processes started by itself} + \end{itemize} + \column{0.5\textwidth} + \textbf{Root User} + \begin{itemize} + \item{Also \emph{called the superuser}} + \item{''System administrator``} + \item{Home directory in \texttt{/root}} + \item{Can do anything on the system} + \item{Access to all files} + \item{Can kill any process} + \end{itemize} + \end{columns} + \end{frame} + + \begin{frame}[t,fragile]{Permissions} + \SU{alice,pwd=~/SomeDir} + \begin{shell} +ls -lah `\StartConsole` +drwxr-xr-x 115 root root 12K Apr 3 13:24 etc +drwxr-xr-x 4 alice alice 4.0K Nov 9 2019 home +lrwxrwxrwx 1 root root 7 Dec 7 03:41 lib -> usr/lib +-rwxr-xr-- 1 alice alice 64 Nov 29 12:34 .fehbg +-rw-r--r-- 1 alice alice 25M Jan 27 2020 script.sh + \end{shell} + \end{frame} + + \begin{frame}[t,fragile]{Permissions} + \begin{table}[!htbp] + \centering\sffamily + \begin{threeparttable} + \begin{tabular}{*{4} {>{\centering\arraybackslash}p{1.2cm}}} + \toprule + type & users & group & others \\ + \cmidrule(lr){1-1}\cmidrule(lr){2-2}\cmidrule(lr){3-3}\cmidrule(lr){4-4} + \dir \enspace\syl & \rd\enspace\wri\enspace\exc & \rd\enspace\wri\enspace\exc& \rd\enspace\wri\enspace\exc \\ + \bottomrule + \end{tabular} + \smallskip + \begin{tablenotes}[flushleft, online]\small + \item[\dir:] Is a directory. + \item[\syl:] Is a symbolic link. + \item[\rd:] Can Read,\, Read files. + \item[\wri:] Can Write,\, Create files. + \item[\exc:] Can Execute,\, List files. + \end{tablenotes} + \end{threeparttable} + \end{table} + \end{frame} + + + \begin{frame}[t,fragile]{Changing permissions} + \begin{itemize} + \item Change permission: \verb$chmod {u,g,a}{+,-}{r,w,x} file$ + \item Change user/group: \verb$chown OWNER:GROUP file$ + \end{itemize} + \SU{alice} + \begin{shell} +chmod +x program.sh +chmod u-x program.sh +chmod g+rw file.txt +chown alice:alice file.txt + \end{shell} + \end{frame} + + \begin{frame}[t,fragile]{Octal Permissions} + \begin{itemize} + \item Change permission: \verb+chmod OCTALMODE file+ + \begin{itemize} + \item \texttt{4: read} + \item \texttt{2: write} + \item \texttt{1: execute} + \end{itemize} + \item Octal representation allows setting all permissions in one go + \item Desired permissions are added up + \iexa{} \texttt{7 = read + write + execute} + \end{itemize} + \SU{bob} + \begin{shell} +chmod 776 program.sh +chmod 774 program.sh + \end{shell} + \end{frame} + + \section{Efficiency} + + \begin{frame}[t,fragile]{Tab Completion} + \begin{itemize} + \item Hit \keys{\tab} to complete a word you are typing (Command, flag, file, \dots) + \item Hit \keys{\tab} twice to show all possible options + \begin{itemize} + \item Use \keys{\tab} to cycle through them + \end{itemize} + \item Extremely useful terminal feature! Use always! + \end{itemize} + \end{frame} + + \begin{frame}[t,fragile]{Command History} + \begin{itemize} + \item Scroll up in your command history by pressing the \keys{\arrowkeyup} key + \item Press \keys{\ctrl} + \keys{r} to search the history + \end{itemize} + \end{frame} + + \begin{frame}[t,fragile]{Globbing} + \begin{columns}[T] + \column{0.4\textwidth} + \begin{itemize} + \item Match zero or more characters: \texttt{*} + \item Match a single character: \texttt{?} + \item Define a range: \texttt{[start-end]} + \begin{itemize} + \item Combine with \texttt{*} or \texttt{?} + \end{itemize} + \item Define several globbings: \texttt{\{glob1, glob2, ...\}} + \end{itemize} + \bigskip + Different Shells have different features + \column{0.65\textwidth} + \SU{alice,pwd=~/Eth} + \begin{shell} +rm -rf ana* +mv ex-1?.pdf exs/ +mv ex-[0-9]*.pdf exs/ +mv ../{IML, IS} sem6 +rm {*.doc,*.docx} + \end{shell} + \end{columns} + \end{frame} + + \begin{frame}[t,fragile]{Chaining Commands} + \begin{itemize} + \item Run next command iff current succeeds: \texttt{\&\&} + \item Run next command in any case: \texttt{;} + \item Pass stdout to stdin: \texttt{|} + \begin{itemize} + \item More on that later + \end{itemize} + \end{itemize} + \SU{bob} + \begin{shell} +mkdir test && cd test +docker-compose down ; docker-compose up -d + \end{shell} + \end{frame} + + \begin{frame}[t,fragile]{Other} + \begin{itemize} + \item Kill currently running process: \keys{CTRL} + \keys{C} + \begin{itemize} + \item Also useful to discard typed command + \end{itemize} + \item Different terminals/shells provide different features + \item Fast movement + \begin{itemize} + \item Got to: end of line, beginning of line + \item Move: one word back, one word forward + \end{itemize} + % \item \todo{Move task to bg etc.} + \end{itemize} + \end{frame} + + \part{Advanced} + \section{Piping and Redirection} + + \begin{frame}[t, fragile]{Complicated Needs} + How would you design an interface that can... + \begin{itemize} + \item \dots delete files larger than 100MB? + \item \dots show the last 2 lines of a file? + \item \dots sort files by length? + \item \dots search calendar entries and create reminders? + \end{itemize} + \end{frame} + + \begin{frame}[t, fragile]{Complicated Needs} + What about this? + + \begin{center} + \includegraphics[height=0.7\textheight]{./Figures/horrorGui.png} + \end{center} + \footnotetext{\url{https://www.jensroesner.com/wgetgui/}} + \end{frame} + + \begin{frame}[t, fragile]{Complicated Needs} + Many possible answers: + \begin{itemize} + \item Big GUI that does everything + \item A simple tool that users can extend themselves + \item Domain specific language that users write queries with + \item Many simple and combinable tools + \end{itemize} + Unix chooses the last two approaches + \end{frame} + + \begin{frame}[t, fragile] + \frametitle{Piping and Redirection} + \begin{itemize} + \item Unix has small and orthogonal tools + \item Piping and redirection are how to combine them + \begin{itemize} + \ides{Pipe:} Sends output from one command to another command + \ides{Redirection:} Read/Write to files (/streams) + \end{itemize} + \end{itemize} + \end{frame} + + \begin{frame}[t, fragile]{Piping} + \begin{minipage}{0.38\textwidth} + \begin{itemize} + \item Sends output from one command to another command + \item Uses the pipe symbol: \texttt{|} + \item Useful for sequential composition + \item Only works in "one direction" + \item Internally connects output of one process to input of other process + \end{itemize} + \end{minipage} + \begin{minipage}{0.60\textwidth} + \SU{bob} + \begin{shellcomp}[fontsize=\small] +cat numbers `\StartConsole` +zero +one +two +three +four `\SU{}` +cat numbers | tail -n 2 `\StartConsole` +three +four `\SU{}` +cat numbers | grep "t" `\StartConsole` +two +three + \end{shellcomp} + + \end{minipage} + \end{frame} + + \begin{frame}[t, fragile]{Piping Example} + List unique owners of files in current directory: + \vspace{0.5cm} + + \begin{minipage}{0.4\textwidth} + \begin{itemize} + \item List files in directory + \item Omit first two lines + \item Truncate whitespace + \item Cut (delete) all columns except the third + \item Sort alphabetically + \item Only show unique entries + \end{itemize} + \end{minipage} + \begin{minipage}{0.55\textwidth} + \SU{bob,pwd=/} + \begin{shellcomp}[fontsize=\small] +ls -l |\ `\StartConsole` + tail -n +2 |\ + sed 's/\s\s*/ /g' |\ + cut -d ' ' -f 3 |\ + sort | uniq + +bob +root + \end{shellcomp} + \end{minipage} + \end{frame} + + \begin{frame}[t, fragile]{Redirection} + \begin{columns}[T] + \column{0.45\textwidth} + \begin{itemize} + \item Read/write to files (/streams) + \item{\makebox[1.7cm]{\:\texttt{> file\hfill}}} Write output to file + \item{\makebox[1.7cm]{\:\texttt{>\hspace{0cm}> file\hfill}}} Append output to file + \item{\makebox[1.7cm]{\:\texttt{< file\hfill}}} Read input from file + \end{itemize} + \column{0.6\textwidth} + \SU{alice} + \begin{shellcomp}[fontsize=\small] +echo "BlaBla" > hello.txt +cat hello.txt `\StartConsole` +BlaBla `\SU{}` +echo "Hello " > hello.txt +cat hello.txt `\StartConsole` +Hello `\SU{}` +echo "World!" >> hello.txt +grep "Hey" < hello.txt `\StartConsole` +Hello World! + \end{shellcomp} + \end{columns} + \end{frame} + + \begin{frame}[t, fragile]{Redirection} + Redirection is useful! + \begin{itemize} + \item Store final or intermediate results + \SU{alice} + \begin{shell} +ls -l | tail -n +2 | sed 's/\s\s*/ /g' > result +cut -d ' ' -f 3 < result | sort | uniq + \end{shell} + \item Append output to files + \SU{bob} + \begin{shell} +./my_script >> log.txt +echo "Logging done!" >> log.txt + \end{shell} + \end{itemize} + \end{frame} + + \section{Processes} + + \begin{frame}[t,fragile]{Process} + \begin{itemize} + \item Every process has a unique ID (PID) + \item View processes with \texttt{ps aux} + \begin{itemize} + \item Filter using \texttt{grep} + \end{itemize} + \end{itemize} + \SU{bob} + \begin{shell} +ps aux | grep firefox `\StartConsole` +bob 2681 0.1 2.6 3702244 428076 ? Sl 07:47 0:48 /usr/lib/firefox/firefox + \end{shell} + \end{frame} + + \begin{frame}[t,fragile]{Process Management} + \begin{itemize} + \item Kill process by pid: \texttt{kill pid} + \item Kill process by name: \texttt{killall name} + \item Kill process with best matching name: \texttt{pkill name} + \item Use \texttt{-SIGKILL} flag if normal kill does not work + \begin{itemize} + \item \texttt{man 7 signal} for more information + \end{itemize} + \end{itemize} + \SU{bob} + \begin{shell}[fontsize=\small] +ps aux | grep firefox `\StartConsole` +bob 2681 0.1 2.6 3702244 428076 ? Sl 07:47 0:48 /usr/lib/firefox/firefox `\SU{}` +kill 2681 +pkill firef +killall -SIGKILL firefox + \end{shell} + \end{frame} + + % \begin{frame}[t,fragile]{HTOP} + % \begin{itemize} + % \item \todo{htop} + % \end{itemize} + % \end{frame} + + \section{PATH} + \begin{frame}[t,fragile]{The PATH Variable} + \begin{columns}[T] + \column{0.5\textwidth} + \begin{itemize} + \item Commands are programs + \begin{itemize} + \item Flag, options, etc. are arguments to this programs + \end{itemize} + \item How does the shell know where programs are? + \item The shell searches the \texttt{PATH} variable + \item \texttt{ls} \textrightarrow \: \texttt{/usr/bin/ls} + \end{itemize} + \column{0.5\textwidth} + \begin{shell} +echo $PATH `\StartConsole +/usr/sbin:/usr/bin + \end{shell}%\todo{\$ breaks highlighting} + \end{columns} + \end{frame} + + \begin{frame}[t,fragile]{Adding Your Own Paths} + \begin{itemize} + \item Let's say you want to add your script directory + \item Temporarily: \texttt{export PATH=\$PATH:\textasciitilde/scripts} + \item Permanently: Add the above to \texttt{\textasciitilde/.bashrc} + \end{itemize} + \end{frame} + + \begin{frame}[t, fragile]{Writing Shell Scripts} + \begin{columns}[T] + \column{0.4\textwidth} + \begin{itemize} + \item Scripts are just a sequence of commands + \item Very easy automation! + \end{itemize} + \column{0.6\textwidth} + \vspace{-1cm} + \SU{bob} + \begin{shellcomp}[fontsize=\small] +cat music.sh `\StartConsole` +#!/usr/bin/bash +filename="$2.%(ext)s" +echo "$1" +youtube-dl -x "$1" -o "$filename" + \end{shellcomp} + \end{columns} + \SU{alice,pwd=~/scripts} + \begin{shellcomp}[fontsize=\small] +chmod +x music.sh +./music.sh "https://www.youtube.com/watch?v=dQw4w9WgXcQ" music +music.sh "https://www.youtube.com/watch?v=dQw4w9WgXcQ" music + \end{shellcomp} + \end{frame} + + \section{Superuser} + \begin{frame}[t,fragile]{Me vs Superuser} + \begin{columns}[T] + \column{0.5\textwidth} + Me: + \begin{itemize} + \item Home is \texttt{/home/bob} + \item Have full access to that folder + \item No access to others homes + \item Can change own settings + \item May not change system-wide settings + \item Start/Stop user services + \end{itemize} + \column{0.5\textwidth} + Superuser: + \begin{itemize} + \item Home is \texttt{/root} + \item Have full access all data + \item Can change everything + \item No access to others homes + \item May not change system-wide settings + \item Can install/remove software + \item Start/Stop all services + \end{itemize} + \end{columns} + \end{frame} + + \begin{frame}[t,fragile]{Sudo} + \begin{itemize} + \item Run command as \textbf{su}per user \textbf{do}es + \ides{Syntax:} \texttt{sudo command} + \item If you forget sudo, run: \texttt{sudo !!} + \item Change user to sudo: \texttt{sudo su} + \end{itemize} + \end{frame} + + \section{Managing software} + \begin{frame}[t,fragile]{How you get software on Linux} + \begin{itemize} + \item Don't download installers from the internet! + \item Software is managed by the distribution and available through a central repository. + \item Software is \emph{packaged} + \item Similarly to Microsoft's or Apple's app stores + \end{itemize} + \end{frame} + + \begin{frame}[t,fragile]{Installing Packages} + \begin{columns}[T] + \column{0.5\textwidth} + \begin{itemize} + \item Depends on distribution! + \item Package Manager is most important feature of a Linux distribution + \end{itemize} + \column{0.5\textwidth} + \vspace{-0.5cm} + Debian, Ubuntu, Mint: + \StartConsole + \begin{shellcomp} +sudo apt install firefox + \end{shellcomp} + Arch, Manjaro: + \StartConsole + \begin{shellcomp} +sudo pacman -S firefox + \end{shellcomp} + RedHat, Fedora: + \StartConsole + \begin{shellcomp} +sudo dnf install firefox + \end{shellcomp} + \end{columns} + \end{frame} + + \begin{frame}[t,fragile]{Searching for Packages} + \begin{columns}[T] + \column{0.5\textwidth} + \vspace{-0.3cm} + Debian, Ubuntu, Mint: + \begin{shellcomp} +apt search firefox + \end{shellcomp} + Arch, Manjaro: + \begin{shellcomp} +pacman -Ss firefox + \end{shellcomp} + RedHat, Fedora: + \begin{shellcomp} +dnf search firefox + \end{shellcomp} + \column{0.5\textwidth} + \begin{itemize} + \item The basic package search is usually quite limited + \item Consult the internet for finding the right programs! + \item Arch Wiki\footnote{\url{https://wiki.archlinux.org}} has lists of many programs + \end{itemize} + \end{columns} + \end{frame} + + \begin{frame}[t,fragile]{Updating Packages} + \begin{columns}[T] + \column{0.5\textwidth} + \begin{itemize} + \item All packages can be upgraded at once + \item Do this every other week! + \end{itemize} + \column{0.5\textwidth} + \vspace{-1cm} + Debian, Ubuntu, Mint: + \begin{shellcomp} +sudo apt update +sudo apt upgrade + \end{shellcomp} + Arch, Manjaro: + \begin{shellcomp} +sudo pacman -Syu + \end{shellcomp} + RedHat, Fedora: + \begin{shellcomp} +sudo dnf update + \end{shellcomp} + \end{columns} + \end{frame} + + \begin{frame}[t,fragile]{Building From Source} + \begin{itemize} + \item Sometimes software is unavailable in the repositories + \item Can download sources and compile them manually + \item Careful! No automatic updates, malware, package manager + conflicts, \ldots + \end{itemize} + \end{frame} + + \section{SSH} + \begin{frame}[t,fragile]{SSH} + \begin{itemize} + \item \emph{Secure shell} + \item SSH allows to log in to another computer over the network + \item Server administration, running jobs on supercomputers, + log in to your computer at home + \end{itemize} + \end{frame} + + \begin{frame}[t,fragile]{Using SSH} + \begin{itemize} + \item \texttt{ssh USERNAME@HOSTNAME} + \item Will ask for user password + \begin{itemize} + \item Cumbersome + \item Bad for security + \end{itemize} + \end{itemize} + \SU{bob} + \begin{shell} +ssh bob@remotehost`\StartConsole` +bob@remotehost's`\ `password: +Last login: Wed Apr 6 08:54:53 2022 from localhost +You have mail. +`\SU{bobremote}` +`\StartConsole` + \end{shell} + \end{frame} + + \begin{frame}[t,fragile]{Login Without Password} + \begin{itemize} + \item Generate an SSH key with \texttt{ssh-keygen} + \item Copy key \texttt{\textasciitilde/.ssh/id\_rsa.pub} to \texttt{\textasciitilde/.ssh/authorized\_keys} on the server + \item Use \texttt{ssh-copy-id -i \textasciitilde/.ssh/id\_rsa USERNAME@REMOTE} + \end{itemize} + \end{frame} + + \section{Version control} + \begin{frame}[t,fragile]{Git} + \begin{itemize} + \item{\texttt{thesis.pdf}} + \item{\texttt{thesis\_old.pdf}} + \item{\texttt{thesis\_copy.pdf}} + \item{\texttt{thesis\_finalversion.pdf}} + \item{\texttt{thesis\_finalversion2.pdf}} + \end{itemize} + \end{frame} + + \begin{frame}[fragile]{Git} + \emph{Git is a version-control system for tracking changes in computer files + and coordinating work on those files among multiple people.} \\ + \hspace{10cm}--- Wikipedia + \end{frame} + + \begin{frame}[t,fragile]{Git} + \begin{itemize} + \item Track changes to your code + \item Comment your changes + \item Easily revert back to older versions + \item Avoid/manage conflicts when working in teams + \item Manage release versions and development versions + \item Work on different branches at the same time + \end{itemize} + \end{frame} + + \begin{frame}[t,fragile]{Git Example} + \begin{columns}[T] + \column{0.5\textwidth} + \begin{itemize} + \item{Initialize a git repository} + \item{Add files you want to include in a commit} + \item{Create a commit for your selected changes} + \item{Push changes to server} + \end{itemize} + \column{0.5\textwidth} + \StartConsole + \begin{shellcomp} +git init + \end{shellcomp} + \begin{shellcomp} +git add changed_file.txt + \end{shellcomp} + \begin{shellcomp} +git commit + \end{shellcomp} + \begin{shellcomp} +git push + \end{shellcomp} + \end{columns} + \end{frame} + + \begin{frame}[t,fragile]{Sharing a Repository} + \begin{itemize} + \item You can use services like \emph{Github} or \emph{Gitlab} to collaborate with others on your project + \item Or host your repositories yourself! + \begin{itemize} + \item You can pull from/push to any server you can access via SSH + \end{itemize} + \end{itemize} + \end{frame} + + \section{Backup} + \begin{frame}[t,fragile]{Backup} + \begin{itemize} + \item Backup only home or full root + \begin{itemize} + \item Convenience vs storage + \end{itemize} + \item \texttt{rsync} is a fancy file-copy tool + \begin{itemize} + \item Open format + \item Incremental backup + \item Runs over SSH + \end{itemize} + \item Use some backup script based on \texttt{rsync} + \begin{itemize} + \item Or roll your own + \item E.g. \texttt{rsync-time-backup} + \end{itemize} + \item \texttt{Borg} is also famous + \end{itemize} + \SU{alice} + \begin{shellcomp} +rsync_tmbackup.sh ~/ alice@homeserver:~/Backup/ ~/rsync_exclude_list + \end{shellcomp} + \end{frame} + +% \section{Ranger} +% +% \begin{frame}[t, fragile] +% \frametitle{Ranger} +% \begin{columns}[T] +% \column{0.5\textwidth} +% \begin{itemize} +% \item File manager on the console +% \begin{itemize} +% \item Usable over SSH +% \end{itemize} +% \item Can move files, change permissions, bulk rename... +% \item Bookmarks +% \item Keyshortcuts for frequent locations +% \item Plugins +% \item Preview functionality +% \end{itemize} +% \column{0.5\textwidth} +% \includegraphics[width=1.0\columnwidth]{img/ranger1.png} +% \end{columns} +% \end{frame} +% +% \begin{frame}[t, fragile] +% \frametitle{Ranger image preview} +% \begin{center} +% \includegraphics[width=0.9\textwidth]{img/ranger_img_preview.png} +% \end{center} +% \end{frame} +% + + \section{Epilogue} + \begin{frame}[t,fragile]{Some Commands} + \begin{itemize} + \item \verb+zip -r out.zip file1 file2+ + \item \verb+unzip out.zip -d dir+ + \item \verb+tar -czf out.tar.gz file1 file2+ + \item \verb+tar -zxvf lab.tar.gz -C dir+ + \item \verb+grep -Rni "linux"+GpgpftArcit45 + \item \verb+du -d 1 -h+ + \item \verb_for f in *.md; do echo pandoc -o $(basename $f .md).pdf $f; done_ + \item \verb+find -type l -iname "*.cr3" | wc -l+ + \end{itemize} + \end{frame} + % \begin{frame}[t,fragile] + % \vspace{2cm} + % % Feedback is very welcome: \url{https://feedback.thealternative.ch} + % \end{frame} + % for f in *; do echo "unzip -d ${f/\.zip/} $f" ; done + +\end{document}