Linux Shell Scripts
File Backup Script
Automating file backups is crucial for maintaining data integrity. Here’s a simple script to perform both
full and incremental backups:
#!/bin/bash
src_dir="/path/to/source"
backup_dir="/path/to/backup"
timestamp=$(date +%Y%m%d_%H%M%S)
backup_file="$backup_dir/backup_full_$[Link]"
tar -czf $backup_file -C $src_dir
echo "Full backup completed: $backup_file"
Incremental Backup Script:
#!/bin/bash
src_dir="/path/to/source"
backup_dir="/path/to/backup"
timestamp=$(date +%Y%m%d_%H%M%S)
backup_file="$backup_dir/backup_incremental_$[Link]"
last_backup="$backup_dir/last_backup.txt"
if [ -f "$last_backup" ]; then
find $src_dir -type f -newer $last_backup > changed_files.txt
else
find $src_dir -type f > changed_files.txt
fi
tar -czf $backup_file -T changed_files.txt
touch $last_backup
echo "Incremental backup completed: $backup_file"
System Monitoring Script
System health checks are vital for maintaining operational efficiency. This script monitors CPU and
memory usage:
#!/bin/bash
cpu_usage=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 -$1}')
memory_usage=$(free | grep Mem | awk '{print $3/$2 * 100.0}')
echo "CPU Usage: $cpu_usage%"
echo "MemoryUsage: $memory_usage%"
User Account Management Script
Managing user accounts is a common administrative task. This script adds or removes users:
#!/bin/bash
manage_user() {
username=$1
action=$2
if [ "$action" == "add" ]; then
sudo useradd -m $username
echo "User $username added."
elif [ "$action" == "remove" ]; then
sudo userdel -r $username
if [ -z "$1" ] || [ -z "$2" ]; then
echo"Usage: $0 <username> <add|remove>"
else
manage_user $1 $2
fi
Log Analyzer Script
Analyzing logs is essential for troubleshooting. This script extracts critical errors from a log file:
#!/bin/bash
log_file="/path/to/[Link]"
output_file="critical_errors.log"
grep -i "error" $log_file > $output_file
echo "Critical errors saved to $output_file"
Rotating Logs:
#!/bin/bash
log_dir="/path/to/logs"
max_size=1000000 # 1 MB
for file in $log_dir/*.log; do
log_size=$(stat -c %s "$file")
if [ $log_size -gt $max_size ]; then
mv "$file" "$[Link]"
echo "Log file $file rotated."
fi
done
Password Generator Script
Creating strong passwords is essential for security. This script generates random passwords:
#!/bin/bash
length=${1:-12}
password=$(tr -dc 'A-Za-z0-9!@#$%^&*()_+=' < /dev/urandom | head -c $length)
echo "Generated Password: $password"
For available users
#!/bin/bash
# Prompt for the username
echo "Available users:"
awk -F: '$3 >= 1000 {print $1}' /etc/passwd # List all non-system users
read -p "Enter the username for which you want to set the password: " username
# Check if the user exists
if id "$username" &>/dev/null; then
# Set the password length from the first argument or default to 12
length=${1:-12}
# Generate the password
password=$(tr -dc 'A-Za-z0-9!@#$%^&*()_+=' < /dev/urandom | head -c $length)
# Set the password
echo "$username:$password" | sudo chpasswd
echo "Password set for $username: $password"
else
echo "User '$username' does not exist."
exit 1
fi
Automated Software Installation Script
This script simplifies software installation, especially on new systems:
#!/bin/bash
software_list=(git vim curl wget)
for software in "${software_list[@]}"; do
if ! dpkg -l | grep -q $software; then
sudo apt-get install -y $software
echo "$software installed."
else
echo "$software is already installed."
fi
done
Explanation: This script checks if a package is already installed before attempting installation. Modify
the software_list array for your requirements.
Network Connectivity Checker Script
This script checks network connectivity:
#!/bin/bash
host="[Link]"
if ping -c 1 $host > /dev/null; then
echo "$host is reachable."
else
echo "$host is unreachable."
fi
File Encryption/Decryption Script
Secure your files with encryption and decryption:
#!/bin/bash
action=$1
file=$2
output=$3
if [ "$action" == "encrypt" ]; then
openssl enc -aes-256-cbc -salt -in $file -out $output
echo "File encrypted: $output"
elif [ "$action" == "decrypt" ]; then
openssl enc -aes-256-cbc -d -in $file -out $output
echo "File decrypted: $output"
else
echo "Usage: $0 <encrypt|decrypt> <file> <output>"
fi
Data Cleanup Script
This script cleans up temporary files and unnecessary logs:
#!/bin/bash
cleanup_dirs=("/tmp" "/var/tmp")
for dir in "${cleanup_dirs[@]}"; do
find $dir -type f -atime +7 -exec rm -f {} \;
echo "Cleaned up $dir."
Done
Website Uptime Checker Script
Monitor website uptime with this script:
#!/bin/bash
url="[Link]
if curl -s --head $url | grep "200 OK" > /dev/null; then
echo "$url is up."
else
echo "$url is down."
fi
System Information Script
Retrieve detailed system information:
#!/bin/bash
echo "System Information:"
uname -a
echo "CPU Information:"
lscpu
echo "Memory Information:"
free -h
echo "Disk Information:"
df -h
Task Scheduler Script
Automate tasks using cron:
#!/bin/bash
backup_script="/path/to/backup/[Link]"
cron_time="0 3 * * *" # Runs at 3 AM daily
(crontab -l; echo "$cron_time $backup_script") | crontab -
echo "Task scheduled to run daily at 3 AM."
Disk Space Monitoring Script
Monitor and report low disk space:
#!/bin/bash
threshold=80
for partition in $(df -h | grep "^/dev" | awk '{print $5 " " $1}'); do
usage=$(echo $partition | awk '{print $1}' | sed 's/%//')
mount=$(echo $partition | awk '{print $2}')
if [ $usage -ge $threshold ]; then
echo "Warning: $mount is $usage% full."
fi
done