Skip to content

Latest commit

 

History

History
193 lines (160 loc) · 5.28 KB

10_linux_and_shell.md

File metadata and controls

193 lines (160 loc) · 5.28 KB

Day 6 - Linux & Shell Scripting

DevOps_Diary

#devopscourse

Agenda

  • Introduction to Linux Operating System
  • Basic Linux Commands
  • Introduction to Shell Scripting
  • Writing Your First Shell Script
  • Best Practices for Shell Scripting

Key Points

1. Introduction to Linux Operating System

  • Linux is a robust open-source operating system commonly used in server environments.
  • Key characteristics include security, stability, and a wide range of distributions (e.g., Ubuntu, CentOS, Debian).
  • Understanding Linux fundamentals is crucial for DevOps professionals as many cloud servers run on Linux distributions.

2. Basic Linux Commands

Learn essential Linux commands for effective command-line navigation and management. Common Daily DevOps Commands include:

  • ls: List files and directories in the current directory.

    • Common options:
      • -l: Display detailed information.
      • -a: List hidden files.
    • Example:
      ls -la
  • cd: Change directories.

    • Example:
      cd /path/to/directory
  • pwd: Print the current working directory.

    • Example:
      pwd
  • mkdir: Create directories.

    • Example:
      mkdir my_directory
  • rm: Remove files or directories.

    • Common options:
      • -r: Remove directories recursively.
      • -f: Force removal without confirmation.
    • Example:
      rm -rf directory_to_remove
  • cp: Copy files and directories.

    • Example:
      cp file.txt /destination/
  • mv: Move or rename files and directories.

    • Example:
      mv old_name.txt new_name.txt
  • ps: List running processes.

    • Common options:
      • -e: Display all processes.
      • -f: Display detailed information.
    • Example:
      ps aux
  • top: Real-time system monitor.

    • Example:
      top
  • grep: Search text using patterns.

    • Example:
      grep "keyword" file.txt
  • find: Search for files and directories.

    • Example:
      find /path/to/search -name "file_pattern"
  • tar: Archive files.

    • Example:
      tar -czvf archive.tar.gz directory_to_archive
  • gzip and gunzip: Compress and decompress files.

    • Example:
      gzip file.txt
  • ssh: Securely connect to remote servers.

    • Example:
      ssh user@remote_server
  • scp: Securely copy files between local and remote servers.

    • Example:
      scp local_file.txt user@remote_server:/path/to/destination/
  • chmod: Change file permissions.

    • Example:
      chmod +x script.sh
  • chown: Change file ownership.

    • Example:
      chown user:group file.txt
  • curl: Transfer data with URLs.

    • Example:
      curl https://example.com
  • wget: Download files from the internet.

    • Example:
      wget https://example.com/file.txt
  • df: Display disk space usage.

    • Example:
      df -h

These Linux commands are essential for managing servers, troubleshooting issues, and automating tasks in a DevOps environment. Familiarity with these commands is crucial for success in DevOps roles.

  • Practice these commands to become familiar with the Linux file system.

3. Introduction to Shell Scripting

  • Shell scripting involves writing and executing scripts in a shell (command-line environment) to automate tasks.
  • Common shells include Bash (Bourne Again SHell), Zsh, and Fish.
  • Shells interpret and execute commands, making them powerful tools for automation.

4. Writing Your First Shell Script

  • Start by creating a simple "Hello World" script in Bash or your preferred shell.
  • Open a text editor and write your script, e.g.:
    #!/bin/bash
    echo "Hello, DevOps!"
  • Make the script executable using the chmod command:
    chmod +x scriptname.sh
  • Execute the script:
    ./scriptname.sh
  1. Best Practices for Shell Scripting
    • Use comments extensively to document your scripts for better readability.
    • Ensure your scripts have proper error handling to gracefully handle unexpected situations.
    • Keep scripts modular and reusable by defining functions.
    • Follow security best practices to prevent unauthorized access to your scripts.
    • Avoid hardcoding sensitive information like passwords or API keys in your scripts.

Homework

  • Practice basic Linux commands by navigating through your system, creating, moving, and deleting files and directories.
  • Write a simple shell script that performs a task of your choice and share it in the discussions for feedback.

Additional Resources

Understanding Linux and shell scripting is foundational for DevOps practitioners. It empowers you to automate tasks and manage server environments efficiently.

Click here to continue to Day 7