Shell Script to get the versioning status of S3 buckets

Today I received a request to get the versioning status of the buckets used by the engineering team. To check the bucket versioning status, normally you can go the S3 page, select the bucket and see the status of versioning in the properties box.

In the below screenshot, we can see versioning is disabled for this bucket.

S3 bucket properties

One can follow this approach for 10, 20, maybe 50 buckets. What if you’ve to check for 100 buckets, 200, 500 buckets?

We can make use of s3api cli command for this requirement.

Note: s3api is installed in almost every ec2 instance. If not, please get it installed before proceeding with this.

[email protected]:~# aws s3api get-bucket-versioning --bucket --region us-east-1
"Status": "Enabled"

If versioning is enabled, it will give the output as “Status”:”Enabled”; if versioning is suspended, output as “Status”:”Suspended”. If versioning is not enabled, the command returns no output.


To print in text format:

[email protected]:~# aws s3api get-bucket-versioning --bucket random.bucket.nameĀ --region us-east-1 --output text


You can configure both the region and the output format in ~/.aws/configure

To check for the list of buckets:

We can use shell script for this requirement.

Save the buckets name in a file and run the below for loop over them.

for buck in $(cat bucketlist.txt)
echo "Checking $buck"
aws s3api get-bucket-versioning --bucket $buck --region us-east-1 --output text
echo "------"

Output would look like this:

Checking bucket1
Checking bucket2
Checking bucket3
Checking bucket4

CSV style output:
The above script helps to find the list of buckets with versioning enabled, but it’s difficult to copy them to a spreadsheet as the output is in two lines and not excel cells friendly.

To get in csv format,

for buck in $(cat bucketlist.txt)
printf "$(echo $buck),$(aws s3api get-bucket-versioning --bucket $buck --region us-east-1 --output text) \n"

Now the output would look like this.

It’s easier to copy this to excel and split it into columns.

Further optimisation of the script:

Instead of manually writing the buckets in a file and run a for loop over them, we can get the list of buckets using s3 ls command and pass it to the loop.

read -p "Enter the region name: " REG
aws s3 ls --region $REG | awk '{print $3}' > ~/bucketlist.txt
for buck in $(cat ~/bucketlist.txt)
printf "$(echo $buck),$(aws s3api get-bucket-versioning --bucket $buck --region $REG) \n"



Posted in AWS

One thought on “Shell Script to get the versioning status of S3 buckets

Leave a Reply

Your email address will not be published. Required fields are marked *