2

I have a S3 bucket with thousands of folders and many txt files inside those folders.

I would like to list all txt files inside the bucket so I can check if they're removable. Then remove them if they are.

Any idea how to do this with s3cmd?

4

1 回答 1

10

这相当简单,但取决于您希望检查的复杂程度。假设您想删除文件名包含“foo”的每个文本文件:

s3cmd --recursive ls s3://mybucket |
    awk '{ print $4 }' | grep "*.txt" | grep "foo" | xargs s3cmd del

如果你想要一个比 grep 可以处理的更复杂的检查,只需将前三个命令重定向到一个文件,然后手动编辑文件或使用或awkperl或任何你喜欢的工具,然后cat输出到s3cmd(取决于检查,你也可以通过管道完成所有操作):

s3cmd --recursive ls s3://mybucket | awk '{ print $4 }' | grep "*.txt" > /tmp/textfiles
magic-command-to-check-filenames /tmp/textfiles
cat /tmp/textfiles | xargs s3cmd del
于 2012-07-27T10:03:00.707 回答