Command Line Essentials: Terminal Commands Every Developer Should Know

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MyrinNew
    Senior Member
    • Feb 2024
    • 5175

    #1

    Command Line Essentials: Terminal Commands Every Developer Should Know

    Command Line Essentials: Terminal Commands Every Developer Should Know

    Table of Contents

    • What is the Command Line?
    • Why Developers Use the Command Line
    • Opening Your Terminal
    • Understanding the Command Line Interface
    • Essential Navigation Commands
    • Working with Files and Directories
    • Viewing and Editing Files
    • File Permissions and Ownership
    • Using Git from the Command Line
    • Process Management
    • Command Line Shortcuts and Tips
    • Common Mistakes to Avoid
    • Troubleshooting Guide





    Introduction

    The command line might look intimidating with its black screen and blinking cursor, but it's one of the most powerful tools in a developer's toolkit. In this tutorial, you'll learn the essential terminal commands that will make you more efficient and confident as a developer.


    What you'll learn:
    • What the command line is and why it matters
    • Essential commands for navigating your file system
    • How to create, move, and delete files and folders
    • Using Git from the command line
    • Productivity shortcuts and tips
    • How to troubleshoot common issues


    Prerequisites:
    • A computer running Windows, macOS, or Linux
    • No prior command line experience required
    • Willingness to experiment (you won't break anything!)


    Time to complete: 40-50 minutes





    What is the Command Line?

    The command line (also called terminal, console, or shell) is a text-based interface for interacting with your computer. Instead of clicking buttons and icons, you type commands to tell your computer what to do.


    Real-World Analogy

    Think of your computer as a restaurant:


    Graphical Interface (GUI):
    • Like a picture menu where you point at what you want
    • Easy to use but limited options
    • Click, drag, drop


    Command Line:
    • Like telling the chef exactly what you want
    • More powerful and flexible
    • Type specific instructions


    Why It Looks Scary (But Isn't)

    When you open a terminal, you see something like:






    user@computer:~$







    This is just your computer saying "I'm ready for your command!" The blinking cursor is waiting for you to type.





    Why Developers Use the Command Line

    Speed and Efficiency

    With GUI (multiple clicks):

    1. Open File Explorer
    2. Navigate to Documents
    3. Create new folder
    4. Name the folder
    5. Open the folder
    6. Create a new file
    7. Name the file


    With command line (one command):






    mkdir -p Documents/my-project && cd Documents/my-project && touch index.html







    Done in 2 seconds! ⚡


    More Control and Power

    Some tasks are impossible or difficult with a graphical interface:
    • Batch renaming 100 files
    • Finding all files containing specific text
    • Automating repetitive tasks
    • Managing remote servers
    • Using advanced developer tools


    Industry Standard

    Professional developers use the command line because:
    • ✅ Many tools only have command line interfaces
    • ✅ Easier to share instructions (copy-paste commands)
    • ✅ Required for server management
    • ✅ Essential for version control (Git)
    • ✅ Faster for complex operations





    Opening Your Terminal

    Windows

    Option 1: Command Prompt
    • Press Windows key + R
    • Type cmd
    • Press Enter


    Option 2: PowerShell (Recommended)
    • Press Windows key + X
    • Select "Windows PowerShell"


    Option 3: Git Bash (Best for Development)
    • Install Git for Windows
    • Right-click anywhere → "Git Bash Here"


    macOS

    Option 1: Spotlight
    • Press Cmd + Space
    • Type "Terminal"
    • Press Enter


    Option 2: Finder
    • Go to Applications → Utilities → Terminal


    Linux

    Most distributions:
    • Press Ctrl + Alt + T


    Or:
    • Search for "Terminal" in your application menu





    Understanding the Command Line Interface

    Anatomy of the Command Prompt

    When you open your terminal, you'll see something like:






    john@macbook:~/Documents$







    Let's break this down:
    • john - Your username
    • @ - Separator
    • macbook - Your computer's name
    • : - Separator
    • ~/Documents - Current directory (where you are)
    • $ - Prompt symbol (means you're a regular user)
    • # - Root/admin prompt (if you see this, you have admin privileges)


    Understanding Paths

    Absolute path - Complete address from the root:






    /Users/john/Documents/projects/my-app







    Relative path - Address from your current location:






    projects/my-app # If you're already in Documents







    Special path symbols:
    • ~ - Your home directory
    • . - Current directory
    • .. - Parent directory (one level up)
    • / - Root directory (Windows uses \)





    Essential Navigation Commands

    pwd - Print Working Directory

    Shows where you currently are in the file system.






    pwd







    Output:






    /Users/john/Documents







    Use case: When you're lost and need to know your current location.





    ls - List Directory Contents

    Shows files and folders in your current location.


    Basic usage:






    ls







    Output:






    file1.txt file2.txt my-folder notes.md







    Common options:






    # List with details (permissions, size, date)
    ls -l

    # List all files including hidden ones (starting with .)
    ls -a

    # List in human-readable format with sizes
    ls -lh

    # Combine options
    ls -lah







    Example output with -lh:






    -rw-r--r-- 1 john staff 2.5K Oct 20 14:30 file1.txt
    drwxr-xr-x 3 john staff 96B Oct 20 14:30 my-folder







    Reading the output:
    • - = file, d = directory
    • rw-r--r-- = permissions
    • 2.5K = file size
    • Oct 20 14:30 = last modified date





    cd - Change Directory

    Moves you to a different folder.


    Go to a specific folder:






    cd Documents







    Go to home directory:






    cd ~
    # or just
    cd







    Go up one level:






    cd ..







    Go up two levels:






    cd ../..







    Go to previous directory:






    cd -







    Use absolute path:






    cd /Users/john/Documents/projects







    Pro tip: Use Tab to autocomplete folder names!






    cd Doc[press Tab]
    # Autocompletes to: cd Documents/










    Working with Files and Directories

    mkdir - Make Directory

    Creates a new folder.


    Create a single folder:






    mkdir my-project







    Create nested folders:






    mkdir -p projects/web-app/src
    # Creates: projects, then web-app inside it, then src inside that







    Create multiple folders at once:






    mkdir folder1 folder2 folder3










    touch - Create Empty File

    Creates a new file or updates the timestamp of an existing file.


    Create a single file:






    touch index.html







    Create multiple files:






    touch index.html style.css script.js







    Create file with path:






    touch src/components/Header.js







    Note: The folder must exist first, or use mkdir -p to create it.





    cp - Copy Files and Directories

    Copies files or folders from one location to another.


    Copy a file:






    cp file.txt file-backup.txt







    Copy a file to another directory:






    cp file.txt ../backup/







    Copy a directory and its contents:






    cp -r my-folder my-folder-backup
    # -r means "recursive" (includes everything inside)







    Copy multiple files:






    cp file1.txt file2.txt file3.txt destination-folder/










    mv - Move or Rename

    Moves files/folders or renames them.


    Rename a file:






    mv oldname.txt newname.txt







    Move a file to another directory:






    mv file.txt Documents/







    Move and rename simultaneously:






    mv file.txt Documents/renamed-file.txt







    Move a directory:






    mv my-folder Documents/projects/










    rm - Remove Files and Directories

    Deletes files or folders permanently.


    ⚠️ WARNING: There's no "Recycle Bin" - deleted files are gone forever!


    Delete a file:






    rm file.txt







    Delete multiple files:






    rm file1.txt file2.txt file3.txt







    Delete a directory and its contents:






    rm -r my-folder
    # -r means "recursive" (deletes everything inside)







    Force delete without confirmation:






    rm -rf my-folder
    # -f means "force" (no confirmation prompts)







    Safe deletion - ask for confirmation:






    rm -i file.txt
    # Prompts: "remove file.txt? (y/n)"







    Pro tip: Use rm -i when starting out to avoid accidents!





    find - Search for Files

    Finds files and directories based on criteria.


    Find by name:






    find . -name "*.txt"
    # Finds all .txt files in current directory and subdirectories







    Find by type:






    # Find only directories
    find . -type d

    # Find only files
    find . -type f







    Find and delete:






    find . -name "*.log" -delete
    # Finds and deletes all .log files







    Find by modification time:






    # Files modified in last 7 days
    find . -mtime -7

    # Files modified more than 30 days ago
    find . -mtime +30










    Viewing and Editing Files

    cat - Display File Contents

    Shows the entire contents of a file.






    cat file.txt







    Combine multiple files:






    cat file1.txt file2.txt







    Create a new file with content:






    cat > newfile.txt
    # Type your content, then press Ctrl+D to save










    less - View File Contents (Paginated)

    Better for large files - shows content page by page.






    less largefile.txt







    Navigation in less:
    • Space - Next page
    • b - Previous page
    • q - Quit
    • /search-term - Search forward
    • ?search-term - Search backward
    • G - Go to end
    • g - Go to beginning





    head - Show First Lines

    Shows the beginning of a file.






    # Show first 10 lines (default)
    head file.txt

    # Show first 5 lines
    head -n 5 file.txt










    tail - Show Last Lines

    Shows the end of a file.






    # Show last 10 lines (default)
    tail file.txt

    # Show last 20 lines
    tail -n 20 file.txt

    # Follow file updates in real-time (useful for logs)
    tail -f logfile.txt










    grep - Search Inside Files

    Finds text within files.


    Search for a word:






    grep "error" logfile.txt







    Search case-insensitively:






    grep -i "error" logfile.txt
    # Finds "error", "Error", "ERROR"







    Search in multiple files:






    grep "TODO" *.js
    # Searches all JavaScript files







    Search recursively in all subdirectories:






    grep -r "function" .







    Show line numbers:






    grep -n "error" file.txt







    Count matches:






    grep -c "error" file.txt










    nano / vim - Text Editors

    Command-line text editors for editing files.


    Using nano (easier for beginners):






    nano file.txt







    Nano shortcuts:
    • Ctrl + O - Save (Write Out)
    • Ctrl + X - Exit
    • Ctrl + K - Cut line
    • Ctrl + U - Paste
    • Ctrl + W - Search


    Using vim (more powerful but steeper learning curve):






    vim file.txt







    Vim basic commands:
    • i - Enter insert mode (start typing)
    • Esc - Exit insert mode
    • :w - Save
    • :q - Quit
    • :wq - Save and quit
    • :q! - Quit without saving


    Pro tip: Start with nano until you're comfortable, then learn vim if you want more power.





    File Permissions and Ownership

    Understanding Permissions

    When you run ls -l, you see something like:






    -rw-r--r-- 1 john staff 2048 Oct 20 14:30 file.txt







    Permission breakdown:






    -rw-r--r--
    │││││││││
    │││└─────── Others can read
    │││
    ││└──────── Group can read
    ││
    │└───────── Group cannot write

    └────────── Owner can read and write







    Permission types:
    • r - Read (4)
    • w - Write (2)
    • x - Execute (1)
    • - - No permission (0)


    chmod - Change Permissions

    Changes file/folder permissions.


    Using symbolic notation:






    # Give owner execute permission
    chmod u+x script.sh

    # Remove write permission from group
    chmod g-w file.txt

    # Give everyone read permission
    chmod a+r file.txt







    Using numeric notation:






    # 755 = Owner can read/write/execute, others can read/execute
    chmod 755 script.sh

    # 644 = Owner can read/write, others can only read
    chmod 644 file.txt

    # 777 = Everyone can do everything (rarely recommended!)
    chmod 777 file.txt







    Common permission patterns:
    • 755 - Executable files (scripts)
    • 644 - Regular files
    • 600 - Private files (only owner can read/write)





    chown - Change Owner

    Changes file/folder ownership.






    # Change owner
    sudo chown newowner file.txt

    # Change owner and group
    sudo chown newowner:newgroup file.txt

    # Change recursively
    sudo chown -R newowner folder/







    Note: Usually requires sudo (admin privileges).





    Using Git from the Command Line

    Git is the most popular version control system, and it's primarily used through the command line.


    Essential Git Commands

    Initialize a new repository:






    git init







    Check status:






    git status







    Add files to staging:






    # Add specific file
    git add file.txt

    # Add all files
    git add .

    # Add all JavaScript files
    git add *.js







    Commit changes:






    git commit -m "Your commit message here"







    View commit history:






    git log

    # Compact view
    git log --oneline

    # Show last 5 commits
    git log -n 5







    Create a branch:






    git branch feature-name







    Switch branches:






    git checkout feature-name

    # Or create and switch in one command
    git checkout -b feature-name







    Merge branches:






    # Switch to main branch first
    git checkout main

    # Merge feature branch
    git merge feature-name







    Clone a repository:






    git clone https://github.com/username/repo-name.git







    Pull latest changes:






    git pull origin main







    Push your changes:






    git push origin main







    View remote repositories:






    git remote -v







    Undo last commit (keep changes):






    git reset --soft HEAD~1







    Discard local changes:






    # Discard changes in specific file
    git checkout -- file.txt

    # Discard all changes
    git reset --hard










    Process Management

    ps - List Running Processes

    Shows currently running processes.






    # Show your processes
    ps

    # Show all processes with details
    ps aux

    # Find specific process
    ps aux | grep node










    top - Monitor System Resources

    Shows real-time system resource usage.






    top







    Navigation:
    • q - Quit
    • M - Sort by memory usage
    • P - Sort by CPU usage
    • k - Kill a process (enter PID when prompted)


    Alternative (better): Use htop if available






    htop










    kill - Terminate Processes

    Stops a running process.


    Kill by process ID:






    kill 1234
    # Where 1234 is the PID (Process ID)







    Force kill:






    kill -9 1234







    Kill by name:






    killall node
    # Kills all processes named "node"







    Find and kill a process:






    # Find the process
    ps aux | grep node

    # Kill it
    kill 1234










    Background and Foreground Processes

    Run command in background:






    node server.js &
    # The & runs it in background







    See background jobs:






    jobs







    Bring job to foreground:






    fg %1
    # Brings job #1 to foreground







    Send running process to background:
    • Press Ctrl + Z (pauses process)
    • Type bg (resumes in background)





    Command Line Shortcuts and Tips

    Keyboard Shortcuts

    Navigation:
    • Ctrl + A - Move to beginning of line
    • Ctrl + E - Move to end of line
    • Ctrl + Left/Right Arrow - Move word by word


    Editing:
    • Ctrl + U - Delete from cursor to beginning of line
    • Ctrl + K - Delete from cursor to end of line
    • Ctrl + W - Delete word before cursor


    History:
    • Up Arrow - Previous command
    • Down Arrow - Next command
    • Ctrl + R - Search command history (type to search, Enter to execute)
    • !! - Repeat last command
    • !$ - Use last argument from previous command


    Control:
    • Ctrl + C - Cancel current command
    • Ctrl + D - Exit terminal (or end input)
    • Ctrl + L - Clear screen (same as clear command)
    • Ctrl + Z - Pause current process





    Useful Command Combinations

    Pipes (|) - Send output of one command to another:






    # Count files in directory
    ls | wc -l

    # Search for error in all logs
    cat *.log | grep "error"

    # Sort and show unique values
    cat file.txt | sort | uniq







    Redirection (> and >>) - Save output to file:






    # Overwrite file with output
    ls > filelist.txt

    # Append output to file
    echo "New line" >> notes.txt

    # Redirect errors
    command 2> errors.txt

    # Redirect both output and errors
    command > output.txt 2>&1







    Command chaining:






    # Run second command only if first succeeds (&&)
    mkdir my-folder && cd my-folder

    # Run second command regardless (
    mkdir my-folder; ls

    # Run second command only if first fails (||)
    command || echo "Command failed"










    Tab Completion

    The Tab key is your best friend!






    # Type part of command or filename, press Tab
    cd Doc[Tab]
    # Autocompletes to: cd Documents/

    # Double Tab shows all possibilities
    git ch[Tab][Tab]
    # Shows: checkout, cherry, cherry-pick, etc.










    Command History

    View command history:






    history







    Execute command from history:






    !123
    # Executes command Minority AIDS Initiative: The Substa...nic Minorities from history







    Search history:






    # Press Ctrl+R, then type search term
    # Press Ctrl+R again to cycle through matches







    Clear history:






    history -c










    Aliases - Custom Shortcuts

    Create shortcuts for frequently used commands.


    Temporary alias (current session only):






    alias ll='ls -lah'
    # Now typing 'll' runs 'ls -lah'







    Permanent aliases (add to ~/.bashrc or ~/.zshrc):






    # Open config file
    nano ~/.bashrc # or ~/.zshrc for Zsh

    # Add aliases
    alias ll='ls -lah'
    alias gs='git status'
    alias gc='git commit'
    alias gp='git push'

    # Save and reload
    source ~/.bashrc







    Useful aliases:






    alias cls='clear'
    alias ..='cd ..'
    alias ...='cd ../..'
    alias projects='cd ~/Documents/projects'
    alias gs='git status'
    alias gaa='git add .'
    alias gcm='git commit -m'










    Common Mistakes to Avoid

    1. Deleting Without Checking

    ❌ Dangerous:






    rm -rf *
    # Deletes EVERYTHING in current directory







    ✅ Safe approach:






    # Always check where you are first
    pwd

    # List what will be deleted
    ls

    # Then delete specific things
    rm -i file.txt










    2. Forgetting Spaces in Commands

    ❌ Wrong:






    cd..
    # Error: command not found







    ✅ Correct:






    cd ..
    # Space is required










    3. Using Absolute Paths When Relative Would Work

    ❌ Unnecessary:






    cd /Users/john/Documents/projects/my-app







    ✅ Better:






    cd ~/Documents/projects/my-app
    # or if you're already in Documents:
    cd projects/my-app










    4. Not Using Tab Completion

    ❌ Slow:






    # Typing out full path manually
    cd /Users/john/Documents/projects/very-long-folder-name







    ✅ Fast:






    cd ~/Doc[Tab]/proj[Tab]/very[Tab]
    # Let Tab complete the rest!










    5. Ignoring Error Messages

    Always read error messages! They tell you exactly what went wrong.


    Common error:






    bash: command not found: node







    What it means: The command doesn't exist or isn't installed.


    Solution: Install the software or check spelling.





    6. Running Commands Without Understanding Them

    ❌ Dangerous:






    # Copying random commands from the internet
    sudo rm -rf /
    # THIS DELETES YOUR ENTIRE SYSTEM!







    ✅ Safe:
    • Read what the command does first
    • Use man command to read documentation
    • Test on unimportant files first
    • Ask if you're unsure





    Troubleshooting Guide

    Issue: "Permission Denied"

    Error:






    bash: ./script.sh: Permission denied







    Solution:






    # Make file executable
    chmod +x script.sh

    # Then run it
    ./script.sh










    Issue: "Command Not Found"

    Error:






    bash: node: command not found







    Possible causes:

    1. Software not installed
    2. Not in your PATH
    3. Typo in command name


    Solutions:






    # Check if it's installed
    which node

    # Install if missing (example for Node.js)
    # On macOS with Homebrew:
    brew install node

    # On Ubuntu/Debian:
    sudo apt install nodejs

    # Check spelling
    noed # Wrong!
    node # Correct!










    Issue: "No Such File or Directory"

    Error:






    bash: cd: Documents: No such file or directory







    Solutions:






    # Check current location
    pwd

    # List what's actually there
    ls

    # Check spelling (case-sensitive!)
    cd documents # Wrong if folder is "Documents"
    cd Documents # Correct










    Issue: Terminal Stuck or Frozen

    Solutions:


    If command is running:
    • Press Ctrl + C to cancel


    If terminal is unresponsive:
    • Press Ctrl + D to close
    • Open new terminal window


    If process is stuck in background:






    # Find the process
    ps aux | grep stuck-process

    # Kill it
    kill -9 PID










    Issue: Accidentally Ran Wrong Command

    Examples:


    Deleted wrong file:
    • If you used rm, file is gone (no undo)
    • Check backups or Time Machine (macOS)
    • Use git checkout file if it's in Git


    Changed wrong directory:






    cd -
    # Goes back to previous directory







    Want to undo last change:






    # For Git
    git checkout -- file.txt

    # For general file changes
    # No built-in undo - use backups!










    Issue: "Cannot Write to File"

    Error:






    bash: file.txt: Permission denied







    Solutions:






    # Check current permissions
    ls -l file.txt

    # Change permissions
    chmod u+w file.txt

    # Or use sudo (be careful!)
    sudo nano file.txt










    Practical Examples and Workflows

    Example 1: Starting a New Project





    # Create project folder and navigate into it
    mkdir my-new-project && cd my-new-project

    # Create project structure
    mkdir src css js img

    # Create initial files
    touch index.html src/app.js css/style.css

    # Initialize Git
    git init

    # Create README
    echo "# My New Project" > README.md

    # Check what you created
    ls -la










    Example 2: Finding and Cleaning Up Old Files





    # Find all log files
    find . -name "*.log"

    # Find log files older than 30 days
    find . -name "*.log" -mtime +30

    # Delete them (be careful!)
    find . -name "*.log" -mtime +30 -delete

    # Or move them to archive
    mkdir archive
    find . -name "*.log" -mtime +30 -exec mv {} archive/ \;










    Example 3: Working with Git





    # Clone a repository
    git clone https://github.com/username/repo.git

    # Navigate into it
    cd repo

    # Create new feature branch
    git checkout -b new-feature

    # Make changes, then check status
    git status

    # Add changes
    git add .

    # Commit
    git commit -m "Add new feature"

    # Push to remote
    git push origin new-feature

    # Switch back to main
    git checkout main

    # Pull latest changes
    git pull origin main










    Example 4: Batch File Operations





    # Rename all .txt files to .md
    for file in *.txt; do
    mv "$file" "${file%.txt}.md"
    done

    # Create 10 numbered files
    for i in {1..10}; do
    touch file$i.txt
    done

    # Find and replace text in all files
    find . -name "*.txt" -exec sed -i 's/oldtext/newtext/g' {} \;










    Next Steps

    Now that you understand command line basics, here's how to continue learning:


    Immediate Practice

    1. Complete all examples in this tutorial
    2. Navigate your file system using only the terminal
    3. Try creating a project structure from scratch
    4. Practice Git commands with a test repository
    5. Create useful aliases for your workflow


    Intermediate Topics

    • Shell scripting and automation
    • Advanced Git workflows (rebase, cherry-pick)
    • Package managers (Homebrew, apt, npm)
    • Environment variables and PATH
    • SSH and remote server access
    • Cron jobs for scheduling tasks


    Advanced Topics

    • Regular expressions with grep and sed
    • AWK for text processing
    • System administration commands
    • Network commands (ping, curl, wget)
    • Process management and system monitoring
    • Custom shell scripts and functions





    Key Takeaways

    The command line is faster and more powerful than graphical interfaces for many tasks


    Essential commands to remember:
    • cd - Navigate directories
    • ls - List contents
    • pwd - Show current location
    • mkdir - Create directories
    • touch - Create files
    • cp - Copy
    • mv - Move/rename
    • rm - Delete (careful!)
    • cat / less - View files
    • grep - Search in files


    Use Tab completion to save time and avoid typos


    Read error messages - they usually tell you exactly what's wrong


    Start with safe commands - use -i flag for confirmations when learning


    Practice regularly - command line proficiency comes with repetition


    Don't fear mistakes - you can't break anything as long as you avoid sudo rm -rf





    Additional Resources






    About the Author: Isabella Otoo is a full-stack developer and technical writer passionate about making complex technical concepts accessible to beginners.


    GitHub Repository: [https://github.com/Bellagirl-maker]


    Last Updated: October 2025




    More...
Working...