如何从 AWS s3 中删除除最近 5 个最近更新/新文件之外的所有文件? [英] How do I delete all except the latest 5 recently updated/new files from AWS s3?

查看:33
本文介绍了如何从 AWS s3 中删除除最近 5 个最近更新/新文件之外的所有文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用以下命令从 AWS S3 中获取最后五个更新的文件

aws s3 ls s3://somebucket/--recursive |排序 |尾-n 5 |awk '{print $4}'

现在我需要删除 AWS S3 中的所有文件,除了从 AWS 中的上述命令获取的最后 5 个文件.

说命令获取1.txt,2.txt,3.txt,4.txt,5.txt.我需要从 AWS S3 中删除除 1.txt、2.txt、3.txt、4.txt 和 5.txt 之外的所有内容.

解决方案

使用 AWS s3 rm 命令带有多个 --exclude 选项(我假设最后 5 个文件不属于某个模式)

aws s3 rm s3://somebucket/--recursive --exclude "somebucket/1.txt" --exclude "somebucket/2.txt" --exclude "somebucket/3.txt" --排除somebucket/4.txt"——排除somebucket/5.txt"

注意:确保您使用 --dryrun 选项进行尝试,在实际删除文件之前确认要删除的文件不包括 5 个文件.>

I can fetch the last five updated files from AWS S3 using the below command

aws s3 ls s3://somebucket/ --recursive | sort | tail -n 5 | awk '{print $4}'

Now I need to delete all the files in AWS S3 except the last 5 files which are fetched from above command in AWS.

Say the command fetches 1.txt,2.txt,3.txt,4.txt,5.txt. I need to delete all from AWS S3 except 1.txt,2.txt,3.txt,4.txt,and 5.txt.

解决方案

Use AWS s3 rm command with multiple --exclude options (I assume the last 5 files do not fall under a pattern)

aws s3 rm s3://somebucket/ --recursive --exclude "somebucket/1.txt" --exclude "somebucket/2.txt" --exclude "somebucket/3.txt" --exclude "somebucket/4.txt" --exclude "somebucket/5.txt"

CAUTION: Make sure you try it with --dryrun option, verify the files to be deleted do not include the 5 files before actually removing the files.

这篇关于如何从 AWS s3 中删除除最近 5 个最近更新/新文件之外的所有文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆