Script to grab sizes of GCP buckets

Prerequisites:

  • Google Cloud SDK Installed & Configured

For a simple script that just does the job and gets the sizes of all your buckets:

for bucket in $(gsutil ls); do
  echo "Bucket: $bucket"
  gsutil du -sh $bucket
done

For a more comprehensive script that picks a specific set of buckets and saves the output to a file:

#!/bin/bash

# File to save the output
output_file="bucket_sizes.txt"

# List of specific buckets
buckets=(
  "gs://bucket1/"
  "gs://bucket2/"
  "gs://bucket3/"
  "gs://bucket4"
)

# Clear the output file if it exists, or create a new one
> $output_file

# Loop through the list and get the size of each bucket
for bucket in "${buckets[@]}"; do
  echo "Bucket: $bucket" >> $output_file
  gsutil du -sh $bucket >> $output_file
done

# Print a message indicating that the process is complete
echo "Bucket sizes have been saved to $output_file"

That’s it, Enjoy!

Unknown's avatar

About Ahmed Tawfik

Cybersecurity Professional, Systems Engineer, OSS & Linux Geek
This entry was posted in Linux and tagged , , . Bookmark the permalink.

Leave a comment