如何从 AWS s3 中删除除最近 5 个 updated/new 文件以外的所有文件?
How do I delete all except the latest 5 recently updated/new files from AWS s3?
我可以使用以下命令从 AWS S3 获取最近五个更新的文件
aws s3 ls s3://somebucket/ --recursive | sort | tail -n 5 | awk '{print }'
现在我需要删除 AWS S3 中的所有文件,除了从 AWS 中的上述命令获取的最后 5 个文件。
说命令获取 1.txt,2.txt,3.txt,4.txt,5.txt
。我需要从 AWS S3 中删除除 1.txt,2.txt,3.txt,4.txt,and 5.txt
.
之外的所有内容
使用带有多个 --exclude
选项的 AWS s3 rm 命令(我假设最后 5 个文件不属于某个模式)
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"
注意:确保你尝试使用--dryrun
选项,在实际删除文件之前确认要删除的文件不包括这5个文件。
使用带 head
的负数来获取除最后 n
行以外的所有行:
aws s3 ls s3://somebucket/ --recursive | sort | head -n -5 | while read -r line ; do
echo "Removing ${line}"
aws s3 rm s3://somebucket/${line}
done
短篇小说: 基于@bcattle answser,这项工作适用于 AWS CLI 2:
aws s3 ls s3://[BUCKER_NAME] --recursive | awk 'NF>1{print }' | grep . | sort | head -n -5 | while read -r line ; do
echo "Removing ${line}"
aws s3 rm s3://[BUCKER_NAME]/${line}
done
长话短说: aws s3 ls
在 CLI 2 文件路径下返回,还有日期创建。这种行为在我们的脚本中是意料之外的,因为我们只希望文件路径与存储桶 uri 连接。
我可以使用以下命令从 AWS S3 获取最近五个更新的文件
aws s3 ls s3://somebucket/ --recursive | sort | tail -n 5 | awk '{print }'
现在我需要删除 AWS S3 中的所有文件,除了从 AWS 中的上述命令获取的最后 5 个文件。
说命令获取 1.txt,2.txt,3.txt,4.txt,5.txt
。我需要从 AWS S3 中删除除 1.txt,2.txt,3.txt,4.txt,and 5.txt
.
使用带有多个 --exclude
选项的 AWS s3 rm 命令(我假设最后 5 个文件不属于某个模式)
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"
注意:确保你尝试使用--dryrun
选项,在实际删除文件之前确认要删除的文件不包括这5个文件。
使用带 head
的负数来获取除最后 n
行以外的所有行:
aws s3 ls s3://somebucket/ --recursive | sort | head -n -5 | while read -r line ; do
echo "Removing ${line}"
aws s3 rm s3://somebucket/${line}
done
短篇小说: 基于@bcattle answser,这项工作适用于 AWS CLI 2:
aws s3 ls s3://[BUCKER_NAME] --recursive | awk 'NF>1{print }' | grep . | sort | head -n -5 | while read -r line ; do
echo "Removing ${line}"
aws s3 rm s3://[BUCKER_NAME]/${line}
done
长话短说: aws s3 ls
在 CLI 2 文件路径下返回,还有日期创建。这种行为在我们的脚本中是意料之外的,因为我们只希望文件路径与存储桶 uri 连接。