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

ssh - LEC 01

Log in to a remote Linux server (e.g., attu)

pwd - LEC 01

Print current working directory

cd - LEC 01

Change working directory

ls - LEC 01

List files in working directory
(relevant flags: -a -l -r)

man - LEC 01

Bring up manual for a command

exit - LEC 01

Log out of shell

xargs - LEC 03

Converts standard input to command line arguments
(relevant flags: -d)

tee - LEC 03

Allows for the output of a command to be redirected to both a file and stdout


System Commands

clear - LEC 01

Clears all output from console

date - LEC 01

Output the system date

cal - LEC 01

Output a text calendar

uname - LEC 01

Print information about the current system


Directory Commands

ls - LEC 01

List files in working directory

pwd - LEC 01

Print current working directory

cd - LEC 01

Change working directory

mkdir - LEC 01

Make a new directory

rmdir - LEC 01

Remove the given directory (must be empty)


File Commands

cp - LEC 01

Copy a file
(relevant flags: -i)

mv - LEC 01

Move a file (also used to rename files)
(relevant flags: -i)

rm - LEC 01

Remove the given file
(relevant flags: -f -r)

touch - LEC 01

Create empty file, or change time-modified

chmod who(+-)what filename - LEC 08

Change permissions for filename (e.g., give owners/group read/write, others).
Example: chmod ug+rw,o-rwx file.sh

chmod NNN filename - LEC 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] - LEC 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

cat - LEC 02

Print contents of a file

less - LEC 02

Output file contents, one page

more - LEC 02

Output file contents, one page

head - LEC 02

Output number of lines of start of file
(relevant flags: -n)

tail - LEC 02

Output number of lines of end of file
(relevant flags: -n)

wc - LEC 02

Count words, characters, lines in a file
(relevant flags: -l -w)


Searching and Sorting Commands

grep - LEC 02

Search given file for pattern
(relevant flags: -E -i -v)

sort - LEC 02

Sort input or file, line based
(relevant flags: -f -r)

uniq - LEC 02

Strip duplicate adjacent lines

find - LEC 02

Search filesystem
(relevant flags: -type)

cut - LEC 02

Remove section from each line of file

sed -r 's/REGEX/TEXT/' file.txt - LEC 07

This will replace all matches of REGEX in the given file with TEXT and output it to the console.


Users

whoami - LEC 08

Print your username

id - LEC 08

Print user ID and group membership

users - LEC 08

List logged-in users (short)

who - LEC 08

List logged-in users (long)

pinky - LEC 08

Print information about users


Groups

groups - LEC 08

Print group membership

groupadd - LEC 08

Create a group

groupdel - LEC 08

Delete a group

groupmod - LEC 08

Modify a group

chgrp - LEC 08

Assign to group

chgrp -R . - LEC 08

Changes all files recursively starting in the current directory to be owned by


Processes

ps - LEC 08

List processes being run
(relevant flags: -u)

top - LEC 08

Show process statistics

kill - LEC 08

Terminate process by PID

killall - LEC 08

Terminate process by name


Java Commands

javac ClassName.java - LEC 02

Compile ClassName

java ClassName - LEC 02

Run ClassName

python, ruby, perl, gcc, go, etc - LEC 02

Run or compile other files in different languages!



Unix File System

Relative Directories

. - LEC 01

References the working directory

.. - LEC 01

References the parent of working directory

~username - LEC 01

username's home directory

~/Desktop - LEC 01

Your desktop


Unix File System

/ - LEC 01

Root directory that contains all directories

/bin - LEC 01

Applications/programs (i.e. binaries)

/dev - LEC 01

Hardware devices

/etc - LEC 01

Configuration files

/home - LEC 01

Contains user's home directories

/proc - LEC 01

Running programs (processes)

/tmp, /var - LEC 01

Temporary files

/usr - LEC 01

Universal system resources

Directory permissions - LEC 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

nano - LEC 01

Very simple editor

vim - LEC 01

More advanced text-editor

emacs - LEC 01

More advanced text-editor

Vim Basics

:w - LEC 01

Write (save) the current file

:wq - LEC 01

Write (save) the current file and exit

:q! - LEC 01

Quit, ignoring all changes

i - LEC 01

Go into insert mode

Esc - LEC 01

Go back to normal mode

hjkl - LEC 01

Move cursor left, down, up, right

u - LEC 01

Undo last change

x - LEC 01

Delete character


Emacs Basics

C = control key, M = alt/meta key

C-x C-f - LEC 01

Read a file into emacs

C-x C-s - LEC 01

Save a file to disk

C-x C-c - LEC 01

Exit emacs

C-s - LEC 01

Search forward

C-r - LEC 01

Search backwards

C-v - LEC 01

Scroll to next screen

M-v - LEC 01

Scroll to previous screen

C-x u - LEC 01

Undo



Bash Scripting

Running a shell script

#!/bin/bash - LEC 09

At top of file, tells the command line how to interpret your file

chmod +x script.sh - LEC 09

Gives script.sh execute permissions so it can be run with ./script.sh


Variables

CLASS="391" - LEC 09

Creates a variable (no spaces on either side of = sign) (variable names are case sensitive)

echo "I am taking $CLASS" - LEC 09

Variables are referenced using the $ operator (output":" I am taking 391)

echo 'I am taking $CLASS' - LEC 09

Single quotes will not expand variables (output":" I am taking $CLASS)

contents=$(ls) - LEC 09

The output of running a command can be saved to a variable and referenced later


Command Line Arguments

$0 - LEC 09

Name of the script

$1, $2, $3... etc. - LEC 09

First, second, third arguments

$# - LEC 09

Number of arguments

$@ - LEC 09

List of all arguments


Arithmetic

let SUM="$a + $b" - LEC 09

Use let expression to do arithmetic (bash supports multiplication, division, addition, and subtraction)

PRODUCT=$(( $a * $b )) - LEC 09

Other way to do arithmetic, must have spaces in between operations!


For Loops

for i in $(seq 1 4); do; echo $i; done - LEC 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 - LEC 09

Iterates over all of the files in the current directory by iterating over output of ls


If Statements

if [ $a -lt $b ]; then; echo "a is less than b"; else; echo "a is not less than b"; fi; - LEC 09

If statement basic syntax. Boolean expression must be surrounded by [ ] with spaces on either side.

-gt, -lt, -ge, -le, -eq, -ne - LEC 09

greater than, less than, greater than or equal to, less than or equal to, equals, not equals

-a - LEC 09

And operator. Can also be written (if [ expr1 ] && [ expr2 ]; then)

-o - LEC 09

Or operator. Can also be written (if [ expr1 ] || [ expr2 ]; then)

if [ ! expr1 ]; then - LEC 09

Negates expr1

=, != - LEC 09

Test if string is equal (=) or not equal (!=)

-z, -n - LEC 09

Test if a string is empty (-z) or nonempty (-n)

-f, -d - LEC 09

Test if a file (-f) or directory (-d) exists

-r, -w, -x - LEC 09

Test if a file exists and is readable (-r), writeable (-w) or executable (-x)


Exit Codes

$? - LEC 09

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

command < filename (Input redirection) - LEC 02

Execute command and read its standard input from the contents of filename instead of from the console.

command > filename (Output redirection) - LEC 02

Execute command and redirect its standard output to the given filename

command 2> filename (Stderr redirection) - LEC 02

Execute command and redirect its standard error to the given filename

command 2>&1 (Stderr redirection) - LEC 02

Execute command and redirect its standard error to standard output

command 2>&1 filename (Stderr redirection) - LEC 02

Execute command, redirect standard error to standard output, and redirect standard output to filename

command1 | command2 (Pipes) - LEC 03

Execute command1 and send its standard output as standard input to command2.

command1 ; command2 (Combining commands) - LEC 03

Execute command1, then execute command2

command1 && command2 (Combining commands) - LEC 03

Execute command1, and if it succeeds, then execute command2

$(command) (Command Substitution) - LEC 03

Execute command, then place the output string literally into the given context i.e. javac $(find -name "*.java")

alias = - LEC 08

Creates alias for (do NOT put spaces surrounding the equals)

.bash_profile - LEC 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 - LEC 08

Everytime you launch a non-login shell (i.e. by launching a new terminal) the commands in ~/.bashrc are run.

$PATH - LEC 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 , will be run when you type

export PATH=/your/new/directory:$PATH - LEC 08

Prepend a directory to your $PATH (directory will be looked through first for a filename matching when )

export PATH=$PATH:/your/new/directory - LEC 08

Append a directory to your $PATH (directory will be looked through last for a filename matching when )


Git Commands

git clone url [dir] - LEC 04

Copy a git repository

git add file1 file2... - LEC 04

Adds file contents to staging area

git stage file1 file2... - LEC 04

Same as git add

git commit - LEC 04

Takes a snapshot of staging area and creates a commit

git status - LEC 04

View status of files in working directory and staging area

git diff - LEC 04

Show difference between staging area and working directory

git log - LEC 04

Show commit history

git pull - LEC 04

Fetch from remote repository and try to merge

git push - LEC 04

Push local repository to remote repository

git branch "branchname" - LEC 05

Create a new branch with the name branch name

git checkout "branch name" - LEC 05

Switch to the branch branch name

git merge feature - LEC 05

Merges the feature branch into the current branch checked out

git log --graph --oneline - LEC 05

View the commit history, and where HEAD and master are located. Flags unnecessary


Regex Syntax

. - LEC 06

Matches any character

^ - LEC 06

Matches start of line

$ - LEC 06

Matches end of line

\< - LEC 06

Matches start of word

\> - LEC 06

Matches end of word

\ - LEC 06

Escape the following character

-i - LEC 06

(Flag to grep) Match case insensitively

| - LEC 06

Logical or operator (match this character set or that character set)

* - LEC 06

Matches zero or more of the preceding character set

+ - LEC 06

Matches one or more of the preceding character set

\? - LEC 06

Matches zero or one of the preceding character set

() - LEC 06

Group characters together

[] - LEC 06

Character set

['^'] - LEC 06

Negate character set

['a-z'] - LEC 06

Matches all lowercase letters

['A-Z'] - LEC 06

Matches all uppercase letters

['0-9'] - LEC 06

Matches all digits

\1 (or \2 \3 etc.) - LEC 06

Back refernences the same characters captured in an earlier grouping (specified by the number)