如何仅显示来自 aws s3 ls 命令的文件?

新手上路,请多包涵

我正在使用 aws cli 使用以下命令( 文档)列出 s3 存储桶中的文件:

 aws s3 ls s3://mybucket --recursive --human-readable --summarize

此命令为我提供以下输出:

 2013-09-02 21:37:53   10 Bytes a.txt
2013-09-02 21:37:53  2.9 MiB foo.zip
2013-09-02 21:32:57   23 Bytes foo/bar/.baz/a
2013-09-02 21:32:58   41 Bytes foo/bar/.baz/b
2013-09-02 21:32:57  281 Bytes foo/bar/.baz/c
2013-09-02 21:32:57   73 Bytes foo/bar/.baz/d
2013-09-02 21:32:57  452 Bytes foo/bar/.baz/e
2013-09-02 21:32:57  896 Bytes foo/bar/.baz/hooks/bar
2013-09-02 21:32:57  189 Bytes foo/bar/.baz/hooks/foo
2013-09-02 21:32:57  398 Bytes z.txt

Total Objects: 10
   Total Size: 2.9 MiB

但是,这是我想要的输出:

 a.txt
foo.zip
foo/bar/.baz/a
foo/bar/.baz/b
foo/bar/.baz/c
foo/bar/.baz/d
foo/bar/.baz/e
foo/bar/.baz/hooks/bar
foo/bar/.baz/hooks/foo
z.txt

如何省略日期、时间和文件大小以仅显示文件列表?

原文由 Borealis 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 803
2 个回答

您不能仅使用 aws 命令来执行此操作,但您可以轻松地将其通过管道传输到另一个命令以删除您不想要的部分。您还需要删除 --human-readable 标志以使输出更易于使用,并删除 --summarize 标志以删除最后的汇总数据。

尝试这个:

 aws s3 ls s3://mybucket --recursive | awk '{print $4}'

编辑:考虑文件名中的空格:

 aws s3 ls s3://mybucket --recursive | awk '{$1=$2=$3=""; print $0}' | sed 's/^[ \t]*//'

原文由 Mark B 发布,翻译遵循 CC BY-SA 4.0 许可协议

将 s3api 与 jq 一起使用( AWS docu aws s3api list-objects ):

这种模式总是递归的。

 $ aws s3api list-objects --bucket "bucket" | jq -r '.Contents[].Key'
a.txt
foo.zip
foo/bar/.baz/a
[...]

您可以通过添加前缀来过滤子目录(此处为 foo 目录)。前缀不能以 /

 $ aws s3api list-objects --bucket "bucket" --prefix "foo/" | jq -r '.Contents[].Key'
foo/bar/.baz/a
foo/bar/.baz/b
foo/bar/.baz/c
[...]

jq 选项:

  • -r = 原始模式,输出中没有引号
  • .Contents[] = 获取 Contents 对象数组内容
  • .Key = 获取每个关键字段(不生成有效的 JSON 数组,但我们处于原始模式,所以我们不在乎)

附录

您可以使用纯 AWS CLI,但值将由 \x09 = 水平选项卡分隔( AWS:控制 AWS CLI 的命令输出 - 文本输出格式

 $ aws s3api list-objects --bucket "bucket" --prefix "foo/" --query "Contents[].Key" --output text
foo/bar/.baz/a   foo/bar/.baz/b   foo/bar/.baz/c   [...]

AWS CLI 选项:

  • --query "Contents[].Key" = 查询内容对象数组并获取其中的每个键
  • --output text = 输出为制表符分隔的文本,带有现在的引号

基于李光阳评论的附录

带有新线的纯 AWS CLI:

 $ aws s3api list-objects --bucket "bucket" --prefix "foo/" --query "Contents[].{Key: Key}" --output text
foo/bar/.baz/a
foo/bar/.baz/b
foo/bar/.baz/c
[...]

原文由 notes-jj 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题