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,440 points
3,619 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,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,520 points
431 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,520 points
332 views
0 votes
0 answers

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

2 days ago in AWS by anonymous
13 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,520 points
1,237 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,560 points
7,252 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 in AWS by athenauserz
1,049 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
3,039 views