Shell Cheat Sheet
Shell Cheat Sheet
Annotations
Starting with a / indicates an absolute file path: /Users/denisefath/Downloads
No slash indicates a relative file path (if pwd is /Users/denisefath/, I could type ls Downloads
Change Directory
cd path cd /Users/denisefath/Downloads
Delete a File
rm info.txt Downloads/tbd.csv (deletes both info.txt and tbd.csv)
Delete a Directory
rmdir Downloads (would delete Downloads folder only if empty)
Create a Directory
mkdir Downloads (would create Downloads directory)
Show Files
ls -R will show all files recursively
Example:
SYNOPSIS
head [-n count | -c bytes] [file ...]
GREP
Grep flags:
-c: print a count of matching lines rather than the lines themselves
-h: do not print the names of files when searching multiple files
-i: ignore case (e.g., treat "Regression" and "regression" as matches)
-l: print the names of files that contain matches, not the matches
-n: print line numbers for matching lines
-v: invert the match, i.e., only show lines that don't match
grep -v -n molar seasonal/spring.csv shows lines (with row number) that have word molar
grep -c incisor seasonal/autumn.csv seasonal/winter.csv would show how many lines contain
that word in each of the files
Count Rows/Lines in a File
wc -l tbd.csv
Wild Card
wc -l Downloads/* will count the rows for all files in Downloads
wc -l Downloads/*.csv will count the rows for all csvs in Downloads
? matches a single character, so 201?.txt will match 2017.txt or 2018.txt, but not 2017-01.txt
[...] matches any one of the characters inside the square brackets, so 201[78].txt matches
2017.txt or 2018.txt, but not 2016.txt
{...} matches any of the comma-separated patterns inside the curly brackets, so {*.txt, *.csv}
matches any file whose name ends with .txt or .csv, but not files whose names end with .pdf
Quit
To force quit, use control + C
Loops
for filetype in gif jpg png; do echo $filetype; done
for file in seasonal/*.csv; do head -n 2 $file | tail -n 1; done
bash filename.sh > output.txt will run script and save output
Can use $@ to make script variables generic, so you can input them