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 account

  • hostname: 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 1: Navigation#

  1. Find your current location with pwd

  2. Go to your home directory

  3. List all files including hidden ones

  4. Go back to where you started

Exercise 2: File Operations#

  1. Create a directory called shell_practice

  2. Create three empty files in it: file1.txt, file2.txt, file3.txt

  3. List the contents

  4. Rename file1.txt to important.txt

  5. Delete file3.txt

Exercise 3: Working with Content#

  1. Echo text into a file: echo "Hello Shell" > greeting.txt

  2. Append more text: echo "Learning is fun!" >> greeting.txt

  3. Display the file contents

  4. Count the lines in the file

Exercise 4: Pipes#

  1. List all files in your current directory

  2. Pipe that through grep to find only .txt files

  3. Count 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#

  1. No spaces in variable assignment: x=5 not x = 5

  2. Case sensitive: File.txt โ‰  file.txt

  3. Quote paths with spaces: cd "My Documents" not cd My Documents

  4. No undo for rm: Always double-check before deleting

  5. rm -rf * is dangerous: Be very careful with wildcards


๐ŸŽฏ Key Takeaways#

  • The shell is fast and powerful once you learn it

  • Navigation: pwd, ls, cd

  • File ops: cp, mv, rm, mkdir

  • Viewing: cat, less, head, tail

  • Searching: grep, find

  • Pipes (|) chain commands together

  • Tab completion and history save time

  • man pages 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#

Remember: Everyone started as a beginner. Practice makes perfect! ๐Ÿš€