Skip to main content
CSE 391
Lecture 1 - June 20
- Need 14/18 points to pass
- SSH basics:
username@server
- Unix: developed in the late 60s at Bell Labs
- Linux: developed in 1992 by Linus
- Why use the CLI? It is often faster and easier!
/bin
is where the binaries are stored
ls -lh
lists everything in h
uman-readable format
- We can use the
find
command to find files!
- Starts in your current directory, recursivly searches from there
-name
matches the end of each path
man
: manual for commands
Lecture 2 - June 27
- Shell expands all
*
wildcards
cat
: concat
enate a file to the terminal
less
: allows easy viewing of text vim-like
head
: print the first lines of a file
tail
: print the last lines of a file
-f
flag to keep running file
wc
: word count program
- by default, counts lines, words, then characters
sort
: sorts lines alphabetically
uniq
collapses adjacent unique lines
- Typically should
sort
it first
grep
: search tool for files
- First argument is search term, proceding arguments are files to search from
- Prints each line that matches the search term
- Running java on the command line:
javac ProgramName.java
combiles the program
javac ProgramName
runs the program
java ProgramName.java
compiles and runs the program
- Streams:
- Standard input:
stdin
- Standard output:
stdout
- Standard error:
stderr
- Reads or adds to the stream however much is requested
- Use the right waka
>
to redirect standard output to a file
- Double waka
>>
appends standard output to a file
- Use the left waka
<
to redirect standard input from a file
- Use two waka
2>
to redirect standard error to a file
- Pipes
|
: takes standard output of what’s on the left and uses it as standard input on the right
- Used to chain commands together
ctrl-D
to close a program
Lecture 3 - July 4
command1 && command2
runs command2
if command1
ran successfully
- Similar to short-circuting evaluations
command1 || command2
runs command2
if command1
didn’t run successfully
command1 ; command2
always runs both commands
- Some commands don’t read from standard input if none are provided
xargs
: “negotiate” between standard input and passing parameters
- Command substitution: evaluate what’s in a
$(eval)
and substitute
- Append
&1
to use the same as standard output (not as confident)
tee
saves standard output to a file while still printing it out
echo
echos what is passed
cut
: cuts up input:
-cRANGE
outputs the RANGE of characters
-dDELIMETER
cuts at the delimeter
-fRANGE
outputs the RANGE of fields
- Standard output and input is buffered, use
stdbuf
to change behavior
Lecture 4 - July 11
- A repo stores the state (and history) of the project
- Don’t store the compiled files, build somewhere else
- Using a remote repository allows us to have a unified source of truth
git status
: display the status of the repo
- Only working directory and staging area
git commit -m "message"
: commits the staged files with the message
- The four stages of git:
- Working directory: the local files on your computer
- Staging area/idex: changes I’m preparing to commit
- Local repository: project changes on my computer
- Remote repository: shared and source of truth
git add
to add files
git log
to view past commits
- Each commit gets its own hash
- Use
--decorate --all --graph
to get a visual history of the repository
origin master
is where the computer thinks the remote repository is at
HEAD
is where we are locally
git restore file
to remove local changes
- Add
--staged
flag to un-stage
- The diffs for files are what is actually tracked, that means a file can be both staged and have unstaged diffs
git diff
allows us to view differences between working directory and staging area
- Add
--staged
flag to view diff between staging area and local repository
git help command
displays the man page for git command
git commit
(no -m
) allows you to add a commit message on first line, then other info on other lines
git pull
“pulls” the remote commits to your local repository
- Often leads to a “merge commit”
git switch -c name
to create a branch and switch to it
HEAD
is most recent commit, HEAD~n
is the commit n
before HEAD
Lecture 5 - July 18
master
or main
is the “single source of truth”, the history of the project
- The code on
master
should be stable
- Continuous integration (CI): when you push, automated tests verifty it is stable
- Each commit references its parent, somewhat like a linked list
- Branches are pointers referencing commits
master
is the default one
HEAD
is the current pointer, where the user is at
- We can move this to whichever branch we want
- Use
git log --graph --oneline
for a simplified version
- To create a new branch, we use
git branch <branch_name>
- The branch is created pointing to wherever
HEAD
is
- To switch branches, we use
git checkout <branch_name>
HEAD
now points to <branch_name>
git merge <merge_name>
merges <merge_name>
into current branch HEAD
- Attemps to auto-merge but runs into conflicts when conflicting diffs
- Changes the file to show the conflicting diffs, must be resolved
- To resolve it, we commit like normal
git status
will now give more information on error
- After merging
<branch_name>
, you can checkout <branch_name>
and git merge master
to fast-forward to the merge
- Fast-forward is like moving
HEAD
to the other branch’s reference (as it is acting like a linked list)
- We save the pointers (branches) from our remote (
origin
) repository as well
- That’s what
origin/master
are
git fetch
fetches information from the remote repository
git pull
is (roughly) git fetch
and git merge
in one command
- Merging in practice:
- Create a local branch and make some commits
- Push those commits to remote
- Open a merge request on GitHub/GitLab
- Collaborate with others, leave comments, fix conflicts
- Merge into master (or other branch)
- Much of the time you can’t push directy to
master
git checkout -b <branch_name>
creates a new branch <branch_name>
and swtiches to it
- When there is a merge conflict in a merge request, (in GitLab) you can make a reconciliation commit in browser
- Finally, you can make the merge commit in the browser
- Use
git branch -d <branch_name>
to remove a branch
- Use
git init
to initialize a git repository
HEAD
is the head of the “linked list” that represents the repository
- A tag is a fixed reference pointing to a specific tag
- A branch is like a tag, but moves as
HEAD
moves
- Use
git tag <tag_name>
to tag a commit (by default HEAD
)
- A “deatched head” is a git repository where no branch is associated with it
- Can happen when checking out something that doesn’t have a branch at it
- This can happen when checking out a tag
git rebase
is useful to apply a o
Lecture 6 - July 25
- A regular expression (regex) is a description of a pattern of text
- This is a common way to search for text
grep
: global regular expression print
- By default, case sensitive: use
-i
to ignore case
- Passing
-E
to grep
allows us to use regular expressions
.
matches any character
- Use backslash
\
to escape (some?) special characters
^
matches the beginning of the line
$
matches the end of the line
- “Words” are things that are separated by some amount of whitespace
\<
refers to the start of a word
\>
refers to the end of a word
pattern1|patttern2
searches for pattern1
or pattern2
()
groups expressions together
x*
matches any number of x
s
x+
matches one or more x
s
x?
matches zero or one x
[xyz]
is equivalant to (x|y|z)
- It also makes all characters in the brackets to literals
[a-z]
, [A-Z]
, [0-9]
, and [a-zA-Z]
all work as expected
x{5}
matches x
five times in a row
x{,5}
matches x
up to five times in a row
x{2-5}
and x{5,}
work as expected
[^0-9]
matches any character that is not [0-9]
- Backreferences allow for capturing and referring to previous patterns
(..)\1
matches any two-character pattern which is repeated
Lecture 7 - August 1
sed -r 's/REGEX/TEXT
does search and replace for text
- Append with a file name to run it on the file
- Use the
-i.EXTENSION
flag to change the file and use the EXTENSION
- By default,
sed
replaces only the first match on each line
- To replace with all matches on each line, append with
g
: sed -r 's/REGEX/TEXT/g
- To use the forward slash, we need to escape it:
\/
- We can use backreferences in the
TEXT
Lecture 8 - August 8
whoami
prints out your username
users
prints out all currently logged in users
pinky
is like users
but more informative
groups
prints all the groups that you belong to
ps
prints all the processes you’re currently running
-u uname
does that for a username
top
or htop
shows system usage with procceses
kill PID
kills the process with PID PID
/usr/bin
holds many of the executable binaries
/usr
stands for universal system resouces
sudo
: super user do
.bash_profile
is run every time you login
.bashrc
is run for every non-login shell
- To make an alias:
alias new_name="command to be ran"
alias
alone will print all aliases
$PATH
is all of the paths that your system will search through for a command
- Each path is separated by a
:
delimiter
- File permissions:
drwxrwxrwx
- If there is a character in a slot, it means that it has that permission
- First character represents if it is a directory
- Each following set of three represent if there are read, write, or execute permissions
- Those sets are owner (
u
), groups (g
), and others (o
)
- Owners of a file can always change the file permissions
- To change permsissions, use the
chomod
command
- Use
chmod T+P
where T
is the type of user and P
is the permission type
chmod T-P
takes away permissions
- Octal codes allow simple ways to type out permissions for a user type
- +4 for read, +2 for write, +1 for execute
- Directory permissions:
- Read: if the person view the directory
- Write: if the person write to the directory
- Execute: if the person enter into the directory
umask
sets default permissions for new files
Lecture 9 - August 15
- The first line of a shell script should be the “shebang”,
#!/bin/bash
- The first line of a Python script should be
#!/bin/python
- Bash scripts are simply shell commands, executed line-by-line
- Shells run sub-shells, so calling shell state doesn’t get changed
- i.e. bash scripts can’t change the calling shells with
cd
- Bash variables:
- Create them using
var_name="value"
- Reference them with
$var_name
- Variables are empty by default
- Single quotes in bash are interpreted as literals, double quotes insert variable names
- Somewhat similar to f-strings
- Dollar sign number variables (e.g.
$1
) are reserved and bound to arguments
$#
refers to the number of arguments (not including program name)
$@
refers to all the arguments
- Command line substitution:
var_name=$(expression)
- Sequence command:
$(seq start stop)
- Loop syntax:
- Start with
for var_name in variable
- Begin body with
do
- End with
done
- It can iterative over command line arguments
- Math in bash:
- Either use
let
syntax and double quotes
- Or use
$((expression))
- If statements:
if [ $a -ge $b ]; then
echo "a is greater than b"
fi
- Square brackets are interpreted as a test alias
- Add
else
betwen then
and fi
for else
- Use
elif
for else-if
- Use
||
to OR expressions together
- Use
&&
to AND expressions together
- Exit codes:
- We can use the exit code to know how a program terminated
- The exit code for the previous command is stored in
$?
- Successful exit code is
0
, non-zero exit codes are failures
exit
command sets exit code to zero by default
exit num
sets exit code to num