Bash Programming Intermediate to Advanced Teaching Plan

Lesson 1: File Handling and Navigation

1.1. List Files with Detailed Info

ls -l /path/to/directory

Shows file permissions, sizes, and timestamps.

1.2. Create Multiple Directories

mkdir -p dir1/dir2/dir3

Creates nested directories recursively.

1.3. Delete Non-Empty Directory

rm -rf /path/to/directory

Forcefully removes directories and contents.

1.4. Copy with File Permissions

cp -a source/ destination/

Copies files/directories preserving permissions.

1.5. Find Files by Name

find /search/path -name "*.log"

Searches for all .log files in the directory.

1.6. Archive and Compress

tar -czf archive.tar.gz /source/directory

Compresses a directory into a tarball.

1.7. Extract Tarball

tar -xzf archive.tar.gz -C /destination/

Extracts files to a specific directory.

1.8. Check Disk Usage

du -sh /path/to/directory

Shows the total size of a directory.

1.9. Create Symbolic Links

ln -s /original/file /link/location

Creates a symbolic link to a file.

1.10. Compare File Contents

diff file1.txt file2.txt

Highlights differences between two files.

Lesson 2: Text Processing

2.1. Search for Patterns

grep "error" /var/log/syslog

Finds lines containing "error" in the log file.

2.2. Replace Text in Files

sed -i 's/old/new/g' file.txt

Searches and replaces text in-place.

2.3. Extract Columns with AWK

awk '{print $1}' file.txt

Prints the first column of data.

2.4. Sort and Uniqify

sort file.txt | uniq -c

Counts occurrences of sorted lines.

2.5. Word Frequency

grep -oEw '\w+' file.txt | sort | uniq -c

Counts word frequencies in a file.

2.6. Regular Expressions with Grep

grep -E '[0-9]{3}-[0-9]{4}' file.txt

Matches patterns like 123-4567.

2.7. Process Output to File

cat file.txt | grep "keyword" > output.txt

Saves filtered output to a new file.

2.8. Combine Files

cat file1.txt file2.txt > combined.txt

Concatenates two files into one.

2.9. Extract Lines by Range

sed -n '5,10p' file.txt

Prints lines 5 to 10 of the file.

2.10. Reverse Lines

tac file.txt

Prints lines in reverse order.

Lesson 3: Process Management

3.1. List Running Processes

ps -aux

Shows all running processes with details.

3.2. Kill Process by Name

killall firefox

Terminates all instances of Firefox.

3.3. Background Process

sleep 100 &

Runs a command in the background.

3.4. Monitor Process Activity

top

Displays real-time process activity.

3.5. Run Command as Root

sudo apt update

Executes a command with elevated privileges.

3.6. Schedule a Job

cron -e

Edits cron jobs for automated tasks.

3.7. Check Process Status

systemctl status apache2

Checks the status of the Apache service.

3.8. List Open Ports

netstat -tuln

Displays active listening ports.

3.9. Trace System Calls

strace ls

Monitors system calls made by a command.

3.10. Attach to Running Process

gdb -p <PID>

Debugs a running process interactively.

Lesson 4: Networking and System Tools

4.1. Check IP Address

ifconfig

Displays network interface configurations.

4.2. Download a File

wget http://example.com/file.iso

Downloads a file from a URL.

4.3. Test Web Server

curl -I http://localhost:8080

Checks HTTP headers from a server.

4.4. SSH Connection

ssh user@192.168.1.100

Establishes an SSH session with a remote server.

4.5. Secure File Transfer

scp file.txt user@remote:/path/to/destination

Securely copies files over SSH.

4.6. DNS Lookup

dig example.com

Queries DNS records for a domain.

4.7. Ping Command

ping -c 4 8.8.8.8

Pings Google's DNS server 4 times.

4.8. Network Traffic Analysis

tcpdump -i eth0 port 80

Captures HTTP traffic on the eth0 interface.

4.9. Firewall Rules

iptables -A INPUT -p tcp --dport 22 -j ACCEPT

Configures firewall to allow SSH traffic.

4.10. HTTP Post Request

curl -X POST -d "name=value" http://api.example.com/endpoint

Sends a POST request with data.

Lesson 5: Advanced Scripting

5.1. Here Document

cat << EOF > file.txt This is a multi-line text. EOF

Creates a file with a block of text.

5.2. While Loop

i=1; while [ $i -le 5 ]; do echo $i; i=$((i+1)); done

Counts from 1 to 5 using a loop.

5.3. Function Definition

myfunc() { echo "Hello, $1!"; }; myfunc World

Defines and calls a function with arguments.

5.4. Conditional Execution

[[ $(date +%H) -gt 12 ]] && echo "Afternoon" || echo "Morning"

Prints greeting based on time of day.

5.5. Read User Input

read -p "Enter name: " name; echo "Hello $name"

Prompts the user for input.

5.6. Error Handling

set -e; false; echo "This won't print"

Exits immediately on command failure.

5.7. Parallel Processing

parallel echo {} ::: arg1 arg2 arg3

Runs commands in parallel.

5.8. Expect Script

#!/usr/bin/expect spawn ssh user@host expect "password:" { send "secret\r" } interact

Automates SSH login with password.

5.9. Regular Expressions in AWK

awk '/^[[:space:]]*$/ {next;1}' file.txt

Skips empty lines in a file.

5.10. Advanced Pipeline

ps aux | awk '{print $2}' | xargs -I {} kill {}

Kills all processes (use with caution).

Lesson 6: System Automation

6.1. Cron Job Example

0 2 * * * /backup/script.sh

Runs a script daily at 2 AM.

6.2. Log Rotation

find /var/log/ -name "*.log" -exec gzip {} \;

Compresses log files older than 7 days.

6.3. Backup Script

tar -czf /backup/$(date +%Y%m%d).tar.gz /source

Creates a timestamped backup.

6.4. Disk Cleanup

find /tmp -type f -mtime +7 -exec rm {} \;

Deletes files older than 7 days in /tmp.

6.5. Monitor Disk Space

df -h | awk '$NF=="/"{print $5}' | grep -q '^9' && echo "Critical!"

Alerts if root partition is over 90% full.

6.6. Automated Update

apt update && apt upgrade -y

Updates all packages automatically.

6.7. Service Management

systemctl restart nginx

Restarts the Nginx service.

6.8. User Management

useradd -m -s /bin/bash newuser

Creates a new user with a home directory.

6.9. Firewall Configuration

ufw allow 80/tcp

Opens port 80 for HTTP traffic.

6.10. Network Monitoring

iftop

Displays real-time network traffic.

Conclusion

This plan covers essential Bash commands for intermediate to advanced users. Practice these examples in a Linux environment to gain proficiency. Combine commands using pipes, redirects, and scripting to automate complex tasks.