Linux Basic Commands

Master essential Linux commands with visual diagrams, data flow charts, and real-world examples for DevOps and system administration

What are Linux Commands?

Linux commands are text-based instructions that interact with the operating system through a shell, enabling file management, process control, and system administration.

Command Structure Architecture

graph LR
    A[Command] --> B[Options/Flags]
    B --> C[Arguments]
    
    A --> A1["ls"]
    B --> B1["-la"]
    C --> C1["/home/user"]
    
    D[Full Command] --> E["ls -la /home/user"]
    
    style A fill:#2196F3
    style B fill:#4CAF50
    style C fill:#FF9800
    style D fill:#9C27B0

Key Points:

  • Command: The executable program name (ls, cd, grep)
  • Options: Modify behavior using single dash (-l) or double dash (--long)
  • Arguments: Input data like file paths, text strings, or parameters
  • Flexibility: Commands can have multiple options and arguments combined

Linux Filesystem Hierarchy

graph TB
    A[Root /] --> B[bin - Essential Binaries]
    A --> C[etc - Configuration]
    A --> D[home - User Directories]
    A --> E[var - Variable Data]
    A --> F[usr - User Programs]
    A --> G[tmp - Temporary Files]
    
    D --> D1[username]
    E --> E1[log - System Logs]
    F --> F1[bin - User Binaries]
    F --> F2[lib - Libraries]
    
    style A fill:#2196F3
    style B fill:#4CAF50
    style C fill:#FF9800
    style D fill:#9C27B0
    style E fill:#F44336
    style F fill:#00BCD4

Key Points:

  • Root (/): Top-level directory containing all system files
  • bin: Essential command binaries like ls, cp, mv accessible to all users
  • etc: System-wide configuration files and scripts
  • home: Individual user directories for personal files and settings
  • var: Variable data including logs, databases, and temporary files
  • usr: Secondary hierarchy for user programs and read-only data

Command Execution Flow

sequenceDiagram
    participant User
    participant Shell
    participant PATH
    participant Kernel
    participant FileSystem
    
    User->>Shell: Enter command: ls -la
    Shell->>Shell: Parse command
    Shell->>PATH: Search for executable
    PATH->>Shell: Found /bin/ls
    Shell->>Kernel: Fork new process
    Kernel->>Kernel: Create child process
    Kernel->>FileSystem: Execute ls binary
    FileSystem->>Kernel: Read directory
    Kernel->>Shell: Return output
    Shell->>User: Display results

Key Points:

  • Parsing: Shell interprets command syntax and expands wildcards/variables
  • PATH Resolution: System searches directories in PATH environment variable
  • Process Creation: Kernel forks child process to execute command
  • System Calls: Program interacts with kernel for filesystem operations
  • Output Streams: Results sent to STDOUT, errors to STDERR with exit codes

File Operations Data Flow

flowchart TB
    A[User Command] --> B{Operation Type}
    
    B -->|Read| C[cat/less/head]
    B -->|Write| D[echo/tee]
    B -->|Copy| E[cp]
    B -->|Move| F[mv]
    B -->|Delete| G[rm]
    
    C --> H[Read from Disk]
    D --> I[Write to Disk]
    E --> J[Copy Data]
    F --> K[Update Inode]
    G --> L[Remove Inode]
    
    H --> M[Display Output]
    I --> M
    J --> M
    K --> M
    L --> M
    
    style A fill:#2196F3
    style B fill:#4CAF50
    style C fill:#FF9800
    style D fill:#9C27B0
    style E fill:#F44336
    style F fill:#00BCD4
    style G fill:#FFEB3B

Key Points:

  • Read Operations: Commands like cat, less, head read file contents into memory
  • Write Operations: echo, tee write data to files, creating or appending
  • Copy: cp duplicates file data and creates new inode entry
  • Move: mv updates directory entry without copying data (same filesystem)
  • Delete: rm removes inode reference, freeing space when no links remain

Process Management Architecture

graph TB
    A[Process Management] --> B[ps - List Processes]
    A --> C[top - Monitor Real-time]
    A --> D[kill - Terminate]
    A --> E[bg/fg - Job Control]
    
    B --> B1[PID - Process ID]
    B --> B2[PPID - Parent ID]
    B --> B3[CPU/Memory Usage]
    
    C --> C1[Live Updates]
    C --> C2[Sort by Resource]
    
    D --> D1[SIGTERM - Graceful]
    D --> D2[SIGKILL - Force]
    
    E --> E1[Background Jobs]
    E --> E2[Foreground Jobs]
    
    style A fill:#2196F3
    style B fill:#4CAF50
    style C fill:#FF9800
    style D fill:#F44336
    style E fill:#9C27B0

Key Points:

  • ps Command: Displays snapshot of current processes with PID and resource usage
  • top/htop: Real-time monitoring with interactive sorting and filtering
  • Signals: SIGTERM (15) allows cleanup, SIGKILL (9) forces immediate termination
  • Job Control: bg sends jobs to background, fg brings them to foreground

Permission System

graph LR
    A[File Permissions] --> B[Owner - rwx]
    A --> C[Group - rwx]
    A --> D[Others - rwx]
    
    B --> E[Read - 4]
    B --> F[Write - 2]
    B --> G[Execute - 1]
    
    H[chmod 755] --> I[Owner: 7 rwx]
    H --> J[Group: 5 r-x]
    H --> K[Others: 5 r-x]
    
    style A fill:#2196F3
    style B fill:#4CAF50
    style C fill:#FF9800
    style D fill:#9C27B0
    style H fill:#F44336

Key Points:

  • Three Permission Sets: Owner, group, and others each have separate permissions
  • Permission Types: Read (4), write (2), execute (1) combine to form octal values
  • chmod Command: Changes permissions using numeric (755) or symbolic (u+x) notation
  • Security: Proper permissions prevent unauthorized access and modifications

Pipeline and Redirection Flow

flowchart LR
    A[Command 1] -->|STDOUT| B[Pipe Symbol]
    B -->|STDIN| C[Command 2]
    C -->|STDOUT| D[Pipe Symbol]
    D -->|STDIN| E[Command 3]
    
    F[Input File] -->|Redirect In| G[Command]
    G -->|Redirect Out| H[Output File]
    G -->|Error Out| I[Error File]
    G -->|Append Out| J[Append File]
    
    style A fill:#2196F3
    style C fill:#4CAF50
    style E fill:#FF9800
    style G fill:#9C27B0

Key Points:

  • Pipes (|): Connect STDOUT of one command to STDIN of next command
  • Redirection (>): Overwrites file with command output
  • Append (>>): Adds output to end of existing file
  • Error Redirection (2>): Captures error messages separately from standard output

Search and Filter Workflow

flowchart TB
    A[Search Operations] --> B[find - File Search]
    A --> C[grep - Text Search]
    A --> D[locate - Fast Search]
    
    B --> B1[By Name: -name]
    B --> B2[By Type: -type]
    B --> B3[By Time: -mtime]
    B --> B4[Execute: -exec]
    
    C --> C1[Pattern Match]
    C --> C2[Recursive: -r]
    C --> C3[Case Insensitive: -i]
    C --> C4[Count: -c]
    
    D --> D1[Database Index]
    D --> D2[Fast Lookup]
    
    style A fill:#2196F3
    style B fill:#4CAF50
    style C fill:#FF9800
    style D fill:#9C27B0

Key Points:

  • find: Searches filesystem in real-time with extensive filtering options
  • grep: Pattern matching in file contents using regular expressions
  • locate: Uses pre-built database for instant filename searches
  • Combination: Commands often piped together for powerful search capabilities

System Monitoring Dashboard

graph TB
    A[System Monitoring] --> B[CPU - top/htop]
    A --> C[Memory - free]
    A --> D[Disk - df/du]
    A --> E[Network - netstat]
    A --> F[Logs - tail/journalctl]
    
    B --> B1[Load Average]
    B --> B2[Process List]
    
    C --> C1[Used/Free RAM]
    C --> C2[Swap Usage]
    
    D --> D1[Filesystem Usage]
    D --> D2[Directory Size]
    
    E --> E1[Active Connections]
    E --> E2[Listening Ports]
    
    F --> F1[Real-time Logs]
    F --> F2[System Events]
    
    style A fill:#2196F3
    style B fill:#4CAF50
    style C fill:#FF9800
    style D fill:#9C27B0
    style E fill:#F44336
    style F fill:#00BCD4

Key Points:

  • CPU Monitoring: top shows real-time CPU usage, load averages, and process statistics
  • Memory Analysis: free displays RAM and swap usage in human-readable format
  • Disk Space: df shows filesystem usage, du calculates directory sizes
  • Network Status: netstat displays active connections and listening services

DevOps Automation Flow

sequenceDiagram
    participant Dev as Developer
    participant Git as Git Repo
    participant CI as CI/CD Pipeline
    participant Server as Production Server
    
    Dev->>Git: git push code
    Git->>CI: Trigger build
    CI->>CI: Run tests
    CI->>CI: Build artifacts
    CI->>Server: ssh deploy script
    Server->>Server: systemctl restart service
    Server->>CI: Deployment status
    CI->>Dev: Notification

Key Points:

  • Version Control: Git commands manage code versions and collaboration
  • Automation: Shell scripts automate build, test, and deployment processes
  • Remote Execution: SSH enables secure remote command execution
  • Service Management: systemctl controls service lifecycle on Linux systems

Common Command Categories

  • pwd - Print working directory
  • cd - Change directory
  • ls - List directory contents

File Management

  • cp - Copy files/directories
  • mv - Move or rename
  • rm - Remove files
  • mkdir - Create directories
  • touch - Create empty files

Text Processing

  • cat - Display file contents
  • grep - Search patterns
  • sed - Stream editor
  • awk - Text processing

System Information

  • uname - System information
  • df - Disk space
  • free - Memory usage
  • uptime - System uptime