There are 2 options for you to use.
- The first option requires the names of the last 5 files
- Second option is a shell script which you cna run to do the same
The first option is this:
Use --exclude option with the aws s3 rm command as show below:
aws s3 rm s3://somebucket/ --recursive --exclude "bucketname/1.txt" --exclude "bucketname/2.txt" --exclude "bucketname/3.txt" --exclude "bucketname/4.txt" --exclude "bucketname/5.txt"
Use this script to get all files but the last 5 uploaded files:
aws s3 ls s3://bucketname/ --recursive | sort | head -n -5 | while read -r line ; do
echo "Removing ${line}"
aws s3 rm s3://bucketname/${line}
done
line - The number of lines (objects) to be removed
bucketname - provide the name of the bucket with the objects
If you are keen to learn about the various AWS skills then this AWS Certification availed by Intellipaat is the right course for you. This course will help you learn to master AWS skills and ace the AWS certification exam!