Commands

Honda,$18000
BMW,$22000
Toyota,$12000
one     1
two 2
three        3
cat cars.txt | sort # sorts lines in alphabetical order
cat cars.txt | wc -l # print number of lines
cat cars.txt | wc -m # print number of chars
cat cars.txt | wc -w # print number of words
cat cars.txt | cut -f1 -d, # print first field, delimited by ','
cat cars.txt | cut -f1,2 -d, # print first and second field, delimited by ','
cat numbers.txt | awk '{print $2}' # print the second field, delimited by any number of spaces
cat /etc/services | grep '[0-9]\{2,\}' # run regex in grep (2 or more digits)
head -2 cars.txt # first two lines
head # first ten lines
tail -2 cars.txt # last two lines
tail cars.txt # last ten lines
cat cars.txt 1>a.txt 2>b.txt # redirect stdout to a, stderr to b.txt
grep dog * # list the files that have "dog" in them and the lines
grep -l dog * # list the filenames that have "dog" in them
grep -l dog * | uniq # unique lines from input

Grep / Regex

  • ^ and $ match the beginning and end of a line respectively
  • \< and \> match the beginning and end of a word respectively
  • . - full stop matches any character
  • string1\|string2 - matches string1 or string2
  • [abc] matches any character that is one of a/b/c
  • [^abc] matches any character that is NOT a/b/c
  • [a-z],[A-Z], [0-9], matches lowercase/uppercase/digit
  • *, \+, matches zero or more of the previous thing, matches 1 or more of the previous thing
grep 'Honda\|BMW' cars.txt # lines that contain either Honda or BMW
grep -rl '^something' /directory # all the files in directory that start with the string "something"; r=recursive, l=files-with-matches
grep -v 2 # filter out the lines that have 4 in it
grep -w "1.2" # grep for exact matches (no regex)

Piping / Scripting

STDIN - 0, STDOUT - 1, STDERR - 2

  • List all files ending in .txt: ls *.txt
  • List all files beginning with 1, 2, or 3: ls [123]*
  • List all files with ’ hello ’ in their name: ls *[[:digit:]]*
#!/bin/bash
 
VARIABLENAME="world"
MYNUMBER=12
OUTPUT=$(echo hi) # hi, output is the stdout of the command
 
MYSECONDNUMBER = $(($MYNUMBER + 3)) # works for +,-,/,*,%
 
echo "Hello, $VARIABLENAME!" # Hello, world!
echo 'Hello, $VARIABLENAME!' # Hello, $VARIABLENAME! (note ' and ")
echo "Hello, ${MYNUMBER}a$MYSECONDNUMBER!" # Hello, 12a15
echo {my,your}$VARIABLENAME # myworld yourworld
 
if [[ -z "$VARIABLENAME" ]]; then # if the variable is null
	# strings, we prefer < <= == > >=
	echo "Variable name is empty"
elif [[ $MYNUMBER -gt $MYSECONDNUMBER ]]; then
	# -gt -le -ge -le -eq -ne
	echo "First number is greater"
else
	echo "This actually prints"
fi
 
echo "Redirect stdout to stderror" 1>&2 # results in an error
  • $1, $2, $3: first/second/third (etc) argument
  • $*: string of all arguments (such as "1 2 3")
  • $#: number (count) of arguments
  • $?: exit code of previously run command:
ls somefile
echo $?

Normal mode

h, j, k, l (left, down, up, right)

dd to delete the current line yy to copy the line p to paste below u undo

G - jump to last line, gg - jump to first line

w - jump to start of next word W - jump to start of next word (words with punctuation) b - jump to start of previous word B - jump to start of previous word (words with punctuation)

0 - jump to start of line, $ - jump to end of line

yy yank a line 2yy yank 2 lines y$/Y copy to end of line p put the clipboard after cursor P put before cursor dd delete a line 2dd delete 2 lines dw delete a word d$/D delete to end of line d0 delete to beginning of line x delete character

Visual mode

v - select character wise, V - select line-wise, v followed by w to select a word

Search/Replace

:%s/abc/ABC/g: replace all the ‘abc’s with “ABC”s (%s is needed for changing all occurrences) :%s/\ \{1,}/,/g: replace many spaces with one comma