Skip to content

Latest commit

 

History

History
344 lines (270 loc) · 5.69 KB

File metadata and controls

344 lines (270 loc) · 5.69 KB

🐧 Mastering Linux Command Line & Bash Scripting

The Linux command line and Bash scripting are incredibly versatile tools that can help you automate tasks, manage systems, and become more efficient in your daily workflows. Here’s a comprehensive guide packed with commands and examples to supercharge your Linux and Bash skills! 🚀


1. Essential Linux Commands 🛠️

Navigation

  • Change Directory:

    cd /path/to/directory

    Move to a specific directory.

  • List Files and Directories:

    ls -la

    List all files and directories, including hidden ones, with detailed information.

  • Show Current Directory:

    pwd

    Prints the current working directory.

  • Find Files:

    find /path -name "filename"

    Search for files or directories by name.


File Management

  • Create a File:

    touch newfile.txt

    Creates an empty file.

  • Create a Directory:

    mkdir new_directory

    Creates a new directory.

  • Copy Files and Directories:

    cp file.txt /destination/
    cp -r folder /destination/

    Copies files or directories.

  • Move or Rename Files:

    mv oldname.txt newname.txt
    mv file.txt /destination/

    Moves or renames files.

  • Delete Files and Directories:

    rm file.txt
    rm -r directory/

    Deletes files or directories.


Viewing Files

  • Display File Content:

    cat file.txt

    Prints the contents of a file.

  • Paginate Through Content:

    less file.txt

    View content one page at a time.

  • Show the First/Last Lines:

    head file.txt  # First 10 lines
    tail file.txt  # Last 10 lines

    Displays the first or last lines of a file.

  • View Line Numbers in a File:

    nl file.txt

    Shows line numbers with the content.


2. Bash Scripting Basics 💻

Writing Your First Script

  1. Create a new script file:

    nano script.sh
  2. Add the script content:

    #!/bin/bash
    echo "Hello, World!"
  3. Save and make it executable:

    chmod +x script.sh
  4. Run the script:

    ./script.sh

Variables, Conditions, and Loops

  • Using Variables:

    name="Linux"
    echo "Welcome to $name!"
  • Conditional Statements:

    if [ -f file.txt ]; then
        echo "File exists."
    else
        echo "File does not exist."
    fi
  • For Loop:

    for i in {1..5}; do
        echo "Iteration $i"
    done
  • While Loop:

    count=1
    while [ $count -le 5 ]; do
        echo "Count: $count"
        ((count++))
    done

3. Advanced Bash Techniques 🚀

String Manipulation

  • Substring Extraction:

    text="Hello, Linux!"
    echo ${text:7:5}  # Output: Linux
  • Replace Text:

    text="I love cats"
    echo ${text/cats/dogs}  # Output: I love dogs

Automating Tasks

  • Backup a Directory:

    #!/bin/bash
    tar -czf backup-$(date +%Y-%m-%d).tar.gz /path/to/directory
    echo "Backup created!"
  • Find and Delete Old Files:

    find /path/to/files -type f -mtime +30 -exec rm {} \;

    Deletes files older than 30 days.


Debugging Scripts

  • Run with Debugging:
    bash -x script.sh
    Shows each command before it executes.

4. Process and System Management 🧠

Process Management

  • View Running Processes:

    ps aux
  • Kill a Process:

    kill -9 <PID>
  • Monitor System Usage:

    top
    htop  # Enhanced interactive view

Disk and Memory Management

  • Check Disk Space:

    df -h
  • Check Free Memory:

    free -h

5. Networking Commands 🌐

Basic Networking

  • Test Connectivity:

    ping google.com
  • Download a File:

    wget https://example.com/file.zip
  • Check Open Ports:

    netstat -tuln

6. Text Processing Power 📄

Search with grep

  • Find a Keyword:

    grep "error" logfile.txt
  • Search Recursively:

    grep -r "TODO" .

Edit with sed

  • Replace Text Inline:

    sed 's/old/new/' file.txt
  • Delete Lines Containing a Pattern:

    sed '/pattern/d' file.txt

Extract with awk

  • Print Specific Columns:

    awk '{print $1, $3}' file.txt
  • Filter Rows:

    awk '$3 > 50' data.txt

7. Scheduling and Automation 🕒

Recurring Tasks with cron

  • Edit Cron Jobs:

    crontab -e
  • Run a Script Daily at Midnight:

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

One-Time Tasks with at

  • Schedule a Task:
    at 4:00 PM

8. Security Best Practices 🔒

  • Encrypt Files:

    gpg -c file.txt
  • Check File Integrity:

    sha256sum file.txt
  • Set File Permissions:

    chmod 600 file.txt

Next Steps

  1. Dive deeper into tools like awk, sed, and grep.
  2. Automate server setups with Bash and tools like Ansible.
  3. Explore system tuning using sysctl.
  4. Learn how to troubleshoot and debug complex Bash scripts.

🐧 With these skills, you’re ready to conquer Linux environments and automate like a pro! 🚀