Last updated: 2026-07-21
Status:
Tags: Software Engineering,Backend Frameworks
Shell Introduction
Concepts
- Shell
- The shell is a textual interface that allows running programs, providing input, and inspecting output, which is not restricted like graphical or voice interfaces.
- Nearly all platforms have shells, which are similar in core despite differences in details.
- Common Shells
- Bash (Bourne Again SHell): one of the most widely used shells, with syntax similar to many others.
- zsh (Z Shell): a powerful and highly customizable shell that builds on features from bash and other shells. It offers advanced tab completion, improved globbing, powerful prompt customization, and a rich plugin ecosystem (such as Oh My Zsh)
- The shell is a programming environment with variables, conditionals, loops, and functions.
- $PATH is an environment variable that lists directories the shell searches for programs when a command is given.
- The directories in $PATH are separated by colons (😃.
- You can use the which command to determine the location of a specific executable file within the directories listed in $PATH.
- File Path: A path in the shell is a delimited list of directories, with / as the separator on Linux and macOS, and \ on Windows; / is the root on Linux and macOS, while Windows has a root per disk partition.
- Absolute path starts with /; relative path is relative to the current working directory.
- . refers to the current directory, .. to its parent directory.
- Programs operate in the current directory unless specified otherwise.
- Programs have input and output streams, which can be redirected using < file, > file, >> (append), and pipes (|) to chain programs.
- Root User: The root user has almost all access restrictions
- sudo is used to perform actions as root;
- The root user has unrestricted access, but users typically use sudo to perform actions as root instead of logging in as root.
- Operations like |, >, < are handled by the shell, not individual programs, which affects permission when using sudo with redirection (e.g., sudo echo 3 > brightness fails, but echo 3 | sudo tee brightness works).
- sysfs exposes kernel parameters as files on Unix-like systems (not on Windows or macOS), allowing kernel reconfiguration.
- Writing to the sysfs file system:
- Requires root privileges
- Mounted under/sys
- Exposes kernel parameters as files for easy reconfiguration - sysfs is not available on Windows or macOS
- Writing to the sysfs file system:
- Using the Shell Commands
- The shell parses commands by splitting them by whitespace, running the program indicated by the first word, and supplying subsequent words as arguments.
- To provide arguments with spaces or special characters, use ' or " to quote the argument, or \ to escape relevant characters.
- The shell prompt indicates the current working directory, and can be configured to show various useful information (covered in a later lecture).
- Most commands accept flags and options starting with - to modify behavior; -h or --help often prints help text.
- Input and output streams of programs can be redirected: < file rewires input to a file, > file rewires output to a file, >> appends output to a file.
- Pipes (|) chain programs, making the output of one the input of another.
- File Permissions
- File and directory permissions are indicated by three groups of three characters (rwx) in the long listing format (ls -l), representing permissions for the owner, the owning group, and everyone else respectively.
- A 'd' at the start of a line in ls -l indicates the item is a directory.
- 'r' (read), 'w' (write), and 'x' (execute) are the basic permissions: the owner needs 'w' to modify a directory (add/remove files); users need 'x' (search) permission to enter a directory (and its parents) and 'r' permission to list its contents.
- Most files in /bin have 'x' permission for everyone, allowing anyone to execute them.
- chmod: Changes file permissions; e.g., chmod to make a file executable.
Commands/Exercise
- Commands
- date: Executes the date program to print the current date and time.
- echo: Prints out its arguments; e.g., echo hello prints hello.
- which: Finds which file is executed for a given program name; e.g., which echo shows /bin/echo.
- pwd: Shows the current working directory.
- cd: Changes the current working directory; e.g., cd /home changes to the /home directory.
- ls: Lists the contents of a directory; with -l uses long listing format, --help shows help.
- ls -l: Uses long listing format to show detailed information about files/directories, including permissions, owner, group, size, and modification time.
- ls --help: Displays help text for the ls command, explaining available flags and options.
- mv: Renames or moves a file.
- cp: Copies a file.
- mkdir: Creates a new directory.
- man: Shows the manual page of a program; e.g., man ls shows the manual for ls; press q to exit.
- cat: Concatenates files; when given file names, prints their contents; without arguments, prints input stream to output stream.
- curl: Used with --head --silent to get header information silently; e.g., curl --head --silent google.com.
- grep: Searches for patterns; e.g., grep --ignore-case content-length searches for content-length case-insensitively.
- cut: Splits lines by a delimiter; e.g., cut --delimiter=' ' -f2 splits by space and takes the second field.
- tail: Outputs the last part of files; e.g., tail -n1 outputs the last line.
- sudo: Allows performing actions as root; e.g., sudo tee brightness runs tee as root.
- find: Searches for files; e.g., sudo find -L /sys/class/backlight -maxdepth 2 -name 'brightness' searches for brightness-related files.
- chmod: Changes file permissions; e.g., chmod to make a file executable.
- Exercises
- Ensure using a Unix shell like Bash or ZSH; check with echo $SHELL.
- Create a new directory called missing under /tmp.
- Look up the touch program using man.
- Use touch to create a new file called semester in missing.
- Write specific content into semester, handling quotes properly.
- Try to execute ./semester and understand why it doesn't work by checking ls output.
- Run sh semester and understand why it works.
- Look up chmod using man.
- Use chmod to make ./semester executable and understand how the shell knows to use sh (shebang line).
- Use | and > to write the "last modified" date from semester into lastmodified.txt in the home directory.
- Write a command to read laptop battery power level or desktop CPU temperature from /sys (macOS users skip).
Shell Scripting and Shell Tools
Shell Scripting Overview
- Shell scripting is used to automate shell tasks, incorporating variables, control flow, and other syntax. Optimized for tasks like command pipelines, file management, and input/output handling.
- Focus on bash scripting as it is the most commonly used shell.
Bash as a programming language
- Variables in Bash
- Assignment:
foo=bar;spaces matter (foo=bar works, foo = bar doesn't) - Accessing variables:
$foo - No spaces allowed:
foo = barcauses an error. - Space character: Used for argument splitting in bash.
- Assignment:
- Strings in Bash
- Single Quotes : Literal strings (no variable substitution).
- Double Quotes : Allows variable substitution.
Bash foo=bar echo "$foo" # Outputs: bar echo '$foo' # Outputs: $foo
- Control Flow in Bash
- Conditional statements:
if,case,while, andfor.
- Conditional statements:
- Functions: in Bash
- Syntax:Bash
function_name () { # commands } # example mcd () { mkdir -p "$1" cd "$1" }1
2
3
4
5
6
7
8- Arguments are accessed via
$1,$2,
- Arguments are accessed via
- Special Variables
$0: Script name.$1to$9: Script arguments.$@: All arguments.$#: Number of arguments.$?: Return code of the previous command.$$: Process ID of the script.!!: Last command with arguments.$_: Last argument of the previous command.
- Function Exit Codes
- Exit codes: Indicate the success or failure of commands.
0: Success.- Non-zero: Error.
- Short-circuiting operators:
&&: Executes next command only if the first one succeeds.||: Executes next command only if the first one fails.Bash false || echo "Oops, fail" # Outputs: Oops, fail true && echo "Success" # Outputs: Success
- Exit codes: Indicate the success or failure of commands.
- Syntax:
- Command Substitution
- Syntax:
$(command) - Use case: Capturing output of a command and using it in another command
Bash echo "Today is $(date)"
- Syntax:
- Process Substitution
- Syntax:
<(command) - Use case: Executes a command and uses the output as input to another command
Bash diff <(ls dir1) <(ls dir2)
- Syntax:
- Globbing and Wildcards
- Wildcards: Match patterns in filenames.
*: Matches any number of characters.?: Matches one character.- Examples
Bash rm foo? # Deletes files like foo1, foo2 rm foo* # Deletes all files starting with foo
- Curly braces
{}: Expand similar commands or filenames.Bash cp file.{txt,pdf} /newdir # Expands to: cp file.txt file.pdf /newdir # Creates files foo/a, foo/b, ..., bar/h touch {foo,bar}/{a..h}
- Wildcards: Match patterns in filenames.
Shell Script
- Shell Functions vs Scripts
- Functions: Loaded into the current shell and modifies the environment.
- Scripts: Executed in a new process, can't change the parent shell’s environment.
- Functions must be in the same language as the shell; scripts can be written in any language (as long as the interpreter is specified with a shebang).
- Functions are loaded once when defined; scripts are loaded each time they run.
- Functions can modify shell variables; scripts cannot.
- Bash Script Execution
- Shebang (
#!/bin/bas): Defines the interpreter. - Use
#!/usr/bin/env bashfor portability (resolves the correct path to the interpreter).- ExampleBash
#!/bin/bash echo "Starting program at $(date)" echo "Running program $0 with $# arguments with pid $$" for file in "$@"; do grep foobar "$file" > /dev/null 2> /dev/null if [[ $? -ne 0 ]]; then echo "File $file does not have any foobar, adding one" echo "# foobar" >> "$file" fi done1
2
3
4
5
6
7
8
9
10
- Example
- Explanation:
$(date): Command substitution to print current date.$0,$#,$$: Special variables for script name, argument count, and PID.grep foobar "$file": Search forfoobarin files.if [[ $? -ne 0 ]]: Check ifgrepfoundfoobar. If not, add it to the file
- Shebang (
Shell Tools
Finding Command Information
- Using Help Flags
- Commands often provide a quick reference via
-hor--help. - Example:
ls --helpgives a brief description of the command and available flags.
- Commands often provide a quick reference via
- Using Man Pages
man <command>shows detailed manuals for commands.- Example:
man rmdisplays detailed options, including flags like-ifor interactive removal. - Interactive Programs: Use
:helpor?for internal help
- Using TLDR Pages
tldr <command>offers simplified, example-based help for common commands.- Use Case: Useful for frequently used complex commands like
tarorffmpeg.
Finding Files
- Using
find- Recursive search for files and directories matching criteria.
- Examples:
- Find all directories named
src:find . -name src -type d - Find all Python files in
testfolder paths:find . -path '*/test/*.py' -type f - Find files modified in the last day:
find . -mtime -1 - Find zip files between 500k and 10M in size:
find . -size +500k -size -10M -name '*.zip'
- Find all directories named
- Examples:
- Performing Actions with
find- Execute commands over the matched files.
- Examples:
- Delete
.tmpfiles:find . -name '*.tmp' -exec rm {} \; - Convert PNG to JPG:
find . -name '*.png' -exec convert {} {}.jpg \;
- Delete
- Alternatives to
findfd: A faster, more user-friendly alternative tofind. Supports colorized output, default regex matching, and easier syntax.Example:fd PATTERN(vsfind -name '*PATTERN*')locate- Uses a database of file paths, updated by
updatedb.- on Mac, you need to run
sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist
- on Mac, you need to run
- Faster but potentially less fresh results than
find. - Example:
locate filename
- Uses a database of file paths, updated by
- Recursive search for files and directories matching criteria.
Finding Code: Using grep
- Search for patterns in files or input streams.
- Examples:
- Print 5 lines before and after a match:
grep -C 5 "pattern" file.txt - Recursive search through directories:
grep -R "pattern" - Invert the match (find lines without the pattern):
grep -v "pattern"
- Print 5 lines before and after a match:
- Examples:
- Alternatives to
greprg(ripgrep) Faster and more intuitive thangrep. Supports recursive search and advanced options.- Examples:
- Find all Python files using the
requestslibrary:rg -t py 'import requests' - Find all files without a shebang:
rg -u --files-without-match "^#\!" - Print statistics of matches:
rg --stats PATTERN
- Find all Python files using the
Finding Shell Commands
- Using
history- Prints all past shell commands in the session.
- Example:
history | grep findsearches for the use offindin your command history.
- Using
Ctrl + R- Reverse search through command history.
- Allows you to match commands by substring.
- Using
fzf- A fuzzy finder that can search command history and other sources with visual feedback.
- Autosuggestions
- Dynamically suggests the most recent matching commands as you type.
- Available in
fishshell and can be enabled inzsh.
- Preventing Sensitive Commands from Being Saved
- Use
HISTCONTROL=ignorespacein.bashrcorsetopt HIST_IGNORE_SPACEin.zshrcto avoid saving commands that start with a space (useful for commands containing sensitive data).
- Use
Directory Navigation
- Efficient Navigation with Aliases and Symlinks
- Use
ln -sto create symbolic links for quick access to directories. - Example:
ln -s /path/to/project ~/project_link
- Use
- Using
fasdandautojumpfasd: Tracks frequency and recency of accessed directories for quick navigation withz <substring>.- Example:
z projectto jump to/home/user/files/cool_project.
- Example:
autojump: Similar functionality usingj <substring>.
- Tools for Directory Structure Overview
tree: Visualizes directory structure in a tree format.- **
broot****,**nnn**, ****ranger**: Advanced file managers for interactive directory navigation.
Data Wrangling
data wrangling: transforming data from one format to another using command-line tools and techniques. It emphasizes combining tools to manipulate both textual and binary data effectively.
Core Concepts and Tools
Data wrangling often involves using pipes (|) to chain commands. Key tools and their roles are detailed:
- grep: Filters data (e.g., extracting SSH-related logs from server journals).
- sed: A stream editor for text modification, commonly used for substitution (
s/REGEX/SUBSTITUTION/). It supports regular expressions and capture groups to extract specific data. - awk: A programming language for processing text streams, adept at field-based operations (e.g., printing specific columns with
{print $2}) and conditional logic. - sort and uniq: Sort data and count occurrences (
uniq -c), withsort -nfor numeric sorting and-kto specify sort columns. - less: A pager for navigating long outputs.
- bc: A calculator for mathematical operations on input streams.
- R: Used for statistical analysis (e.g., generating summary statistics for data).
- gnuplot: For simple data visualization (e.g., box plots of username occurrence counts).
- xargs: Passes processed data as arguments to other commands (e.g., uninstalling software).
Regular Expressions
A critical component of data wrangling, regular expressions (regex) enable pattern matching. Key syntax includes:
.: Any single character (except newline).*/+: Zero or more/one or more of the preceding element (greedy by default).[abc]: Any one of the specified characters.(RX1|RX2): Matches either regex pattern.^/$: Start/end of a line.- Capture groups
(...)store matched text for reuse in substitutions (e.g.,\1for the first group).
Notably, sed has unique regex behavior (requiring \ for special characters unless -E is used), while tools like Perl support non-greedy matching (*?).
Practical Examples
analyzing SSH login attempt logs to demonstrate workflows:
- Filtering logs: Use
grep sshdandgrep "Disconnected from"to isolate relevant entries. - Extracting usernames:
sedwith regex (and capture groups) extracts usernames from filtered logs. - Counting and sorting:
sort | uniq -ccounts username occurrences;sort -nk1,1sorts by count, withtail -n10to get top 10. - Further processing:
awkextracts specific fields (e.g., usernames),Rcomputes statistics (e.g., summary of counts), andgnuplotvisualizes results.
Binary Data Wrangling
Pipes work for binary data too. For example, ffmpeg captures a camera image, converts it to grayscale, compresses it with gzip, sends it over SSH, decompresses it, saves a copy, and displays it.
Exercises
Six exercises reinforce concepts:
- Practicing regex via an interactive tutorial.
- Analyzing words in
/usr/share/dict/words(e.g., counting those with at least three "a"s and no "'s" ending). - Explaining pitfalls of in-place substitution (e.g.,
sed ... input.txt > input.txt) and finding correct methods. - Calculating average, median, and max system boot times over the last 10 boots using
journalctl(Linux) orlog show(macOS). - Identifying boot messages unique to the past three reboots by filtering, normalizing, and deduplicating logs.
- Fetching online datasets with
curl, extracting numerical columns, and computing min/max/sum differences.