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¶
Command | Lecture | Description |
---|---|---|
ssh | 01 | Log in to a remote Linux server (e.g., attu ) |
pwd | 01 | Print current working directory |
cd | 01 | Change working directory |
ls | 01 | List files in working directory (relevant flags: -a -l -r ) |
man | 01 | Bring up manual for a command |
exit | 01 | Log out of shell |
xargs | 03 | Converts standard input to command line arguments (relevant flags: -d ) |
tee | 03 | Allows for the output of a command to be redirected to both a file and stdout |
System Commands¶
Command | Lecture | Description |
---|---|---|
clear | 01 | Clears all output from console |
date | 01 | Output the system date |
cal | 01 | Output a text calendar |
uname | 01 | Print information about the current system |
Directory Commands¶
Command | Lecture | Description |
---|---|---|
ls | 01 | List files in working directory |
pwd | 01 | Print current working directory |
cd | 01 | Change working directory |
mkdir | 01 | Make a new directory |
rmdir | 01 | Remove the given directory (must be empty) |
File Commands¶
Command | Lecture | Description |
---|---|---|
cp | 01 | Copy a file (relevant flags: -i ) |
mv | 01 | Move a file (also used to rename files) (relevant flags: -i ) |
rm | 01 | Remove the given file (relevant flags: -f -r ) |
touch | 01 | Create empty file, or change time-modified |
chmod who(+-)what filename | 08 | Change permissions for filename (e.g., give owners/group read/write, others). Example: chmod ug+rw,o-rwx file.sh |
chmod NNN filename | 08 | 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 |
umask [options] [mask] | 08 | 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¶
Command | Lecture | Description |
---|---|---|
cat | 02 | Print contents of a file |
less | 02 | Output file contents, one page |
more | 02 | Output file contents, one page |
head | 02 | Output number of lines of start of file (relevant flags: -n ) |
tail | 02 | Output number of lines of end of file (relevant flags: -n ) |
wc | 02 | Count words, characters, lines in a file (relevant flags: -l -w ) |
Searching and Sorting Commands¶
Command | Lecture | Description |
---|---|---|
grep | 02 | Search given file for pattern (relevant flags: -E -i -o -v ) |
sort | 02 | Sort input or file, line based (relevant flags: -f -r ) |
uniq | 02 | Strip duplicate adjacent lines |
find | 02 | Search filesystem (relevant flags: -type ) |
cut | 02 | Remove section from each line of file |
sed -r 's/REGEX/TEXT/' | 07 | This will replace all matches of REGEX in the given file with TEXT and output it to the console. (relevant flags: -i ) |
Users¶
Command | Lecture | Description |
---|---|---|
whoami | 08 | Print your username |
id | 08 | Print user ID and group membership |
users | 08 | List logged-in users (short) |
who | 08 | List logged-in users (long) |
pinky | 08 | Print information about users |
Groups¶
Command | Lecture | Description |
---|---|---|
groups | 08 | Print group membership |
groupadd | 08 | Create a group |
groupdel | 08 | Delete a group |
groupmod | 08 | Modify a group |
chgrp | 08 | Assign |
chgrp | 08 | Changes all files recursively starting in the current directory to be owned by |
Processes¶
Command | Lecture | Description |
---|---|---|
ps | 08 | List processes being run (relevant flags: -u ) |
top | 08 | Show process statistics |
kill | 08 | Terminate process by PID |
killall | 08 | Terminate process by name |
Java Commands¶
Command | Lecture | Description |
---|---|---|
javac ClassName.java | 02 | Compile ClassName |
java ClassName | 02 | Run ClassName |
Unix File System¶
Relative Directories¶
Command | Lecture | Description |
---|---|---|
. | 01 | References the working directory |
.. | 01 | References the parent of working directory |
~username | 01 | username's home directory |
~/Desktop | 01 | Your desktop |
Unix File System¶
Command | Lecture | Description |
---|---|---|
/ | 01 | Root directory that contains all directories |
/bin | 01 | Applications/programs (i.e. binaries) |
/dev | 01 | Hardware devices |
/etc | 01 | Configuration files |
/home | 01 | Contains user's home directories |
/proc | 01 | Running programs (processes) |
/tmp, /var | 01 | Temporary files |
/usr | 01 | Universal system resources |
Directory permissions | 08 | 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¶
Command | Lecture | Description |
---|---|---|
nano | 01 | Very simple editor |
vim | 01 | More advanced text-editor |
emacs | 01 | More advanced text-editor |
Vim Basics¶
Command | Lecture | Description |
---|---|---|
:w | 01 | Write (save) the current file |
:wq | 01 | Write (save) the current file and exit |
:q! | 01 | Quit, ignoring all changes |
i | 01 | Go into insert mode |
Esc | 01 | Go back to normal mode |
hjkl | 01 | Move cursor left, down, up, right |
u | 01 | Undo last change |
x | 01 | Delete character |
Emacs Basics¶
C = control key, M = alt/meta key
Command | Lecture | Description |
---|---|---|
C-x C-f | 01 | Read a file into emacs |
C-x C-s | 01 | Save a file to disk |
C-x C-c | 01 | Exit emacs |
C-s | 01 | Search forward |
C-r | 01 | Search backwards |
C-v | 01 | Scroll to next screen |
M-v | 01 | Scroll to previous screen |
C-x u | 01 | Undo |
Bash Scripting¶
Running a shell script¶
Command | Lecture | Description |
---|---|---|
#!/bin/bash | 09 | At top of file, tells the command line how to interpret your file |
chmod +x script.sh | 09 | Gives script.sh execute permissions so it can be run with ./script.sh |
Variables¶
Command | Lecture | Description |
---|---|---|
CLASS="391" | 09 | Creates a variable (no spaces on either side of = sign) (variable names are case sensitive) |
echo "I am taking $CLASS" | 09 | Variables are referenced using the $ operator (output: I am taking 391) |
echo 'I am taking $CLASS' | 09 | Single quotes will not expand variables (output: I am taking $CLASS) |
contents=$(ls) | 09 | The output of running a command can be saved to a variable and referenced later |
Command Line Arguments¶
Command | Lecture | Description |
---|---|---|
$0 | 09 | Name of the script |
$1, $2, $3... etc. | 09 | First, second, third arguments |
$# | 09 | Number of arguments |
$@ | 09 | List of all arguments |
Arithmetic¶
Command | Lecture | Description |
---|---|---|
let SUM="$a + $b" | 09 | Use let expression to do arithmetic (bash supports multiplication, division, addition, and subtraction) |
PRODUCT=$(( $a * $b )) | 09 | Other way to do arithmetic, must have spaces in between operations! |
For Loops¶
Command | Lecture | Description |
---|---|---|
for i in $(seq 1 4); do; echo $i; done | 09 | For loop syntax, begins with do and ends with done. Semicolon is the same thing as a new line. |
for file in $(ls); do; echo $file; done | 09 | Iterates over all of the files in the current directory by iterating over output of ls |
If Statements¶
Command | Lecture | Description |
---|---|---|
if [ $a -lt $b ]; then | 09 | If statement basic syntax. Boolean expression must be surrounded by [ ] with spaces on either side. |
-gt, -lt, -ge, -le, -eq, -ne | 09 | greater than, less than, greater than or equal to, less than or equal to, equals, not equals |
-a | 09 | And operator. Can also be written (if [ expr1 ] && [ expr2 ]; then) |
-o | 09 | Or operator. Can also be written (if [ expr1 ] || [ expr2 ]; then) |
if [ ! expr1 ]; then | 09 | Negates expr1 |
=, != | 09 | Test if string is equal (=) or not equal (!=) |
-z, -n | 09 | Test if a string is empty (-z) or nonempty (-n) |
-f, -d | 09 | Test if a file (-f) or directory (-d) exists |
-r, -w, -x | 09 | Test if a file exists and is readable (-r), writeable (-w) or executable (-x) |
Exit Codes¶
Command | Lecture | Description |
---|---|---|
$? | 09 | Variable containing exit status of previously run program. It will be 0 if the program executed correctly, or not 0 if it failed. |
Input/Output Redirection and Command Substitution¶
Command | Lecture | Description |
---|---|---|
command < filename | 02 | stdin redirection: execute command and read its standard input from the contents of filename instead of from the console. |
command > filename | 02 | stdout redirection: execute command and redirect its standard output to the given filename |
command 2> filename | 02 | stderr redirection: execute command and redirect its standard error to the given filename |
command 2>&1 | 02 | stderr redirection: execute command and redirect its standard error to standard output |
command 2>&1 > filename | 02 | stdout and stderr redirection: execute command, redirect standard error to standard output, and redirect standard output to filename |
command1 | command2 | 02 | Pipes: execute command1 and send its standard output as standard input to command2. |
command1 ; command2 | 03 | "THEN": Execute command1, then execute command2 |
command1 && command2 | 03 | "AND": Execute command1, and if it succeeds, then execute command2 |
command1 || command2 | 03 | "OR": Execute command1, and if it fails, then execute command2 |
$(command) | 03 | Command substitution: execute command, then place the output string literally into the given context e.g. `javac $(find -name "*.java")` |
alias | 08 | Creates alias |
.bash_profile | 08 | 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 |
.bashrc | 08 | Everytime you launch a non-login shell (i.e. by launching a new terminal) the commands in ~/.bashrc are run. |
$PATH | 08 | 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 |
export PATH=/your/new/directory:$PATH | 08 | Prepend a directory to your $PATH (directory will be looked through first for a filename matching |
export PATH=$PATH:/your/new/directory | 08 | Append a directory to your $PATH (directory will be looked through last for a filename matching |
Git Commands¶
Command | Lecture | Description |
---|---|---|
git clone url [dir] | 04 | Copy a git repository |
git add file1 file2... | 04 | Adds file contents to staging area |
git stage file1 file2... | 04 | Same as git add |
git commit | 04 | Takes a snapshot of staging area and creates a commit |
git status | 04 | View status of files in working directory and staging area |
git diff | 04 | Show difference between staging area and working directory |
git log | 04 | Show commit history |
git pull | 04 | Fetch from remote repository and try to merge |
git push | 04 | Push local repository to remote repository |
git branch <branch> | 04 | Create a new branch with given name |
git checkout <branch> | 04 | Switch to the given branch |
git switch <branch> | 04 | Switch to the given branch |
git merge feature | 04 | Merges the feature branch into the current branch checked out |
git log --graph --oneline | 04 | View the commit history, and where HEAD and master are located. Flags unnecessary |
Regex Syntax¶
Command | Lecture | Description |
---|---|---|
. | 06 | Matches any character |
^ | 06 | Matches start of line |
$ | 06 | Matches end of line |
\< | 06 | Matches start of word |
\> | 06 | Matches end of word |
\ | 06 | Escape the following character |
| | 06 | Logical or operator (match this character set or that character set) |
* | 06 | Matches zero or more of the preceding character set |
+ | 06 | Matches one or more of the preceding character set |
? | 06 | Matches zero or one of the preceding character set |
() | 06 | Group characters together |
[] | 06 | Character set |
[^] | 06 | Negate character set |
[a-z] | 06 | Matches all lowercase letters |
[A-Z] | 06 | Matches all uppercase letters |
[0-9] | 06 | Matches all digits |
\1 | 06 | Back references the same characters captured in an earlier grouping (specified by the number), up to \9 |
{} | 06 | Specify how many occurrences of a match we want. Can be an exact number (i.e. {N} ) or an inclusive range of numbers (i.e. {MIN,MAX} ). |