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,490 points
9,647 views

1 answer to this question.

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,150 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
• 57,640 points
2,018 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
• 57,640 points
3,473 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 1,338 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 in AWS by MD
• 55,540 points
324 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
• 57,640 points
2,884 views
0 votes
1 answer
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
4,022 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
8,928 views