24sp ver.
Note: this is for the Spring 2024 iteration of CSE 391. Looking for a different quarter? Please visit https://courses.cs.washington.edu/courses/cse391/.
Info
This page will serve as a collected reference of the commands introduced in the lecture videos and slides. It is not intended to replace the man pages or watching lectures or reading over the slides, but will hopefully help you navigate the commands we have shared.
Info
Some commands have a “relevant flags” section with flags used in lecture/homeworks prior to the current homework. They serve as a place to start looking - but you should still read the man page to see what these flags do. Importantly, this does not include flags we expect you to discover in the “self-discovery” portion of a homework!
Linux Commands¶
Basic Shell Commands¶
Log in to a remote Linux server (e.g., attu
)
Print current working directory
Change working directory
List files in working directory
(relevant flags: -a
-l
-r
)
Bring up manual for a command
Log out of shell
Converts standard input to command line arguments
(relevant flags: -d
)
Allows for the output of a command to be redirected to both a file and stdout
System Commands¶
Clears all output from console
Output the system date
Output a text calendar
Print information about the current system
Directory Commands¶
List files in working directory
Print current working directory
Change working directory
Make a new directory
Remove the given directory (must be empty)
File Commands¶
Copy a file
(relevant flags: -i
)
Move a file (also used to rename files)
(relevant flags: -i
)
Remove the given file
(relevant flags: -f
-r
)
Create empty file, or change time-modified
Change permissions for filename (e.g., give owners/group read/write, others).
Example: chmod ug+rw,o-rwx file.sh
Change permissions for filename using octal codes. Digits correspond to owner(u), group(g), others(o) respectively (+4 for read, +2 for write, +1 for execute).
Example: chmod 660 file.txt
Sets the default permissions of files, calling umask similarly to chmod with octal will remove those permissions for newly created files (start with a leading 0 to indicate base 8 e.g. umask 0022)
File Examination Commands¶
Print contents of a file
Output file contents, one page
Output file contents, one page
Output number of lines of start of file
(relevant flags: -n
)
Output number of lines of end of file
(relevant flags: -n
)
Count words, characters, lines in a file
(relevant flags: -l
-w
)
Searching and Sorting Commands¶
Search given file for pattern
(relevant flags: -E
-i
-v
)
Sort input or file, line based
(relevant flags: -f
-r
)
Strip duplicate adjacent lines
Search filesystem
(relevant flags: -type
)
Remove section from each line of file
This will replace all matches of REGEX in the given file with TEXT and output it to the console.
Users¶
Print your username
Print user ID and group membership
List logged-in users (short)
List logged-in users (long)
Print information about users
Groups¶
Print group membership
Create a group
Delete a group
Modify a group
Assign
Changes all files recursively starting in the current directory to be owned by
Processes¶
List processes being run
(relevant flags: -u
)
Show process statistics
Terminate process by PID
Terminate process by name
Java Commands¶
Compile ClassName
Run ClassName
Run or compile other files in different languages!
Unix File System¶
Relative Directories¶
References the working directory
References the parent of working directory
username's home directory
Your desktop
Unix File System¶
Root directory that contains all directories
Applications/programs (i.e. binaries)
Hardware devices
Configuration files
Contains user's home directories
Running programs (processes)
Temporary files
Universal system resources
Directories have the same permissions as files but read determines viewing privileges, write determines add/delete file privileges, and execute determines whether a user can enter the directory.
Text Editors¶
Very simple editor
More advanced text-editor
More advanced text-editor
Vim Basics¶
Write (save) the current file
Write (save) the current file and exit
Quit, ignoring all changes
Go into insert mode
Go back to normal mode
Move cursor left, down, up, right
Undo last change
Delete character
Emacs Basics¶
C = control key, M = alt/meta key
Read a file into emacs
Save a file to disk
Exit emacs
Search forward
Search backwards
Scroll to next screen
Scroll to previous screen
Undo
Bash Scripting¶
Running a shell script¶
At top of file, tells the command line how to interpret your file
Gives script.sh execute permissions so it can be run with ./script.sh
Variables¶
Creates a variable (no spaces on either side of = sign) (variable names are case sensitive)
Variables are referenced using the $ operator (output":" I am taking 391)
Single quotes will not expand variables (output":" I am taking $CLASS)
The output of running a command can be saved to a variable and referenced later
Command Line Arguments¶
Name of the script
First, second, third arguments
Number of arguments
List of all arguments
Arithmetic¶
Use let expression to do arithmetic (bash supports multiplication, division, addition, and subtraction)
Other way to do arithmetic, must have spaces in between operations!
For Loops¶
For loop syntax, begins with do and ends with done. Semicolon is the same thing as a new line.
Iterates over all of the files in the current directory by iterating over output of ls
If Statements¶
If statement basic syntax. Boolean expression must be surrounded by [ ] with spaces on either side.
greater than, less than, greater than or equal to, less than or equal to, equals, not equals
And operator. Can also be written (if [ expr1 ] && [ expr2 ]; then)
Or operator. Can also be written (if [ expr1 ] || [ expr2 ]; then)
Negates expr1
Test if string is equal (=) or not equal (!=)
Test if a string is empty (-z) or nonempty (-n)
Test if a file (-f) or directory (-d) exists
Test if a file exists and is readable (-r), writeable (-w) or executable (-x)
Exit Codes¶
Variable containing exit status of previously run program. It will be 0 if the program executed correctly, or not 0 if it failed.
Other¶
Input/Output Redirection and Command Substitution¶
Execute command and read its standard input from the contents of filename instead of from the console.
Execute command and redirect its standard output to the given filename
Execute command and redirect its standard error to the given filename
Execute command and redirect its standard error to standard output
Execute command, redirect standard error to standard output, and redirect standard output to filename
Execute command1 and send its standard output as standard input to command2.
Execute command1, then execute command2
Execute command1, and if it succeeds, then execute command2
Execute command, then place the output string literally into the given context i.e. javac $(find -name "*.java")
Creates alias
Everytime you log in to a shell (i.e. by ssh-ing into attu or logging into your computer), the commands in ~/.bash_profile are run
Everytime you launch a non-login shell (i.e. by launching a new terminal) the commands in ~/.bashrc are run.
Stores the path to all directories which the system will look for programs to run. If a path is in $PATH and contains a file named
Prepend a directory to your $PATH (directory will be looked through first for a filename matching
Append a directory to your $PATH (directory will be looked through last for a filename matching
Git Commands¶
Copy a git repository
Adds file contents to staging area
Same as git add
Takes a snapshot of staging area and creates a commit
View status of files in working directory and staging area
Show difference between staging area and working directory
Show commit history
Fetch from remote repository and try to merge
Push local repository to remote repository
Create a new branch with the name branch name
Switch to the branch branch name
Merges the feature branch into the current branch checked out
View the commit history, and where HEAD and master are located. Flags unnecessary
Regex Syntax¶
Matches any character
Matches start of line
Matches end of line
Matches start of word
Matches end of word
Escape the following character
(Flag to grep) Match case insensitively
Logical or operator (match this character set or that character set)
Matches zero or more of the preceding character set
Matches one or more of the preceding character set
Matches zero or one of the preceding character set
Group characters together
Character set
Negate character set
Matches all lowercase letters
Matches all uppercase letters
Matches all digits
Back references the same characters captured in an earlier grouping (specified by the number)
Specify how many occurrences of a match we want. Can be an exact number (e.g. {N}) or a range of numbers (e.g. {MIN,MAX}).