Building an Interactive S3 Bucket Manager with Bash

Managing AWS S3 buckets from the command line can be tedious, especially when you need to perform multiple operations. I created an interactive bash script that simplifies S3 bucket management with a user-friendly menu interface.

Why Build Th…


This content originally appeared on DEV Community and was authored by Dmitry Romanoff

Managing AWS S3 buckets from the command line can be tedious, especially when you need to perform multiple operations. I created an interactive bash script that simplifies S3 bucket management with a user-friendly menu interface.

Why Build This?

While AWS CLI is powerful, repeatedly typing long commands gets old fast. This script provides:

  • Interactive menu - No need to remember complex commands
  • Visual feedback - Clear success/failure indicators
  • Pattern matching - Find buckets using wildcards
  • Bulk operations - Upload/download entire folders
  • Safety checks - Validates bucket names and file existence

Key Features

1. Smart Bucket Pattern Matching

# Find buckets containing "backup"
*backup*

# Find buckets starting with "my"
my%

# Complex patterns
my*prod*2024

The script converts shell wildcards to regex patterns for flexible searching.

2. Visual Status Indicators

Every operation shows clear feedback:

  • ✅ Green checkmark for success
  • ❌ Red X for failures

3. Comprehensive File Operations

  • Upload/download single files
  • Recursive folder operations
  • File deletion with confirmation
  • Bucket-to-bucket copying

Core Implementation

The script uses a simple menu loop with case statements:

while true; do
    echo "S3 Bucket Manager - Bucket: $BUCKET_NAME"
    echo "1) List buckets by pattern"
    echo "2) Set bucket name"
    # ... more options

    read -p "Select option (1-14): " choice

    case $choice in
        1) # Pattern matching logic
        2) # Bucket name setting
        # ... handle each option
    esac
done

Bucket Name Validation

The script enforces AWS S3 naming rules:

if [[ ! "$BUCKET_NAME" =~ ^[a-z0-9][a-z0-9.-]*[a-z0-9]$ ]] || 
   [[ ${#BUCKET_NAME} -lt 3 ]] || 
   [[ ${#BUCKET_NAME} -gt 63 ]]; then
    echo "❌ Invalid bucket name"
fi

Safe File Handling

File paths are properly escaped and support tilde expansion:

filename=$(echo "$filename" | sed 's/^"\|"$//g')
filename=$(eval echo "$filename")

Advanced Operations

Bucket Renaming

Since S3 doesn't support direct renaming, the script:

  1. Creates new bucket
  2. Syncs all contents
  3. Deletes original bucket
  4. Updates current bucket reference
if aws s3 mb "s3://$new_bucket" 2>/dev/null; then
    if aws s3 sync "s3://$BUCKET_NAME" "s3://$new_bucket" 2>/dev/null && 
       aws s3 rb "s3://$BUCKET_NAME" --force 2>/dev/null; then
        BUCKET_NAME="$new_bucket"
        echo "✅ Bucket renamed to '$new_bucket'"
    fi
fi

Pattern Matching Magic

Converting shell wildcards to regex:

pattern=$(echo "$pattern" | sed 's/%/.*/g' | sed 's/\*/.*/g')
aws s3 ls | grep -E "$pattern" | head -20

Usage Examples

Quick Setup

./s3_manager.sh
# Enter bucket name when prompted
# Use menu to perform operations

Common Workflows

  1. Find and switch to bucket:

    • Option 1: List buckets with pattern
    • Option 2: Set bucket name
  2. Upload project folder:

    • Option 8: Copy folder to bucket
    • Enter: /path/to/project
  3. Backup entire bucket:

    • Option 12: Copy bucket to new bucket
    • Creates complete duplicate

Error Handling

The script handles common issues:

  • Missing files/folders
  • Invalid bucket names
  • Network failures
  • Permission errors

All AWS CLI operations redirect stderr to /dev/null and check exit codes for clean error reporting.

Prerequisites

  • AWS CLI installed and configured
  • Valid AWS credentials with S3 permissions
  • Bash shell (works on macOS, Linux, WSL)

Code

#!/bin/bash

# Get bucket name from user
read -p "Enter S3 bucket name: " BUCKET_NAME

while true; do
    echo
    echo "S3 Bucket Manager - Bucket: $BUCKET_NAME"
    echo "----------------------------------------------------------"
    echo "1) List buckets by pattern"
    echo "2) Set bucket name"
    echo "3) Check if bucket exists"
    echo "4) Create bucket"
    echo "5) List files in bucket"
    echo "6) Copy file to bucket"
    echo "7) Copy file from bucket"
    echo "8) Copy folder to bucket"
    echo "9) Copy folder from bucket"
    echo "10) Remove file from bucket"
    echo "11) Remove bucket"
    echo "12) Copy bucket to new bucket"
    echo "13) Rename bucket"
    echo "14) Exit"
    echo
    read -p "Select option (1-14): " choice

    case $choice in
        1)
            read -p "Enter pattern (* for wildcard, % for any chars): " pattern
            pattern=$(echo "$pattern" | sed 's/%/.*/g' | sed 's/\*/.*/g')
            aws s3 ls | grep -E "$pattern" | head -20 || echo "No buckets match pattern"
            ;;
        2)
            read -p "Enter new S3 bucket name: " BUCKET_NAME
            echo "✅ Bucket name set to: $BUCKET_NAME"
            ;;
        3)
            if aws s3 ls "s3://$BUCKET_NAME" >/dev/null 2>&1; then
                echo "✅ Bucket '$BUCKET_NAME' exists"
            else
                echo "❌ Bucket '$BUCKET_NAME' does not exist"
            fi
            ;;
        4)
            if aws s3 ls "s3://$BUCKET_NAME" >/dev/null 2>&1; then
                echo "✅ Bucket '$BUCKET_NAME' already exists"
            else
                # Validate bucket name
                if [[ ! "$BUCKET_NAME" =~ ^[a-z0-9][a-z0-9.-]*[a-z0-9]$ ]] || [[ ${#BUCKET_NAME} -lt 3 ]] || [[ ${#BUCKET_NAME} -gt 63 ]]; then
                    echo "❌ Invalid bucket name. Must be 3-63 chars, lowercase, start/end with letter/number"
                elif aws s3 mb "s3://$BUCKET_NAME" 2>/dev/null; then
                    echo "✅ Bucket '$BUCKET_NAME' created"
                else
                    echo "❌ Failed to create bucket '$BUCKET_NAME' (may already exist globally)"
                fi
            fi
            ;;
        5)
            aws s3 ls "s3://$BUCKET_NAME"
            ;;
        6)
            read -p "Enter filename to upload: " filename
            filename=$(echo "$filename" | sed 's/^"\|"$//g')
            filename=$(eval echo "$filename")
            if [ -f "$filename" ]; then
                if aws s3 cp "$filename" "s3://$BUCKET_NAME/" 2>/dev/null; then
                    echo "✅ File '$filename' uploaded to bucket"
                else
                    echo "❌ Failed to upload file '$filename'"
                fi
            else
                echo "❌ File '$filename' not found"
            fi
            ;;
        7)
            read -p "Enter filename to download: " filename
            filename=$(echo "$filename" | sed 's/^"\|"$//g')
            if aws s3 cp "s3://$BUCKET_NAME/$filename" "./" 2>/dev/null; then
                echo "✅ File '$filename' downloaded to current directory"
            else
                echo "❌ Failed to download file '$filename'"
            fi
            ;;
        8)
            read -p "Enter folder path to upload: " foldername
            foldername=$(echo "$foldername" | sed 's/^"\|"$//g')
            if [ -d "$foldername" ]; then
                if aws s3 cp "$foldername" "s3://$BUCKET_NAME/" --recursive 2>/dev/null; then
                    echo "✅ Folder '$foldername' uploaded to bucket"
                else
                    echo "❌ Failed to upload folder '$foldername'"
                fi
            else
                echo "❌ Folder '$foldername' not found"
            fi
            ;;
        9)
            read -p "Enter folder name to download: " foldername
            foldername=$(echo "$foldername" | sed 's/^"\|"$//g')
            if aws s3 cp "s3://$BUCKET_NAME/$foldername" "./$foldername" --recursive 2>/dev/null; then
                echo "✅ Folder '$foldername' downloaded to current directory"
            else
                echo "❌ Failed to download folder '$foldername'"
            fi
            ;;
        10)
            read -p "Enter filename to remove: " filename
            if aws s3 rm "s3://$BUCKET_NAME/$filename" 2>/dev/null; then
                echo "✅ File '$filename' removed from bucket"
            else
                echo "❌ Failed to remove file '$filename'"
            fi
            ;;
        11)
            if aws s3 rb "s3://$BUCKET_NAME" --force 2>/dev/null; then
                echo "✅ Bucket '$BUCKET_NAME' removed"
            else
                echo "❌ Failed to remove bucket '$BUCKET_NAME'"
            fi
            ;;
        12)
            read -p "Enter new bucket name: " new_bucket
            if aws s3 mb "s3://$new_bucket" 2>/dev/null; then
                if aws s3 sync "s3://$BUCKET_NAME" "s3://$new_bucket" 2>/dev/null; then
                    echo "✅ Bucket '$BUCKET_NAME' copied to '$new_bucket'"
                else
                    echo "❌ Failed to copy bucket contents"
                fi
            else
                echo "❌ Failed to create new bucket '$new_bucket'"
            fi
            ;;
        13)
            read -p "Enter new bucket name: " new_bucket
            if aws s3 mb "s3://$new_bucket" 2>/dev/null; then
                if aws s3 sync "s3://$BUCKET_NAME" "s3://$new_bucket" 2>/dev/null && aws s3 rb "s3://$BUCKET_NAME" --force 2>/dev/null; then
                    BUCKET_NAME="$new_bucket"
                    echo "✅ Bucket renamed to '$new_bucket'"
                else
                    echo "❌ Failed to rename bucket"
                fi
            else
                echo "❌ Failed to create new bucket '$new_bucket'"
            fi
            ;;
        14)
            echo "Exiting..."
            exit 0
            ;;
        *)
            echo "Invalid option. Please select 1-14."
            ;;
    esac
done

Conclusion

This interactive S3 manager transforms tedious AWS CLI commands into a streamlined workflow. The menu-driven interface makes it perfect for:

  • DevOps automation
  • Quick file transfers
  • Bucket management tasks
  • Learning S3 operations

The script demonstrates how bash can create powerful, user-friendly tools for cloud management. With just 150 lines of code, you get a comprehensive S3 management interface.


This content originally appeared on DEV Community and was authored by Dmitry Romanoff


Print Share Comment Cite Upload Translate Updates
APA

Dmitry Romanoff | Sciencx (2025-08-04T13:38:39+00:00) Building an Interactive S3 Bucket Manager with Bash. Retrieved from https://www.scien.cx/2025/08/04/building-an-interactive-s3-bucket-manager-with-bash/

MLA
" » Building an Interactive S3 Bucket Manager with Bash." Dmitry Romanoff | Sciencx - Monday August 4, 2025, https://www.scien.cx/2025/08/04/building-an-interactive-s3-bucket-manager-with-bash/
HARVARD
Dmitry Romanoff | Sciencx Monday August 4, 2025 » Building an Interactive S3 Bucket Manager with Bash., viewed ,<https://www.scien.cx/2025/08/04/building-an-interactive-s3-bucket-manager-with-bash/>
VANCOUVER
Dmitry Romanoff | Sciencx - » Building an Interactive S3 Bucket Manager with Bash. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/08/04/building-an-interactive-s3-bucket-manager-with-bash/
CHICAGO
" » Building an Interactive S3 Bucket Manager with Bash." Dmitry Romanoff | Sciencx - Accessed . https://www.scien.cx/2025/08/04/building-an-interactive-s3-bucket-manager-with-bash/
IEEE
" » Building an Interactive S3 Bucket Manager with Bash." Dmitry Romanoff | Sciencx [Online]. Available: https://www.scien.cx/2025/08/04/building-an-interactive-s3-bucket-manager-with-bash/. [Accessed: ]
rf:citation
» Building an Interactive S3 Bucket Manager with Bash | Dmitry Romanoff | Sciencx | https://www.scien.cx/2025/08/04/building-an-interactive-s3-bucket-manager-with-bash/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.