Materials Notes
CSE 391
Week 1
Introduction to Linux
- Linux - another operating system, another way to interact with the computer.
- CS department has servers which can be remotely accessed with
ssh
. - Virtual machine - put an OS within your OS.
Flags, Arguments, and Editors
ls -l
: contains a flag or an optionl
which provides arguments. Long-listing format.- Command: the command name, flags, then arguments. Flags provide optional specifications.
man
: pull up the manual.- Forward slash: can search for a word, e.g.
/long
- pressn
to cycle through the matches.
- Forward slash: can search for a word, e.g.
-a
command: do not ignore hidden files.*
.- Every directory has
.
and..
: references to the current directory and the previous parent directory. cd
brings you back to your home directory.
Week 2
More Unix Commands
cat
- print out contents to the file. It’s limited to a buffer size, thoughless filename
opens up a scrollable interface with very similar keybindings tovim
.more filename
- similar toless
head
- see the beginning of the file. Specify-n
flag.tail
- see the end.wc
- word countsort
- outputs a sorted version of the file argumentgrep re_exp filea fileb
- performs regular expression matching on files- You can also specify a directory in lieu of files:
grep re_exp project/*
.
- You can also specify a directory in lieu of files:
- How to run Java in the command line? Compile with
javac file.java
, then run withjava file
.java file.java
will compile and run simultaneously.
Input Output Redirection
- Three streams (abstracted locations):
stdin
,stdout
,stderr
. - A program accepts
stdin
and outputs tostdout
andstderr
. - You can redirect
stdout
into other areas. - You can direct
stdin
into programs. - Ise
2>
to redirect bothstdout
andstderr
. - Distinguish between
stdin
and parameters. - If no file is provided, programs will read from
stdin
. - To append to a file, use the double wakka:
>>
.
Pipes
- Pipes allow you to chain together commands
|
. It takesstdout
from the left argument and puts it asstdin
into the right argument. - Unix philosophy - do one thing and do it well.
- You can impose repeated conditions by chaining.
uniq
- prints the number of repeated linessort
- sorts a file by lines- Log file - stores progress on a file.
Week 3
I/O Redirection, xargs
&&
can be used to chain together commands in which commands are executed only if the previous command does not raise an error.||
will only execute if the previous command fails.;
- chain together commands regardless of failure or correctness.- Some commands don’t accept standard input - you can use
xargs
to take standard input and convert it into arguments. For instance,xargs javac < ls *.java
. find
- recursively searches all files in the current folder or with the current folder as parent.$(...)
- command substitution. Command line evaluates the command first.2>&1
- have standard error go to wherever standard output is.tee
- prints to the console and saves to the file as well.- e.g.
java printer.java | tee output.txt
- e.g.
echo
- prints to the terminal.cut
- helps with indexing, e.g.-c 4
gets the fourth character in the input.-c4-6
,-c1,3,5
- Cut is usually used to break input strings by delimiters.
-d, -f2
- Pipe operators let you get pretty far pretty quick.
Week 4
Introduction to Git
- Git - version control software.
- How to work with partners?
- Repository - a location which stores a copy of all files. What should be in a file? Source code files, build files, images, general resource files - but not executables and object files.
- Everyone working on the project has a complete version of the repository.
- Remote repository - a central repository hosted on services like GitHub
README.md
is the standard documentation..git
subdirectory - the ‘actual’ repository, manages the state of the repository for us.git status
- updates us about the state of changes.git stage
- get ready to take a snapshot of the repository.git commit
- create a snapshot of the repository.git push
- push to remote repository
Four Phases of Git
- Phases
- Working directory
- Staging Area / Index
- Local repository
- Remote repository
git stage
orgit add
- move into the staging area.git commit
- move from the staging area to the local repository.git push
- move from the local repository to the remote repository.git status
- tells us about the state of the working directory.git log
- tells us about the interaction between the staging area and the local repository.- Each commit has a unique identifier.
- What is the point of a staging area? Why use an intermediate step? Git makes us be deliberate about the changes we want to make, we have to manage the history.
- Only changes which are staged get stored into the local repository during the commit. This allows you to group many different changes and push them all at once.
Git Practice
- Git doesn’t work on files, it works on diffs - changes from one file to another.
- Commits are groups of diffs
- Just typing
git commit
will open upvim
to provide a message - can provide a message and a longer body, too. git pull
- automatically merges repositories such that the repository can be pushed.- Git histories often aren’t linear: need to be repeatedly separated and merged. These are recorded all by git.
git log --decorate --all --graph
Week 5
Git Branching
- Continuous integration - any time you push changes, you need to do tests before you commit to the master.
- Master - single source of truth, the history of the project. SUpposed to be stable.
- To make modifications, we need to work on a separate branch.
- Commit history
- HEAD - ‘current pointer’, the pointer which belongs to us, the user. We can move HEAD around from branch to branch.
- Head - pointer / reference to the current git branch.
git branch feature
- creates a new branch calledfeature
. But it does not automatically switch branches.- Type
git checkout feature
to switch to a branch. git log
can look differently depending on whereMAIN
is pointing.- How to reconcile different histories?
Git Merge
git merge feature
- mergesfeature
into the current branch.- Git can’t fix things for you - you need to go into the files and fix the conflicts.
- After we finish editing, we need to stage / add the files with the resolved changes.
- We need to make a commit with the conflicts resolved.
git log --graph --oneline
will show you the history in a visual format.
Git Branching: Working with Remote
- How to use branches with remote repositories?
- Origin is a way of referring to the remote repository.
- If you try to
git push
when the remote has been changed since when you last updated it, you’re going to get an error. git fetch
- does not change your local repository, but the origin master and origin head will be missing. Downloads copy of remote repository to a new branch.- You can try to
git merge origin/...
and resolve the issues. - Git is built on simple foundations.
git pull
isgit fetch + git merge
.
Git Merge Requests
- In practice, we often don’t merge branches locally and push to remote
- GitHub / GitLab can help us handle these.
- Steps:
- Create a local branch and make commits
- Push commits to remote
- Open a pull/merge request on GitLab
- Collaborate and fix conflicts
- Merge into master or another branch
- We need to be very deliberate about what we put on master
git checkpoint -b feature
- Merge requests - can be handled on Gitlab.
git branch -d feature
removesfeature
branch from your local repository.
Week 6
Introduction to Regular Expressions
- Regex - description of a pattern of text
- Many applications of regular expressions
- Almost all programming languages use regular expressions
- grep is case sensitive by default
.
will match any character.- Use
-E
for extended regex. $
- end of line^
- start of line- Spaces are interpreted literally.
- Why doesn’t
....
match 4 letter characters? \<
- beginning of word;\>
- end of word
Or and Repeated Characters
- Or can be done with
|
. - We can group together an expression with parentheses:
(e|a)t
is equivalent toet|at
. *
- ‘zero or more of’?
- ‘zero or one of’+
- ‘one or more of’
Character Sets and Backreferences
- Enumerating a lot of characters indiivudally is a pain. We can use a character set with square brackets:
[abcde]
is equivalent to(a|b|c|d|e)
. [A-Z]
,[a-z]
,[0-9]
, or combine:[a-zA-Z0-9]
.- Curly brackets:
{n}
, matches exactly 3 instances from the set.{a,b}
, anything froma
tob
(inclusive). ^
negates the entire character set - anything except for what is in the set. Interprets signs literally.(..)\1
will matchabab
,acac
, all repeated.\1
is whatever was the frist saved one.
Week 7
Intro to sed
grep -E "CSE[^3][0-9]{2}" classes.txt
sed -r 's/REGEX/TEXT/'
- normalsed
usage- By default
sed
does not change the contents of the file - You can use
sed -ri.bkp
to make changes in-place. - By default,
sed
replaces the first occurrence on each line. To change all matches, we need's/REGEX/TEXT/g'
. - You can escape special characters for the search criteria, but not for the replace string.
- You can replace with backwards references.
Week 8
Introduction to users, groups, adn permissions
whoami
- tells you your usernameusers
- see other users on the serverpinky
- see user IP addresses, names, etc.groups
ps
- see processes- ps -u username`
top
/htop
- updated processes on the system by intensitykill pid
- kill process ID, only accessible to admins. Give people only as much power as they need.- Virtual machine
sudo
-super user do
sudo
- gives you higher permissions- Short-hand:
!!
refers to the last command that you ran, e.g.sudo !!
bashrc
, bash_profile
, and $PATH
- Customizability of linux systems - customize your shell, giving you a starting point for your UX on Linux.
.bash_profile
and.bashrc
.bash_profile
, goes through line by line and runs various commands. Every time you log into your system, bash runs the contents of this file.- You can set up an alias on
.bashrc
-alias name="cmd"
. source .bashrc
- reload/rerun the file.$PATH
variable, string that bash does to look for commands in.- Use
PATH=$PATH:/...
to redirect the path variable to the current path.- To make this persistent, add it to your
.bashrc
.
- To make this persistent, add it to your
File and Directory Permissions
- File permissions:
- Is it a directory -
d
- Owner:
rwx
- Group:
rwx
- Others:
rwx
- Is it a directory -
- If you own a file, you can always change the permissions.
chmod u+x
: give execute permissions to the owner (u
).- Octal codes: +4, +2, +1.
444
- everyone gets write. umask
: sets default permissions for newly created files.