# Topics

• File system structure
• Command Line Interface (CLI)
• Command interpreter (Shell)
• The Bourne Again Shell (bash)
• Useful unix commands
• Text files & text editors
• Standard streams
• I/O redirection

# File System Structure I

• Example file system
                         /
|
-----------------------------------------
|         |         |         |         |
bin       usr       home      etc       tmp
|                   |
-----     -------------------------------------
|   |     |     |     |     |     |     |     |
ls cat  john  alice  bill  carl  sam  jane  lisa  
• Virtually all modern operating systems have a hierarchical file system structure. The structure looks like a tree, drawn upside down with its root at the top.
• I assume you’re using a UNIX-like system: linux, OS X, WSL, etc.
• A directory (or folder) contains other files and/or directories. In the picture, every name is a directory except ls and cat, which are files.
• The root directory is the starting point of the file system hierarchy. It is denoted by the / character.

# File System Structure II

• Example file system
                         /
|
-----------------------------------------
|         |         |         |         |
bin       usr       home      etc       tmp
|                   |
-----     -------------------------------------
|   |     |     |     |     |     |     |     |
ls cat  john  alice  bill  carl  sam  jane  lisa  
• At any point in time, the OS assumes you are located at some specific directory in the file system. This place is called your current directory, and is denoted by the . character.
• Double dots .. denotes your parent directory. It is the directory containing your current directory, unless you are at the root of the file system, in which case your parent directory is the root itself. E.g., home is the parent of alice.
• Every user has a home directory. The ~ character denotes your home directory. It is where you are put when you start a terminal session. In general ~user denotes the home directory of user user. So if sam is my home directory, then ~ refers to the sam directory, while ~alice refers to alice’s home directory.

# Pathnames

• Example file system
                         /
|
-----------------------------------------
|         |         |         |         |
bin       usr       home      etc       tmp
|                   |
-----     -------------------------------------
|   |     |     |     |     |     |     |     |
ls cat  john  alice  bill  carl  sam  jane  lisa  
• You can refer to a file or directory by writing its access path that either starts from the root or from your currect directory. This access path is called the pathname of the target.
• The forward slash character / separates the nodes along the pathname.
• A path that starts from the root is called an absolute pathname, e.g., /home/bill is an absolute pathname to bill
• A path that starts from the current directory is called a relative pathname, e.g. ../../bin/ls is a relative pathname from my current directory sam to ls
• Every absolute pathname has / as its first character; no relative pathname has / as its first character.

# Command Line Interface (CLI)

• Through CLI the user interacts with the computer by typing in commands on the keyboard, and the computer responds by printing out text messages on the user’s terminal.
• Compared to the Graphical User Interface (GUI), a CLI is
• harder to learn
• harder to master
• not as pretty
• faster
• more powerful, in fact, some CLI functionality may not have a GUI equivalent

# Shells

• A shell is a command interpreter. It interprets the commands you type in, executes the commands if possible, prints out the results of execution, or prints out error messages if it can’t execute your commands, or if it encounters any problem.
• Some common shells that are in general use:
• sh — the Bourne shell
• csh — the C shell
• tcsh — the Tenex C Shell
• ksh — the Korn Shell
• bash — the Bourne-again shell
• zsh — the Z shell

# bash I

• I assume you’ll be using the Bourne-Again Shell (bash) since it is the default shell on the macs, linux machines, and WSL.
• The format of a simple bash command is

command [flags] [arguments]

where white spaces (blanks or tabs) separate the components. e.g.

$ls$ cp *.txt novel
• However, bash is a full-blown scripting language as well as an interpreter. It has regular expressions, conditionals, loops, and functions, e.g.,

$for f in ch[1-3].ps do ps2pdf$f
done

# bash II

• All commands entered interactively through the keyboard must end in a newline character by the user pressing the enter key.
• A valid command is either internal or external. The shell understands an internal command and is able to execute it immediately. Examples of internal commands are

$pwd$ alias l=“ls -l”

• If the shell does not understand a command, it assumes that it is an external command, and will search all directories specified by the PATH variable for a file having that command as name. If the file is found and is executable, the shell loads it into memory and executes it. Examples of external commands are

$ls$ head file

# Useful unix commands I

• ls — list files and directories
• cd — change directory
• pwd — print working directory
• mkdir — make directoryies
• rmdir — remove directories
• rm — remove files and/or directories
• mv — move (or rename) files and/or directories
• cp — copy files and/or directories

# Useful unix commands II

• cmp — check if two files are the same
• diff — list all differences between two files
• echo — echo command line arguments
• cat — concatenate files
• less — browse text files
• man — read manual page for external commands, system calls, and C API
• info — similar to man
• help — ask for help with internal commands

# Text files & text editors

• A text file is meant to be read by humans. It contains characters that can be safely printed to the screen and printers. It doesn’t contain formatting characters or control characters or other binary data.
• A text editor is an app that allows the user to conveniently read, write, modify, and save text files. It actually works on buffers in RAM until such time when the user tells it to write those buffers onto secondary storage.
• Examples of simple text editors are nano on a Unix machine, TextEdit on a Mac (in plain text mode), and Notepad++ on Windows machine.
• Some editors are meant to be used for writing program source code. They have extensive features useful to programmers. Examples are TextWrangler, Sublime Text, and atom.
• Some advanced general-purpose text editors have features that are useful for editing all kinds of text files. Examples are emacs and vim.

# Standard streams

• A process is a program in execution.
• A stream is a sequence of bytes.
• Every process are allocated 3 standard streams by the OS when it starts:
• standard input — the keyboard
• standard output — the computer screen (or terminal or console)
• standard error output — the computer screen
• The values of these standard streams can easily be changed from the command line by the user

# I/O Redirection I

• A text file is a kind of text stream so it is reasonable to redirect any of the standard I/O streams to a file.
• The command

prog < infile

runs prog with its stardard input coming from infile.
• The command

prog > outfile

runs prog with its stardard output redirected to outfile. If outfile does not exist, it will be created with content of the output from prog. If outfile exists, its old content will be overwritten by the output from prog.

# I/O Redirection II

• The command

prog >> outfile

runs prog with its stardard output redirected to outfile. If outfile does not exist, it will be created with content of the output from prog. If outfile exists, its content will have the output from prog appended to its old content.

• The command

prog1 | prog2

has the effect of running both prog1 and prog2 in parallel, but with the stardard output of prog1 being the stardard input of prog2.

# I/O Redirection III

• The command

prog 2> errfile

runs prog and redirects the standard error output to the file errfile.

• It’s possible to redirect the standard error output to the standard output. E.g.,

prog > outfile 2>&1

runs prog and redirects both the standard output and the standard error output to the file outfile. In bash, you can get the same effect by typing

prog &> outfile