25 Bash Scripts For DevOps Engineers & SysAdmins For Daily Use
Bash scripting is a powerful and versatile tool for automating repetitive tasks, managing system resources, and simplifying complex workflows. A Bash script is essentially a text file containing a sequence of commands that the shell interprets and executes.
Bash scripts are useful because they allow you to perform tasks more efficiently by automating repetitive actions. They are widely used in system administration, software development, and DevOps to streamline processes, manage servers, and handle large datasets effectively.
To create a Bash script, you can follow these steps:
- Open a text editor: Use any text editor, such as
nano
,vim
, orgedit
. - Start with a shebang: Include
#!/bin/bash
at the top of your script to specify the shell interpreter. - Write your commands: Add the commands you want to automate.
- Save the file: Save the script with a
.sh
extension (e.g.,myscript.sh
). - Make it executable: Run
chmod +x myscript.sh
to give execution permissions. - Run the script: Execute it with
./myscript.sh
.
Here’s a list of 25 Bash scripts that you can use for everyday tasks, complete with descriptions and examples
1. Hello World Script
- Prints “Hello, World!” to the terminal.
#!/bin/bash
echo "Hello, World!"
2. System Info Script
- Displays system information like hostname, kernel version, and uptime.
#!/bin/bash
echo "Hostname: $(hostname)"
echo "Kernel Version: $(uname -r)"
echo "Uptime: $(uptime -p)"
3. Backup Script
- Creates a backup of a specified directory.
#!/bin/bash
src="/path/to/source"
dest="/path/to/backup"
tar -czvf "$dest/backup_$(date +%Y%m%d).tar.gz" "$src"
4. Disk Usage Script
- Checks disk usage and alerts if it exceeds a threshold.
#!/bin/bash
threshold=80
usage=$(df / | tail -1 | awk '{print $5}' | sed 's/%//')
if [ "$usage" -gt "$threshold" ]; then
echo "Warning: Disk usage is above $threshold%"
fi
5. Find and Replace Script
- Finds and replaces text in files.
#!/bin/bash
find . -type f -name "*.txt" -exec sed -i 's/old_text/new_text/g' {} +
6. Process Monitor Script
- Checks if a process is running and restarts it if not.
#!/bin/bash
process="nginx"
if ! pgrep -x "$process" > /dev/null; then
echo "$process is not running. Restarting..."
systemctl start "$process"
fi
7. Password Generator Script
- Generates a random password of specified length.
#!/bin/bash
length=12
echo "$(tr -dc A-Za-z0-9 </dev/urandom | head -c $length)"
8. Cleanup Script
- Deletes files older than a certain number of days.
#!/bin/bash
find /path/to/directory -type f -mtime +30 -delete
9. Weather Checker Script
- Fetches the current weather using an API.
#!/bin/bash
api_key="your_api_key"
location="New York"
curl -s "http://api.openweathermap.org/data/2.5/weather?q=$location&appid=$api_key" | jq '.weather[0].description'
10. Log File Analyzer Script
- Extracts specific information from log files.
#!/bin/bash
grep "ERROR" /var/log/syslog | tail -10
11. File Organizer Script
- Moves files into directories based on extensions.
#!/bin/bash
for file in *; do
ext="${file##*.}"
mkdir -p "$ext"
mv "$file" "$ext/"
done
12. Reminder Script
- Sends a notification or reminder at a specific time.
#!/bin/bash
echo "Don't forget to take a break!" | at 14:00
13. Internet Speed Test Script
- Measures internet speed using
speedtest-cli
.
#!/bin/bash
speedtest-cli
14. Directory Size Script
- Displays the size of a directory.
#!/bin/bash
du -sh /path/to/directory
15. Auto-Updater Script
- Updates system packages.
#!/bin/bash
sudo apt update && sudo apt upgrade -y
16. Screenshot Taker Script
- Takes a screenshot and saves it.
#!/bin/bash
scrot ~/Pictures/screenshot_$(date +%Y%m%d_%H%M%S).png
17. Port Checker Script
- Checks if a port is open.
#!/bin/bash
port=80
if nc -zv localhost $port; then
echo "Port $port is open."
else
echo "Port $port is closed."
fi
18. Todo List Script
- Manages a simple todo list.
#!/bin/bash
echo "1. Add a task"
echo "2. View tasks"
read choice
if [ "$choice" -eq 1 ]; then
echo "Enter task:" >> todo.txt
elif [ "$choice" -eq 2 ]; then
cat todo.txt
fi
19. Archive Extractor Script
- Extracts files from tar.gz archives.
#!/bin/bash
tar -xzvf archive.tar.gz
20. Service Status Script
- Checks the status of a service.
#!/bin/bash
service="nginx"
systemctl status "$service"
21. Battery Status Script
- Displays the battery status of a laptop.
#!/bin/bash
acpi -b
22. System Resource Monitor Script
- Displays CPU and memory usage.
#!/bin/bash
echo "CPU Usage: $(top -bn1 | grep "Cpu(s)" | awk '{print $2 + $4}')%"
echo "Memory Usage: $(free -m | awk 'NR==2{printf "%.2f", $3*100/$2 }')%"
23. IP Address Checker Script
- Shows the public and private IP addresses.
#!/bin/bash
echo "Public IP: $(curl -s ifconfig.me)"
echo "Private IP: $(hostname -I)"
24. File Downloader Script
- Downloads files using
wget
.
#!/bin/bash
url="https://example.com/file.zip"
wget "$url"
25. Schedule Cron Job Script
- Automates scheduling of a cron job.
#!/bin/bash
echo "0 2 * * * /path/to/script.sh" | crontab -
Conclusion
The 25 Bash scripts outlined above demonstrate the immense flexibility and practicality of Bash scripting for everyday tasks. From automating backups and monitoring system resources to generating passwords and managing files, these scripts can significantly enhance productivity and save time.
By mastering Bash scripting, you gain the ability to automate complex workflows, reduce human error, and customize solutions tailored to your specific needs. Whether you’re a beginner or an experienced user, these scripts serve as a foundation for building more advanced and robust solutions. Start experimenting with these examples, and you’ll quickly discover the endless possibilities that Bash scripting offers.