Practical Bash Scripting Examples: Automating Tasks

October 30, 2024

Practical Bash Scripting Examples: Automating Tasks

Welcome to the final post in our Bash scripting series! In this post, we will explore practical examples of Bash scripts that can help you automate common tasks. By applying the knowledge you’ve gained in previous posts, you will see how to write scripts that can save you time and effort in your daily computing tasks.

Example 1: Backing Up Files

One common task that many users face is backing up important files. Below is a simple Bash script that creates a backup of a specified directory.

#!/bin/bash

# Directory to back up
SOURCE_DIR="/path/to/source"

# Backup destination
BACKUP_DIR="/path/to/backup"

# Create a timestamp
TIMESTAMP=$(date +%Y%m%d_%H%M%S)

# Create a backup
cp -r "$SOURCE_DIR" "$BACKUP_DIR/backup_$TIMESTAMP"

echo "Backup of $SOURCE_DIR completed successfully!"

In this script:

  • We define the source directory and the backup destination.
  • We create a timestamp to append to the backup folder name.
  • We use the cp command to create a backup.
  • Finally, we provide feedback to the user.

Example 2: Automating System Updates

Keeping your system updated is crucial for security and performance. Here’s a Bash script that automates the process of updating a Debian-based system.

#!/bin/bash

# Update the package list
sudo apt update

# Upgrade all packages
sudo apt upgrade -y

# Clean up
sudo apt autoremove -y

echo "System update completed successfully!"

This script:

  • Updates the package list using apt update.
  • Upgrades all installed packages with apt upgrade.
  • Cleans up unnecessary packages.
  • Provides feedback upon completion.

Example 3: Monitoring Disk Usage

Monitoring disk usage is essential for maintaining system performance. The following script checks disk usage and sends an alert if usage exceeds a specified threshold.

#!/bin/bash

# Set threshold (in percentage)
THRESHOLD=80

# Get current disk usage
USAGE=$(df / | grep / | awk '{ print $5 }' | sed 's/%//g')

# Check if usage exceeds threshold
if [ "$USAGE" -gt "$THRESHOLD" ]; then
    echo "Warning: Disk usage is at ${USAGE}%!"
else
    echo "Disk usage is under control at ${USAGE}%."
fi

In this script:

  • We define a threshold for disk usage.
  • We retrieve the current disk usage using df and process the output.
  • We use an if statement to check if usage exceeds the threshold and provide appropriate feedback.

Example 4: Batch Renaming Files

Batch renaming files can be tedious. Here’s a script that renames all .txt files in a directory by adding a prefix.

#!/bin/bash

# Directory containing files
DIRECTORY="/path/to/directory"

# Prefix to add
PREFIX="new_"

# Loop through .txt files and rename
for FILE in "$DIRECTORY"/*.txt; do
    mv "$FILE" "${DIRECTORY}/${PREFIX}$(basename "$FILE")"
done

echo "Files renamed successfully!"

This script:

  • Defines the directory and prefix for renaming.
  • Uses a for loop to iterate through all .txt files.
  • Renames each file using the mv command.

Conclusion

In this post, we’ve explored several practical examples of Bash scripting that can help you automate everyday tasks. By applying these examples, you can enhance your productivity and efficiency in managing your system. Remember, the key to mastering Bash scripting is practice, so don’t hesitate to modify these scripts and create your own!

Thank you for following along in this Bash scripting series! Happy scripting!