The Shell: Your Command Center#
The shell is a text-based interface to your computer. While it might seem old-fashioned compared to graphical interfaces, itโs incredibly powerful and fast once you learn it.
What Youโll Learn#
What the shell is and why it matters
Basic navigation commands
File operations
Pipes and redirection
Tips and tricks
Prerequisites#
Access to a terminal (Linux/macOS built-in, Windows use WSL or Git Bash)
Basic typing skills
๐ Note About This Notebook#
While you can run some commands in Jupyter cells using ! or %%bash, itโs highly recommended to practice in an actual terminal for the full experience.
Open a terminal and run commands there!
1. What is the Shell?#
The shell is a program that takes commands from the keyboard and gives them to the operating system to perform.
Common Shells#
Bash (Bourne Again Shell) - Most popular, weโll use this
Zsh (Z Shell) - Default on modern macOS
Fish (Friendly Interactive Shell) - Beginner-friendly
PowerShell - Windows default (different syntax)
Terminology#
Shell: The program that interprets commands
Terminal: The application that runs the shell
Console: Physical terminal (mostly historical)
Command Line: The text interface
The Prompt#
When you open a terminal, youโll see something like:
username@hostname:~/path $
username: Your user accounthostname: Computer name~/path: Current directory (~= home directory)$: Prompt symbol (#for root user)
3. File Operations#
Creating Files and Directories#
touch file.txt # Create empty file
mkdir mydir # Create directory
mkdir -p a/b/c # Create nested directories
# Try creating a test directory
!mkdir -p test_shell/subdir
!touch test_shell/file.txt
!ls -R test_shell
Copying Files#
cp file.txt copy.txt # Copy file
cp file.txt ~/Documents/ # Copy to different directory
cp -r mydir mydir_copy # Copy directory recursively
cp *.txt backup/ # Copy all .txt files
Moving/Renaming Files#
mv oldname.txt newname.txt # Rename file
mv file.txt ~/Documents/ # Move file
mv *.txt archive/ # Move multiple files
Deleting Files#
โ ๏ธ WARNING: Deletion is permanent! No recycle bin!
rm file.txt # Delete file
rm -i file.txt # Interactive (ask before deleting)
rm -r mydir # Delete directory recursively
rm -f file.txt # Force delete (no confirmation)
rm -rf mydir # Force delete directory (DANGEROUS!)
๐ก Pro Tip#
Never run rm -rf / or rm -rf * as root - youโll delete everything!
4. Viewing File Contents#
cat - Concatenate and Display#
cat file.txt # Display entire file
cat file1.txt file2.txt # Display multiple files
cat *.txt # Display all .txt files
less - View Large Files#
less largefile.txt # Page through file
# Press:
# Space = next page
# b = previous page
# / = search
# q = quit
head and tail - View Start/End#
head file.txt # First 10 lines
head -n 20 file.txt # First 20 lines
tail file.txt # Last 10 lines
tail -n 50 file.txt # Last 50 lines
tail -f logfile.txt # Follow (watch file grow)
5. Searching#
grep - Search File Contents#
grep "pattern" file.txt # Find pattern in file
grep -i "pattern" file.txt # Case-insensitive
grep -r "pattern" directory/ # Recursive search
grep -n "pattern" file.txt # Show line numbers
grep -v "pattern" file.txt # Invert (show non-matching)
find - Search for Files#
find . -name "*.py" # Find all Python files
find /home -type d -name "test" # Find directories named test
find . -size +1M # Files larger than 1MB
find . -mtime -7 # Modified in last 7 days
6. Pipes and Redirection#
This is where the shell becomes incredibly powerful!
Output Redirection#
command > file.txt # Write output to file (overwrite)
command >> file.txt # Append output to file
command 2> error.log # Redirect errors
command &> all.log # Redirect both output and errors
Input Redirection#
command < input.txt # Read input from file
Pipes | - Chain Commands#
Send output of one command as input to another.
ls -l | grep ".py" # List only Python files
cat file.txt | wc -l # Count lines
ps aux | grep python # Find Python processes
history | tail -n 20 # Last 20 commands
cat huge.txt | head -n 100 | tail -n 10 # Lines 90-100
# Example: Count Python files
!ls -R .. | grep ".py" | wc -l
7. Useful Commands#
File Information#
wc file.txt # Count lines, words, characters
wc -l file.txt # Count lines only
du -sh directory/ # Disk usage (size)
file filename # Determine file type
stat filename # Detailed file information
Text Processing#
sort file.txt # Sort lines
uniq file.txt # Remove duplicate adjacent lines
sort file.txt | uniq # Remove all duplicates
cut -d',' -f1 file.csv # Extract first column
tr 'a-z' 'A-Z' < file # Convert to uppercase
System Information#
date # Current date and time
whoami # Current username
hostname # Computer name
uptime # How long system has been running
df -h # Disk space
free -h # Memory usage
top # Running processes (q to quit)
ps aux # All processes
8. Keyboard Shortcuts#
Essential Shortcuts#
Ctrl+C: Kill current command
Ctrl+D: Exit shell / End of input
Ctrl+Z: Suspend current command
Ctrl+L: Clear screen (same as
clear)Ctrl+A: Go to beginning of line
Ctrl+E: Go to end of line
Ctrl+U: Delete from cursor to beginning
Ctrl+K: Delete from cursor to end
Ctrl+W: Delete word before cursor
Ctrl+R: Reverse search command history
Tab: Auto-complete
โ/โ: Navigate command history
๐ก Pro Tip#
The Tab key is your best friend! It auto-completes filenames and commands.
9. Command History#
history # Show command history
history | grep git # Search history for git commands
!123 # Run command number 123 from history
!! # Run last command again
!$ # Last argument of previous command
10. Getting Help#
man - Manual Pages#
man command # Show manual for command
man ls # Manual for ls
man man # Manual for man itself!
Other Help#
command --help # Short help (most commands)
command -h # Alternative help flag
info command # Info pages (alternative to man)
which command # Show command location
type command # Show command type
๐ Exercises#
Exercise 2: File Operations#
Create a directory called
shell_practiceCreate three empty files in it:
file1.txt,file2.txt,file3.txtList the contents
Rename
file1.txttoimportant.txtDelete
file3.txt
Exercise 3: Working with Content#
Echo text into a file:
echo "Hello Shell" > greeting.txtAppend more text:
echo "Learning is fun!" >> greeting.txtDisplay the file contents
Count the lines in the file
Exercise 4: Pipes#
List all files in your current directory
Pipe that through
grepto find only.txtfilesCount how many there are with
wc -l
Exercise 5: Advanced#
Find all Python files in your Education_Playground directory:
find ~/Education_Playground -name "*.py" | wc -l
๐ฏ Common Patterns#
Find Large Files#
find . -type f -size +100M | sort -rh | head -10
Search All Python Files for Pattern#
grep -r "def" --include="*.py" .
Count Lines of Code#
find . -name "*.py" -exec wc -l {} + | tail -1
Find Recently Modified Files#
find . -type f -mtime -1
Disk Usage by Directory#
du -sh */ | sort -rh | head -10
โ ๏ธ Common Mistakes to Avoid#
No spaces in variable assignment:
x=5notx = 5Case sensitive:
File.txtโfile.txtQuote paths with spaces:
cd "My Documents"notcd My DocumentsNo undo for rm: Always double-check before deleting
rm -rf *is dangerous: Be very careful with wildcards
๐ฏ Key Takeaways#
The shell is fast and powerful once you learn it
Navigation:
pwd,ls,cdFile ops:
cp,mv,rm,mkdirViewing:
cat,less,head,tailSearching:
grep,findPipes (
|) chain commands togetherTab completion and history save time
manpages are your friend
๐ Next Steps#
Practice daily: Use the terminal for everyday tasks
Learn shell scripting: Automate repetitive tasks (
02_shell_scripting.ipynb)Master Git: Version control is essential (
03_git_essentials.ipynb)Customize: Set up aliases and functions in your
.bashrc
๐ Further Reading#
The Linux Command Line - Free book
Explainshell - Understand any command
Command Line Challenge - Practice exercises
Remember: Everyone started as a beginner. Practice makes perfect! ๐