The terminal and the shell
Be able to navigate, run programs and connect commands together in a Unix shell.
Prerequisites
- CPython — the basicsrequired
Intuition
The basics:
| Command | Does |
|---|---|
pwd | where am I? |
ls -la | list the files, hidden ones included |
cd ../data | change directory |
cat file.txt | show the contents |
less file.txt | page through it (q to quit) |
cp a b, mv a b, rm a | copy, move, remove |
mkdir -p a/b | create a directory tree |
grep -r "pattern" . | search in files |
find . -name "*.csv" | find files |
rm does not ask. There is no wastebasket.
Code
# A pipe: send the output of one command to the next
cat data.csv | wc -l # the number of lines
grep "ERROR" app.log | head -20 # the first 20 errors
# Redirection
python train.py > output.txt 2>&1 # stdout and stderr to a file
python train.py >> log.txt # append instead of overwriting
# A chain: find the ten most common errors in a log
grep "ERROR" app.log | cut -d' ' -f5- | sort | uniq -c | sort -rn | head -10
# Environment variables — this is how you pass secrets to a program
export API_KEY="..." # only in this terminal
python script.py # os.environ["API_KEY"] in Python
# Run it in the background and keep the log
nohup python long_job.py > job.log 2>&1 &
The chain above is why the shell is worth learning: six small programs that each do one thing, connected into an analysis that would have taken twenty lines of Python.
Mastery means
- Navigates and handles files in a shell
- Connects commands together with pipes and redirection
- Runs programs with arguments and environment variables
Sign in to do the exercises and build your mastery up.
Sources
- GNU Bash — manual (GFDL) — GFDL