IFI 8410 — Session 6: Unix File System and Command Line
One tree, small tools, and text flowing between them
Where we are going
Ideas, not just an OS
Multics → UNIX → BSD, Linux, macOS. Why a 1970s design still runs the cloud.
Knowing where you are
One root, paths, the working directory, and the commands that move and organize.
Composition
stdin, stdout, stderr; redirection with >; pipelines with |.
From typing to scripting
Permissions, executable scripts, and running Python from the shell.
Running example: what are the most frequent words in Shakespeare? — answered without writing a program.
Which one am I on?
bash and zsh.man command on your own machine.Do one thing well
wc -l # count lines
sort # put lines in order
uniq -c # collapse repeats, with a count
grep # keep lines that match
head # show the first few
tr # translate or delete charactersThe power is not in any one tool. It is in the interface they share.
Reading a command line
The shell reads the line, expands shorthand such as ~ and *, then starts the program. The program never sees the wildcard — only the filenames it matched.
Why bother
Over SSH there is often no desktop at all — the cluster, the server, the container.
The same operation on 10,000 files, run again next month with one command.
Ad-hoc questions answered by connecting tools no one wrote for that question.
A recorded command is evidence; a sequence of mouse clicks is a memory.
The terminal is not an alternative to programming — it is one of the environments programming happens in.
Knowing where you are
Absolute — from the root, means the same anywhere:
/home/student/ifi8410/data/scores.csv
Relative — from where you stand right now:
data/scores.csv
Building a workspace
out/ and rebuild it without risking data.cp and mv overwrite the target without asking.rm -r with a wildcard, run ls on the same pattern and read what it matched. Check pwd first.Looking without opening
less file pages through interactively — space scrolls, / searches, q quits. It never loads the whole file into memory, so it opens a gigabyte instantly.
Reading the nine characters
x means "may enter" — without it you cannot cd in, even if you can read it.chmod 755 is the same as rwxr-xr-x — one digit per class.Why two output channels
import sys
# the answer — the next program's input
print(f"{count} {word}")
# the commentary — for a human
print("warning: 3 rows skipped",
file=sys.stderr)stdout, they would land in your data file.stderr; anything a program reads goes to stdout.Same rule in the shell: echo results, but send progress messages with echo "..." >&2.
Repointing the channels
2>&1 merges stderr into stdout — one combined stream.
2>/dev/null discards errors entirely. Use it deliberately, not by habit.
Building it one stage at a time
tr -d '.,:;?!"()[]' < data/shakespeare.txt \
| tr 'A-Z' 'a-z' \
| tr -s ' \t\r' '\n' \
| grep -v -e '^[[:space:]]*$' \
| sort \
| uniq -c \
| sort -rn \
| head -n 5head and look.From typing to tooling
#!/bin/bash
# Read text on stdin,
# write "count word" on stdout.
tr -d '.,:;?!"()[]' \
| tr 'A-Z' 'a-z' \
| tr -s ' \t\r' '\n' \
| grep -v -e '^[[:space:]]*$' \
| sort | uniq -c | sort -rn#!/bin/bash on line 1 names the interpreter.chmod +x wordfreq.sh makes it runnable.stdin, so it composes like a built-in tool.The shell starts your programs too
import sys
from pathlib import Path
def main():
if len(sys.argv) < 2:
print("usage: wordfreq.py FILE [N]",
file=sys.stderr)
return 1 # exit status
path = Path(sys.argv[1])
top_n = int(sys.argv[2]) if len(sys.argv) > 2 else 20
...
if __name__ == "__main__":
sys.exit(main())sys.argv carries the words after the command.Same operating system, two dialects
| Shell | Python |
|---|---|
pwd | Path.cwd() |
ls | Path(".").iterdir() · Path(".").glob("*.csv") |
mkdir -p out/reports | Path("out/reports").mkdir(parents=True, exist_ok=True) |
cp · mv · rm | shutil.copy() · shutil.move() · Path.unlink() |
~ | Path.home() |
cmd > file · cmd 2> file | sys.stdout · sys.stderr |
Neither is "the real way". The shell is for interactive work and quick composition; Python is for logic that belongs in a program.
Now: hands-on
Guided
06-Unix-Command-Line — the word-frequency pipeline, built stage by stage, with a Your turn section at the end.
Reference
File System and CLI for the commands, UNIX Overview for the ideas, UNIX Reference to go further.
Homework 5 — organize a directory tree from the shell, run your scripts from the command line, and show the commands you used. Due Wednesday, October 14.
Take away
/; your working directory decides how relative paths resolve.pwd, ls, cd answer where you are, what is here, how to move — check before you act.mkdir, cp, mv, rm organize the tree; rm has no undo.stdin, stdout, stderr.stdout, messages on stderr — that separation is what makes composition safe.> and >> send output to a file; < feeds input from one; | connects one program to the next.chmod +x.sys.argv, exit status, and pathlib are the same ideas, seen from inside Python.
