use*_*372 1 linux shell amazon-s3 amazon-web-services aws-cli
我可以使用以下命令从 AWS S3 中获取最后五个更新的文件
aws s3 ls s3://somebucket/ --recursive | sort | tail -n 5 | awk '{print $4}'
Run Code Online (Sandbox Code Playgroud)
现在我需要删除 AWS S3 中的所有文件,除了从 AWS 中的上述命令获取的最后 5 个文件。
说命令 fetches 1.txt,2.txt,3.txt,4.txt,5.txt。我需要从 AWS S3 中删除除1.txt,2.txt,3.txt,4.txt,and 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" --exclude "somebucket/4.txt" --exclude "somebucket/5.txt"
Run Code Online (Sandbox Code Playgroud)
注意:请务必使用--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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1679 次 |
| 最近记录: |