Advertisement

HexaGuard: Mastering the Art of Digital Shadows

Command-Line Basics: Bash, CMD, PowerShell (ls, grep, awk, sed, cron jobs)

 To master the basics of the command line, you'll want to familiarize yourself with three popular shells:

  1. Bash (Bourne Again Shell) – Common in Linux and macOS.
  2. CMD (Command Prompt) – Windows command-line interpreter.
  3. PowerShell – A more advanced command-line interface for Windows, with scripting capabilities.

Here’s a breakdown of essential commands for each of these:


1. Bash Basics (Linux/macOS)

  • Navigating Directories:

    • ls: List directory contents.
      bash

      ls # Lists files in current directory ls -l # Lists files with detailed info (permissions, owner, etc.) ls -a # List all files, including hidden files
    • cd <directory>: Change directory.
      bash

      cd /path/to/folder # Navigate to a specific directory cd ~ # Go to home directory cd .. # Move one directory up
  • Searching Files:

    • grep <pattern> <file>: Search for a pattern inside a file.
      bash

      grep "error" log.txt # Finds occurrences of "error" in log.txt
    • find: Search for files in a directory hierarchy.
      bash

      find . -name "test.txt" # Finds test.txt in current directory and subdirectories
  • Text Processing:

    • awk: Process text files (often used for data extraction).
      bash

      awk '{print $1}' file.txt # Prints the first column of a file
    • sed: Stream editor for modifying files.
      bash

      sed 's/old/new/g' file.txt # Replaces "old" with "new" globally in the file
  • Automation (Cron Jobs):

    • Cron Jobs: Automate tasks on Linux/macOS (for scheduled tasks).
      • To list cron jobs:
        bash

        crontab -l
      • Edit cron jobs:
        bash

        crontab -e
      • A cron job example to run a script every day at midnight:
        bash

        0 0 * * * /path/to/script.sh

2. CMD (Windows)

  • Navigating Directories:

    • dir: List files in the current directory.

      cmd

      dir # Lists files in the current directory dir /a # Shows all files, including hidden
    • cd <directory>: Change directory.

      cmd

      cd C:\path\to\folder # Navigate to a specific directory cd .. # Move one directory up
  • Searching Files:

    • findstr <pattern> <file>: Search for a pattern inside a file.
      cmd

      findstr "error" log.txt # Finds occurrences of "error" in log.txt
  • Text Processing:

    • For basic text manipulation, you might use batch scripts with commands like for, set, and echo, but CMD lacks tools like awk and sed by default.
  • Automation (Task Scheduler):

    • For scheduled tasks in Windows, use Task Scheduler instead of cron:
      • Open Task Scheduler > Create Task > Set triggers and actions.

3. PowerShell Basics (Windows)

  • Navigating Directories:

    • Get-ChildItem (alias ls): List files in the current directory.

      powershell

      Get-ChildItem # Lists files in the current directory ls # Alias for Get-ChildItem
    • Set-Location <path> (alias cd): Change directory.

      powershell

      Set-Location C:\path\to\folder # Navigate to a specific directory cd C:\path\to\folder # Alias for Set-Location
  • Searching Files:

    • Select-String <pattern> <file>: Search for a pattern inside a file.
      powershell

      Select-String "error" log.txt # Finds occurrences of "error" in log.txt
  • Text Processing:

    • PowerShell has more advanced text manipulation features than CMD.
    • Select-Object, Where-Object, and ForEach-Object are commonly used for filtering and processing text.
      powershell

      Get-Content file.txt | Select-String "error" # Filters out lines with "error"
  • Automation (Task Scheduler):

    • Similar to CMD, PowerShell can create and manage tasks through Task Scheduler.
      • Use New-ScheduledTask cmdlets to automate tasks.
      powershell

      New-ScheduledTaskTrigger -Daily -At "6:00AM" -Action { PowerShell -Command "C:\path\to\script.ps1" }

Tips for Mastery:

  1. Practice Regularly: Start using the terminal every day. You can practice by doing basic file operations, text processing, and automation tasks.
  2. Learn Scripting: Both PowerShell and Bash support scripting. Learn how to write simple scripts to automate repetitive tasks.
  3. Read Documentation: Use man <command> in Bash to read the manual pages for commands (e.g., man ls), or Get-Help <command> in PowerShell (e.g., Get-Help Get-ChildItem).
  4. Experiment with Real Projects: Try creating cron jobs or task scheduler jobs to run scripts that monitor system health or perform backups.

Post a Comment

0 Comments