CHAPTER 1: TERMINAL and FINDER
A Technical Guide to Linux Scripting Fundamentals
1. Documentation & The REPL Model
MAN PAGES
To access the comprehensive system documentation for any utility, use the man page followed by the specific command. This is the primary source of truth for flags and arguments.
The Terminal & REPL
The Terminal is a program that facilitates an instance of Bash (the Bourne Again Shell). It operates on a REPL architecture:
- READ: It captures user input.
- EVAL: It parses and executes the command logic.
- PRINT: It outputs the result to the screen.
- LOOP: It returns to the prompt, waiting for the next instruction.
afzal@afzal $ echo hi
hi
2. Navigation and Environment Awareness
afzal@afzal $ pwd
/Users/afzal/projects
pwd: Present Working Directory. It reveals your current absolute path in the file system hierarchy.
Core Commands:
ls: List the files and sub-directories within the current directory.touch: Instantly create a new, empty file or update the timestamp of an existing one.rm: Remove/delete a file from the system.clear(orCtrl + L): Flushes the terminal buffer to provide a clean workspace.cd: Change Directory. This command modifies your working context, which can be verified usingpwd.
Command History
Use the Up and Down Arrow keys to cycle through previously executed commands for rapid re-entry.
3. Basic File Manipulation
To rename or relocate a file, use the mv (move) command.
mv original_name.txt new_name.txt
Wildcards and Safety
The * (asterisk) is a glob wildcard used to match any character pattern when deleting or moving files.
rm -i lesson-*
The -i flag enables Interactive Mode, forcing the shell to ask for a y/n (yes/no) confirmation before each deletion.
Aliases
If you want rm to always use specific flags, you can define an alias. For example, to make it recursive and forced:
alias rm='rm -rf'
To check the current configuration of an alias, use: alias rm.
4. Hidden Files & Hierarchy
Files prefixed with a . (dot) are considered hidden files.
touch .secret_config.txt
By default, Finder and the ls command do not display these files. To view them:
- Terminal: Use
ls -a(list all). - Finder: Use the shortcut
Cmd + Shift + .(orCtrl + Hon some systems).
./ represents the current directory, and ../ represents the parent directory above it in the hierarchy.
Navigation Shortcuts:
cd -: Return to the last directory you were in.cd ~: Jump to the User Home directory.cd /: Move to the system Root directory.
Tab Completion: Use the Tab key to auto-complete file and directory names. If there are multiple matching patterns, the terminal will emit a bell sound.
5. File Searching and Paging
cat: Concatenates and prints the entire contents of a file to the standard output.
cat /usr/share/dict/words
Searching with Grep:
grep is a powerful pattern-matching utility.
grep "dave" [file]: Finds occurrences of "dave" anywhere in the line.grep "^dave" [file]: Matches lines beginning with "dave".grep "dave$" [file]: Matches lines ending with "dave".
File Redirection:
echo "text" > file.txt: Overwrites the file with new content.echo "text" >> file.txt: Appends the text to the end of the existing file.
6. Advanced Grep Flags & Pipelines
Flags can be combined to provide context to your search results:
-A1: (After) Print the match and 1 line following it.-B1: (Before) Print the match and 1 line preceding it.-C1: (Context) Print the match and lines before/after.-i: Case-insensitive search.-o: Print only the matching part of the pattern.
PIPELINES (|)
Pipelines allow you to chain commands together, treating them as filters. The output of the first command becomes the input for the second.
cat file.txt | grep "dave"
If grep is not given a filename, it automatically reads from Standard Input. This is the foundation of powerful data processing in Linux.