28 Lecture 5 (Pre) — Linux Basics
29 Linux Basics — 75‑Minute Lecture (Colab Edition, using vim)
Audience: Absolute beginners to Linux
Format: using !
(line) or %%bash
(cell) magics. Editing demo uses vim
29.1 Learning Outcomes
By the end, students will be able to:
- Explain what Linux, the shell, and the filesystem are
- Navigate directories and inspect files
- Create, copy, move, and delete files/directories safely
- Use wildcards, pipes, and redirection to combine commands
- Understand permissions and make a script executable
- Edit a text file in vim (open, insert, save, quit)
- Run shell scripts in Colab and install packages (apt/pip)
29.2 Colab Setup (2–3 min)
Colab uses a Linux VM with a working directory at /content
. To run shell commands in Colab:
- One‑liner: prefix with
!
- Multi‑line: start a cell with
%%bash
# Verify we’re on Linux
!uname -a
!lsb_release -a || cat /etc/os-release
# Where are we?
!pwd
!ls -lah
Persistence: Colab VMs reset. To keep files between sessions, mount Google Drive:
from google.colab import drive
'/content/drive')
drive.mount(!ls -lah /content/drive/MyDrive
29.3 Agenda (timeboxed)
- (0–10) What is Linux? Shell vs kernel. Filesystem anatomy.
- (10–25) Navigation + basic file ops
- (25–35) Permissions & ownership (Colab nuances)
- (35–50) Redirection, pipes, text tools (grep, sort, wc, cut)
- (50–60) vim crash course (editing in a real terminal)
- (60–70) Shell scripts, variables, PATH
- (70–75) Packages (apt/pip) + help/man + wrap‑up
29.4 1) What is Linux? (0–10 min)
- Kernel: talks to hardware; Userland: tools (
bash
,ls
,vim
, etc.) - Shell: the program that reads your commands (default in Colab:
bash
). - Filesystem: a tree with root
/
(not “C:”), home~
, current.
and parent..
. - Common paths:
/bin
,/usr/bin
(programs),/home/…
(homes),/etc
(config),/var
(logs),/tmp
(temp). In Colab, workspace is/content
and you often store persistent data under/content/drive/MyDrive/...
.
Mini‑lab:
!echo $SHELL
!whoami
!pwd
!ls -lah /
29.6 3) Permissions & Ownership (25–35 min)
ls -l
output: -rwxr-xr-- 1 owner group size date name
- First char
-
file /d
directory - Triplets
rwx
for user/group/others (read/write/execute) - Change mode:
chmod u+x script.sh
(add execute for user)
Colab note: you usually run as root, so ownership quirks differ from multi‑user systems. Still practice
chmod
and safe deletions.
Mini‑lab:
%%bash
/content/play
cd > hello.sh <<'EOF'
cat #!/usr/bin/env bash
"Hello from a script"
echo
EOF-l hello.sh
ls +x hello.sh
chmod u/hello.sh .
29.7 4) Redirection, Pipes, & Essential Text Tools (35–50 min)
Redirection
>
overwrite,>>
append,2>
stderr,&>
stdout+stderr|
pipe: feed output of left command into right command
Tools
grep PATTERN
(search);-n
show line numbers,-i
case‑insensitivewc -l
(line count),sort
,uniq -c
,cut -d, -f1
,tr
,tee
Mini‑lab:
%%bash
/content/play
cd # Make a small CSV
> stocks.csv <<'EOF'
cat
ticker,price180
AAPL,420
MSFT,181
AAPL,120
NVDA,182
AAPL,
EOF# Count rows, list unique tickers with counts
-l stocks.csv
wc -d, -f1 stocks.csv | tail -n +2 | sort | uniq -c | sort -nr
cut # Filter rows with AAPL and take last 2
'^AAPL,' stocks.csv | tail -n 2 grep
29.8 5) vim Crash Course (50–60 min)
Goal: basic edits: open → insert → save → quit.
Install (if needed):
!apt-get update -qq && apt-get install -y -qq vim
Colab caution: vim is fully interactive; works best in a Terminal session (Colab’s Connect to a terminal feature or a local terminal/WSL). If you can’t open a real terminal, use the heredoc technique shown earlier to create/edit files.
vim basics
- Launch:
vim hello.txt
- Modes: Normal (default), Insert (
i
), Command (:
) - Insert text: press
i
, type; ESC to leave insert - Save & quit:
:wq
• Quit without saving::q!
• Save::w
- Navigation: arrows or
h j k l
; jump to line:42
- Search:
/pattern
thenn
/N
Exercise (in a real terminal):
vim notes.txt
- Press
i
, type a few lines ESC
, then:wq
cat notes.txt
29.9 6) Shell Scripts, Variables, PATH (60–70 min)
Variables
NAME=Alice
echo "Hi $NAME"
Script + shebang
#!/usr/bin/env bash
set -euo pipefail
NAME=${1:-World}
echo "Hello, $NAME!"
Colab demo:
%%bash
/content/play
cd > greet.sh <<'EOF'
cat #!/usr/bin/env bash
set -euo pipefail
=${1:-World}
NAME"Hello, $NAME!"
echo
EOF+x greet.sh
chmod u/greet.sh
./greet.sh Linux .
PATH
- Shows where the shell looks for executables.
!echo $PATH
!which python
- Run from current dir with
./script.sh
(current dir isn’t automatically in PATH).
29.10 7) Packages & Help (70–75 min)
System packages (apt)
!apt-get update -qq
!apt-get install -y -qq tree # example
!tree -L 2 /content/play
Python packages (pip)
%pip install -q pandas
-c "import pandas as pd; print(pd.__version__)" python
Getting help
cmd --help
(portable) •man cmd
(manual; may be minimal in Colab)apropos
to search manuals;tldr
if installed for concise examples
29.11 Mini‑Labs (for practice / homework)
Find & count: In
/usr/bin
, count how many programs start withg
.!ls -1 /usr/bin/g* 2>/dev/null | wc -l
CSV pipeline: From
stocks.csv
, output the max price per ticker.%%bash /content/play cd -F, 'NR>1 {max[$1] = ($2>max[$1] ? $2 : max[$1])} END {for (k in max) print k "," max[k]}' stocks.csv | sort awk
Permissions: Make a script that fails without execute bit; then add it and rerun.
vim: Create
todo.txt
with three lines and save.
29.12 One‑Page Cheat Sheet
Navigation: pwd
, ls -lah
, cd
, mkdir -p
, tree
(optional)
Files: touch
, cp
, mv
, rm [-r]
, cat
, less
, head
, tail
, find
, grep -R
Globs: *
, ?
, [abc]
, {a,b}
Pipes/Redirection: |
, >
, >>
, 2>
, &>
; tee
Text tools: grep
, sort
, uniq -c
, wc -l
, cut
, tr
, awk
Permissions: ls -l
, chmod u+x
, umask
Processes: ps aux
, top -bn1
, kill PID
Editing (vim): i
insert, ESC
normal, :w
save, :q
quit, :wq
save+quit, :q!
force quit
Scripts: #!/usr/bin/env bash
, chmod +x
, ./script.sh
Packages: apt-get update
, apt-get install -y PKG
; Python: %pip install PKG
Help: cmd --help
, man cmd
, which cmd
, type cmd