_______________________________________________________________________________
Module 4: Automation & Scripting
_______________________________________________________________________________
1. The Power of the "Pipe" (|)
In Linux, you can take the output of one command and "pipe" it directly into
another.
Example: ls /etc | grep ".conf"
Translation: "List everything in /etc, but only show me the lines that contain
'.conf'."
/-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=/
2. Redirecting Output (> and >>)
Sometimes you don't want to see the result on the screen; you want to save it
to a file.
ls > files.txt: Overwrites files.txt with the list of files.
ls >> files.txt: Appends the list to the end of the file without deleting what's
already there.
/-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=/
3. Bash Scripting 101
A script is just a text file full of commands.
Create a file: nano myscript.sh
The first line must be the Shebang: #!/bin/bash (This tells Linux to use the
Bash interpreter).
Write your commands.
Make it executable: chmod +x myscript.sh
Run it: ./myscript.sh
/-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=/
Module 4 Practical Challenge: The "Backup" Script
We are going to create a script that simulates a daily backup of your work.
Create the script: Create a file named daily_backup.sh.
Add the logic: Inside the file, write code that:
Prints "Starting backup..." to the screen.
Creates a directory named backups in your home folder (if it doesn't exist).
Copies all .txt files from your Linux_Basics folder into the backups folder.
Prints "Backup complete!" and the current date/time (use the date command).
Run it: Make it executable and run it. Verify the files are actually in the
backups folder.
/-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=/
The Final Step: The Cron Job
A Cron Job is a scheduled task.
Type crontab -e to open your personal schedule.
Each line follows this format: minute hour day month weekday command
Challenge: Add a line to your crontab that runs your daily_backup.sh every day
at 3:30 PM.
(Hint: It would look something like 30 15 * * * /home/youruser/daily_backup.sh)
_______________________________________________________________________________
(DIR) Module 1
(DIR) Module 2
(DIR) Module 3
(DIR) Module 4
(DIR) Back to Home