How to download the latest file in a S3 bucket using AWS CLI

0 votes

I have an S3 bucket that contains database backups. I am creating a script that I would like to download the latest backup, but I'm not sure how to go about only grabbing the most recent file from a bucket.

Is it possible to copy only the most recent file from a s3 bucket to a local directory using AWS CLI tools?

Sep 6, 2018 in AWS by datageek
• 2,540 points
19,855 views

1 answer to this question.

+1 vote

You can use the below command

$ aws s3 ls $BUCKET --recursive | sort | tail -n 1 | awk '{print $4}' some/other/object

Command explained

$ aws s3 ls $BUCKET --recursive  -- Lists all the objects in bucket

$ aws s3 ls $BUCKET --recursive | sort  -- Lists all the objects based on date

$ aws s3 ls $BUCKET --recursive | sort | tail -n 1 -- Selects the last line of the output ie most recent object

$ aws s3 ls $BUCKET --recursive | sort | tail -n 1 | awk '{print $4}' -- prints the name of most recent object

To learn more about Migrating to AWS, join our AWS Certification. Also, If you wish to master the principles of AWS and work on step-by-step tasks that have significant relevance for the world of business, industry professionals developed the AWS syllabus.

answered Sep 6, 2018 by Archana
• 4,170 points

Related Questions In AWS

–1 vote
1 answer

How to decrypt the encrypted S3 file using aws-encryption-cli --decrypt

Use command : aws s3 presign s3://mybucket/abc_count.png you get ...READ MORE

answered Oct 22, 2018 in AWS by Priyaj
• 58,020 points
5,204 views
0 votes
1 answer

How to create a S3 bucket using AWS CLI?

You can use the following command:- C:\Users\priyj_kumar>aws s3api ...READ MORE

answered Feb 15, 2019 in AWS by Priyaj
• 58,020 points
6,127 views
0 votes
0 answers

How to upload a file in to aws s3 by using programmatically??

Sep 13, 2019 in AWS by anonymous

closed Sep 16, 2019 by Kalgi 3,897 views
0 votes
1 answer

How to edit files in the AWS S3 bucket from CLI?

Hi@akhtar, According to my knowledge,  you can't edit ...READ MORE

answered Jun 8, 2020 in AWS by MD
• 95,460 points
9,685 views
0 votes
1 answer

Unziiping a tar.gz file in aws s3 bucket and upload it back to s3 using lambda

Hi@khyati, You can do your task using lambda. ...READ MORE

answered Dec 3, 2020 in AWS by MD
• 95,460 points
18,744 views
+1 vote
3 answers

How to get ARN for s3 Bucket using aws cli .

An ARN is a non-opaque, constructible identifier, ...READ MORE

answered Aug 16, 2018 in AWS by Priyaj
• 58,020 points
5,645 views
0 votes
2 answers

How to skip headers when reading a CSV file in S3 and creating a table in AWS Athena?

Thanks for the answer. This should be clear ...READ MORE

answered Aug 14, 2019 in AWS by athenauserz
11,833 views
0 votes
2 answers

How to display just the name of files using aws s3 ls command?

aws s3 ls s3://<your_bucket_name>/ | awk '{print ...READ MORE

answered Mar 17, 2019 in AWS by anonymous
22,211 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP