Search code examples
amazon-web-servicesamazon-s3aws-clitail

aws-cli command in windows to get the latest object from s3 bucket


I am using a command using aws cli in my windows machine to get latest file from s3 bucket .

aws s3 ls s3://Bucket-name --recursive | sort |tail -n 1

It is listing all the files in sorted manner according to date upto here:

aws s3 ls s3://Bucket-name --recursive | sort 

But writing the full command throws error:

'Tail is not recognized as an internal or external command'.

Is there some other alternative for tail or for the full command.


Solution

  • The AWS CLI permits JMESPath expressions in the --query parameter.

    This command shows the most recently-updated object:

    aws s3api list-objects --bucket my-bucket --query 'sort_by(Contents, &LastModified)[-1].Key' --output text
    

    It's basically saying:

    • Sort by LastModified
    • Obtain the last [-1] entry
    • Show the Key (filename)