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,430 points
1,853 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

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

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

Related Questions In AWS

0 votes
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
• 56,140 points
219 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 in AWS by Priyaj
• 56,140 points
82 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
• 56,140 points
800 views
0 votes
1 answer
–1 vote
1 answer

How to read a csv file stored in Amazon S3 using csv.DictReader

The code would be something like this: import ...READ MORE

answered Oct 25, 2018 in AWS by Archana
• 5,360 points
4,207 views
0 votes
1 answer

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

This is a known deficiency. The best ...READ MORE

answered Sep 4, 2018 in AWS by Archana
• 4,090 points
580 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 in AWS by anonymous
1,705 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.