Files
OBIJupyterHub/web_src/lectures/computers/unix/slides_unix.qmd

366 lines
13 KiB
Plaintext
Raw Normal View History

2025-10-31 14:19:31 +01:00
---
title: "Unix Basics"
format:
revealjs:
theme: beige
transition: fade
width: 1280
height: 720
center: true
---
## Why Learn Unix?
- Foundational philosophy for modern computing
- Powers most of the digital world
- Born at AT&T's Bell Labs in late 1960s
- Design principles: simplicity, modularity
## Unix Today
- **Certified Unix**: macOS
- **Unix-like Systems**: GNU/Linux, BSD variants
- **Linux**: Dominant Unix-like OS
- Understanding Unix = understanding DNA of modern OS
## Unix System Overview
**Multi-user, Multitasking Operating System**
```{mermaid}
graph TD
A[OS Kernel] --> B[User 1 Process]
A --> C[User 2 Process]
A --> D[User 3 Process]
B --> E[File System]
C --> E
D --> E
style A fill:#2e86ab,color:#fff
style B fill:#a23b72,color:#fff
style C fill:#f18f01,color:#fff
style D fill:#c73e1d,color:#fff
style E fill:#3c8d5f,color:#fff
```
## File System Structure
**Hierarchical Tree Organization**
```{mermaid}
graph TD
Root[/] --> etc["/etc<br/>config files"]
Root --> home["/home<br/>user directories"]
Root --> usr["/usr<br/>system software"]
Root --> var["/var<br/>variable data"]
home --> user1[/user1/]
home --> user2[/user2/]
user1 --> docs[Documents]
user1 --> downloads[Downloads]
style Root fill:#2e86ab,color:#fff;
style etc fill:#a23b72,color:#fff;
style home fill:#f18f01,color:#fff;
style usr fill:#c73e1d,color:#fff;
style var fill:#3c8d5f,color:#fff;
```
## File Naming Rules
**Allowed Characters:**
- Letters (a-z, A-Z), Numbers (0-9)
- Recommended: `.`, `%`, `-`, `_`, `:`, `=`
**Key Rules:**
- **Case sensitive**: `File.txt` ≠ `file.txt` ≠ `FILE.TXT`
- **No leading dash**: `-file` causes issues (use `./-file`)
- **Hidden files**: Start with `.` (e.g., `.bashrc`)
**Best Practices:**
```bash
good_file.txt # Clear and safe
data-set-01.csv # Descriptive with hyphens
project_config # No extension needed
```
## Globbing - Pattern Matching
**Wildcard Characters:**
```bash
* # Any sequence of characters
? # Any single character
[abc] # Any character in set
[a-z] # Any character in range
```
**Practical Examples:**
```bash
ls *.txt # All text files
ls report?.pdf # report1.pdf, reportA.pdf
ls [abc]*.log # a.log, b_data.log, c-backup.log
ls image[0-9].jpg # image0.jpg through image9.jpg
```
## Advanced Globbing
**Combining Patterns:**
```bash
ls *.{txt,md} # All .txt AND .md files
ls project_{dev,prod} # project_dev, project_prod
ls file[!0-9]* # Files NOT starting with digit
```
**Use in Commands:**
```bash
cp *.txt backup/ # Copy all text files
rm temp_*.log # Remove temporary logs
chmod +x *.sh # Make all scripts executable
```
**Pattern Expansion:**
```bash
echo *.py # Shows what patterns match
ls data_202{3,4}*.csv # Multiple year patterns
```
## Globbing vs Regular Expressions
**Key Differences:**
- **Globbing**: File matching (`ls *.txt`)
- **Regex**: Text pattern matching (`grep "^A" file.txt`)
**Remember:** Globbing happens **before** command execution!
## File Permissions
**Three categories × Three rights**
```bash
-rwxr-xr-- # Owner: read, write, execute
# Group: read, execute
# Others: read only
```
```{=html}
<svg viewBox="0 0 600 200" xmlns="http://www.w3.org/2000/svg">
<defs>
<pattern id="read" patternUnits="userSpaceOnUse" width="10" height="10">
<rect width="10" height="10" fill="#2e86ab" opacity="0.3"/>
<line x1="0" y1="0" x2="10" y2="10" stroke="#2e86ab" stroke-width="1"/>
</pattern>
<pattern id="write" patternUnits="userSpaceOnUse" width="10" height="10">
<rect width="10" height="10" fill="#a23b72" opacity="0.3"/>
<rect x="2" y="2" width="6" height="6" fill="#a23b72" opacity="0.6"/>
</pattern>
<pattern id="execute" patternUnits="userSpaceOnUse" width="10" height="10">
<rect width="10" height="10" fill="#f18f01" opacity="0.3"/>
<circle cx="5" cy="5" r="3" fill="#f18f01" opacity="0.6"/>
</pattern>
</defs>
<!-- Owner Permissions -->
<g transform="translate(50, 50)">
<rect x="0" y="0" width="150" height="100" fill="#2e86ab" opacity="0.1" stroke="#2e86ab" stroke-width="2"/>
<text x="75" y="30" text-anchor="middle" font-family="Arial, sans-serif" font-weight="bold" font-size="16" fill="#2e86ab">Owner</text>
<g transform="translate(40, 50)">
<rect x="0" y="0" width="20" height="20" fill="url(#read)"/>
<text x="10" y="35" text-anchor="middle" font-family="monospace" font-size="12" fill="#2e86ab">r</text>
<rect x="30" y="0" width="20" height="20" fill="url(#write)"/>
<text x="40" y="35" text-anchor="middle" font-family="monospace" font-size="12" fill="#a23b72">w</text>
<rect x="60" y="0" width="20" height="20" fill="url(#execute)"/>
<text x="70" y="35" text-anchor="middle" font-family="monospace" font-size="12" fill="#f18f01">x</text>
</g>
</g>
<!-- Group Permissions -->
<g transform="translate(225, 50)">
<rect x="0" y="0" width="150" height="100" fill="#a23b72" opacity="0.1" stroke="#a23b72" stroke-width="2"/>
<text x="75" y="30" text-anchor="middle" font-family="Arial, sans-serif" font-weight="bold" font-size="16" fill="#a23b72">Group</text>
<g transform="translate(40, 50)">
<rect x="0" y="0" width="20" height="20" fill="url(#read)"/>
<text x="10" y="35" text-anchor="middle" font-family="monospace" font-size="12" fill="#2e86ab">r</text>
<rect x="30" y="0" width="20" height="20" fill="#f5f5f5" stroke="#ccc" stroke-width="1"/>
<text x="40" y="35" text-anchor="middle" font-family="monospace" font-size="12" fill="#999">-</text>
<rect x="60" y="0" width="20" height="20" fill="url(#execute)"/>
<text x="70" y="35" text-anchor="middle" font-family="monospace" font-size="12" fill="#f18f01">x</text>
</g>
</g>
<!-- Others Permissions -->
<g transform="translate(400, 50)">
<rect x="0" y="0" width="150" height="100" fill="#f18f01" opacity="0.1" stroke="#f18f01" stroke-width="2"/>
<text x="75" y="30" text-anchor="middle" font-family="Arial, sans-serif" font-weight="bold" font-size="16" fill="#f18f01">Others</text>
<g transform="translate(40, 50)">
<rect x="0" y="0" width="20" height="20" fill="url(#read)"/>
<text x="10" y="35" text-anchor="middle" font-family="monospace" font-size="12" fill="#2e86ab">r</text>
<rect x="30" y="0" width="20" height="20" fill="#f5f5f5" stroke="#ccc" stroke-width="1"/>
<text x="40" y="35" text-anchor="middle" font-family="monospace" font-size="12" fill="#999">-</text>
<rect x="60" y="0" width="20" height="20" fill="#f5f5f5" stroke="#ccc" stroke-width="1"/>
<text x="70" y="35" text-anchor="middle" font-family="monospace" font-size="12" fill="#999">-</text>
</g>
</g>
</svg>
```
## Process Concept
**Program vs Process**
```{=html}
<svg viewBox="0 0 500 200" xmlns="http://www.w3.org/2000/svg">
<defs>
<marker id="arrowhead" markerWidth="10" markerHeight="7" refX="9" refY="3.5" orient="auto">
<polygon points="0 0, 10 3.5, 0 7" fill="#2e86ab"/>
</marker>
<filter id="shadow" x="-20%" y="-20%" width="140%" height="140%">
<feDropShadow dx="2" dy="2" stdDeviation="3" flood-color="#000000" flood-opacity="0.3"/>
</filter>
</defs>
<!-- Program (Static) -->
<g transform="translate(50, 50)">
<rect x="0" y="0" width="180" height="100" rx="10" fill="#2e86ab" filter="url(#shadow)"/>
<rect x="5" y="5" width="170" height="90" rx="8" fill="#ffffff"/>
<text x="90" y="35" text-anchor="middle" font-family="Arial, sans-serif" font-weight="bold" font-size="18" fill="#2e86ab">Program</text>
<text x="90" y="60" text-anchor="middle" font-family="Arial, sans-serif" font-size="14" fill="#666">Static Code</text>
<text x="90" y="80" text-anchor="middle" font-family="Arial, sans-serif" font-size="12" fill="#888">(On Disk)</text>
</g>
<!-- Arrow -->
<path d="M 240 100 L 310 100" stroke="#2e86ab" stroke-width="3" marker-end="url(#arrowhead)"/>
<text x="275" y="95" text-anchor="middle" font-family="Arial, sans-serif" font-size="14" fill="#2e86ab">execution</text>
<!-- Process (Dynamic) -->
<g transform="translate(320, 50)">
<rect x="0" y="0" width="180" height="100" rx="10" fill="#a23b72" filter="url(#shadow)"/>
<rect x="5" y="5" width="170" height="90" rx="8" fill="#ffffff"/>
<text x="90" y="35" text-anchor="middle" font-family="Arial, sans-serif" font-weight="bold" font-size="18" fill="#a23b72">Process</text>
<text x="90" y="60" text-anchor="middle" font-family="Arial, sans-serif" font-size="14" fill="#666">Running Instance</text>
<text x="90" y="80" text-anchor="middle" font-family="Arial, sans-serif" font-size="12" fill="#888">(In Memory)</text>
</g>
</svg>
```
## Shell Environment
**Your Command Interface**
```{=html}
<svg viewBox="0 0 500 250" xmlns="http://www.w3.org/2000/svg">
<defs>
<filter id="shellShadow" x="-20%" y="-20%" width="140%" height="140%">
<feDropShadow dx="3" dy="3" stdDeviation="4" flood-color="#000000" flood-opacity="0.2"/>
</filter>
</defs>
<!-- Main Shell Container -->
<rect x="100" y="30" width="300" height="160" rx="15" fill="#f8f9fa" stroke="#2e86ab" stroke-width="3" filter="url(#shellShadow)"/>
<!-- Shell Title -->
<text x="250" y="60" text-anchor="middle" font-family="Arial, sans-serif" font-weight="bold" font-size="20" fill="#2e86ab">Shell Environment</text>
<!-- Command Input Area -->
<rect x="130" y="80" width="240" height="30" rx="5" fill="#ffffff" stroke="#a23b72" stroke-width="2"/>
<text x="140" y="100" text-anchor="start" font-family="monospace" font-size="14" fill="#333">$ ls -l *.txt</text>
<!-- Input Source -->
<g transform="translate(50, 150)">
<rect x="0" y="0" width="80" height="40" rx="8" fill="#2e86ab" opacity="0.8"/>
<text x="40" y="20" text-anchor="middle" font-family="Arial, sans-serif" font-size="12" fill="#fff">Keyboard</text>
<text x="40" y="35" text-anchor="middle" font-family="Arial, sans-serif" font-size="10" fill="#fff">Input</text>
</g>
<!-- Output Destination -->
<g transform="translate(370, 150)">
<rect x="0" y="0" width="80" height="40" rx="8" fill="#a23b72" opacity="0.8"/>
<text x="40" y="20" text-anchor="middle" font-family="Arial, sans-serif" font-size="12" fill="#fff">Terminal</text>
<text x="40" y="35" text-anchor="middle" font-family="Arial, sans-serif" font-size="10" fill="#fff">Output</text>
</g>
<!-- Arrows -->
<path d="M 130 150 L 180 120" stroke="#2e86ab" stroke-width="2" marker-end="url(#arrowhead)"/>
<path d="M 320 120 L 370 150" stroke="#a23b72" stroke-width="2" marker-end="url(#arrowhead)"/>
<!-- Process Execution -->
<rect x="200" y="120" width="100" height="40" rx="5" fill="#f18f01" opacity="0.8"/>
<text x="250" y="140" text-anchor="middle" font-family="Arial, sans-serif" font-size="12" fill="#fff">Process</text>
<text x="250" y="155" text-anchor="middle" font-family="Arial, sans-serif" font-size="10" fill="#fff">Execution</text>
</svg>
```
## Command Structure
```bash
command [options] [arguments] [redirections]
```
**Examples:**
```bash
ls -l /home # List with details
grep "error" log.txt # Search pattern
find . -name "*.txt" # Find files
```
## Input/Output Redirection
**Controlling Data Flow**
```{mermaid}
flowchart LR
A[Input File<br/>data.txt] --> B{Command<br/>program}
B --> C[Output File<br/>results.txt]
style A fill:#2e86ab,color:#fff
style B fill:#a23b72,color:#fff
style C fill:#f18f01,color:#fff
```
## Pipes - Command Chaining
**Connect processes together**
```bash
cat file.txt | grep "error" | sort | uniq
```
```{mermaid}
flowchart LR
A[cat<br/>Reads file] -->|stdout| B[grep<br/>Filters text]
B -->|stdout| C[sort<br/>Orders data]
C -->|stdout| D[uniq<br/>Removes duplicates]
style A fill:#2e86ab,color:#fff
style B fill:#a23b72,color:#fff
style C fill:#f18f01,color:#fff
style D fill:#3c8d5f,color:#fff
```
## Essential Commands
| Category | Commands | Purpose |
|----------|----------|---------|
| **Files** | `ls, cp, mv, rm, mkdir` | Manage files and directories |
| **Navigation** | `cd, pwd` | Move around file system |
| **Text** | `cat, grep, head, tail, wc` | View and search text |
| **Process** | `ps, kill, &, bg, fg` | Manage running programs |
| **Help** | `man, --help` | Get command information |
## Getting Started
**Next Steps:**
- Practice basic file operations
- Learn command combinations with pipes
- Study pattern matching with wildcards
- Explore shell scripting for automation
- Use `man` pages for detailed help
**Remember:** Unix mastery comes through practice!