Toronto, Canada · Support hours Mon–Fri, 9 AM–5 PM ET
+1-647-578-8440 +1-800-406-7400 (US & Intl)

How to check file update on an S3 bucket and download latest version into the local folder

Rez Moss 1 min read
How to check file update on an S3 bucket and download latest version into the local folder

1- Install the AWS CLI on your server.

2- Run aws configure to add your AWS key and secret.

3- Run aws s3 ls to confirm the CLI works and you can access your S3 bucket.

4- Save the code below into a bash script file:

#!/bin/bash

aws s3 ls s3://MY-BUCKET > a.txt     #get files list in S3 bucket
A=$(cat a.txt| md5sum | cut -d " " -f1)  #get md5 checksum for a.txt
B=$(cat b.txt| md5sum | cut -d " " -f1)  #get md5 checksum for b.txt


if [[ $A == $B ]]   #If A==B, which means they are equal
then
echo "Nothing to update => " $(date +"%d-%m-%Y-%H-%M-%S") >> logs.txt
else
echo "We should update"
    rm -rf b.txt 
    rm -rf updatedPkg.zip

    aws s3 cp s3://MY-BUCKET/updatedPkg.zip ./updatedPkg.zip  #Copy new file from S3 to local

    cp a.txt b.txt  # overwrite b.txt with a.txt content
    
    echo "last update => " $(date +"%d-%m-%Y-%H-%M-%S") >> logs.txt
fi

5- Add the script to cron to run it on a schedule.

Need expert advice? Schedule your AWS consultation today!

Schedule now

Keep reading

All articles →
migration 4 min read

Migrating from On-Premises Servers to AWS EC2 Benefits and Step-by-Step Guide

Why moving on-premises servers to AWS EC2 pays off in cost, scale, and uptime, plus a four-step plan: assess, plan, migrate, optimize.

Read article →
aws 5 min read

Understanding AWS Lambda Pricing - A Comprehensive Breakdown

How AWS Lambda pricing works, how it compares with EC2, and practical ways to cut your Lambda bill.

Read article →
aws 5 min read

Top 5 Innovative Uses of AWS Lambda in Modern Web Applications

Five practical uses of AWS Lambda in web applications: real-time data processing, scheduled tasks, serverless APIs, IoT backends, and security …

Read article →

Need expert help with AWS?

Book a consultation or pick a support plan. Certified engineers answer during business hours, and you can cancel anytime.

See pricingContact us