![](/img/trans.png)
[英]How to filter the output of ls command to display only files created in February?
[英]How to display only files from aws s3 ls command?
我正在使用 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
如何省略日期、時間和文件大小以僅顯示文件列表?
您不能僅使用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]*//'
將 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
= Horizontal Tab 分隔( 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
[...]
一個簡單的過濾器是:
aws s3 ls s3://mybucket --recursive | perl -pe 's/^(?:\S+\s+){3}//'
這將刪除日期、時間和大小。 只留下文件的完整路徑。 它也可以在沒有遞歸的情況下工作,它也應該與包含空格的文件名一起工作。
簡單的方法
aws s3 ls s3://mybucket --recursive --human-readable --summarize|cut -c 29-
我的解決方案
使用 aws cli 僅遞歸列出文件。
aws s3 ls s3://myBucket --recursive | awk 'NF>1{print $4}' | grep .
grep .
- 清除空行。
示例: aws s3 ls s3://myBucket
PRE f5c10c1678e8484482964b8fdcfe43ad/
PRE f65b94ad31734135a61a7fb932f7054d/
PRE f79b12a226b542dbb373c502bf125ffb/
PRE logos/
PRE test/
PRE userpics/
2019-05-14 10:56:28 7754 stage.js
解決方案: aws s3 ls s3://myBucket --recursive | awk 'NF>1{print $4}' | grep .
aws s3 ls s3://myBucket --recursive | awk 'NF>1{print $4}' | grep .
stage.js
簡單的命令是
aws s3 ls s3://mybucket --recursive --human-readable --summarize |cut -d ' ' -f 8
如果您需要時間戳,只需更新命令字段值。
僅對於文件名,我發現最簡單的是:
aws s3 ls s3://path/to/bucket/ | cut -d " " -f 4
這將在空格處剪切返回的輸出 ( cut -d " "
) 並返回第四列 ( -f 4
),即文件名列表。
一個 S3 存儲桶可能不僅有文件,還有帶有前綴的文件。 如果您使用--recursive
它不僅會列出文件,還會列出前綴。 如果您不關心前綴和存儲桶中的文件或存儲桶中的前綴,這應該可以工作。
aws s3 ls s3://$S3_BUCKET/$S3_OPTIONAL_PREFIX/ --recursive | awk '{ if($3 >0) print $4}'
awk
的$3
是文件的大小,如果前綴為0
。 也可能是文件為空,因此它也會跳過空文件。
我建議不要依賴間距並獲取第 4 個字段。
從技術上講,無論它在哪個位置,您都需要最后一個字段。
因此,使用rev
對您有利更安全......
rev
按字符反轉字符串輸入字符
因此,當您通過管道將aws s3 ls
輸出到rev
您將所有內容都顛倒了,包括字段的位置,因此最后一個字段始終成為第一個字段。
而不是弄清楚最后一個字段的位置,您只需rev
,首先獲取,然后再次rev
,因為該字段中的字符也會反過來。 (例如2013-09-02 21:32:57 23 Bytes foo/bar/.baz/a
變成a/zab./rab/oof setyB 32 75:23:12 20-90-3102
)
然后cut -d" "
-f1 would retrieve the first field
a/zab./rab/oof <br> then
again to get
rev again to get
foo/bar/.baz/a`
aws s3 ls s3://mybucket --recursive | rev | cut -d" " -f1 | rev
How to display only files from aws s3 ls command?
1. Basic command
$ aws s3 ls s3://bucket --recursive
output :
2021-02-10 15:29:02 0 documents/
2021-02-10 15:29:02 18 documents/data/data.txt
2021-03-15 23:35:12 0 documents/data/my code.txt
2. To get only keys from s3 bucket containing spaces also.
$ aws s3 ls s3://bucket --recursive | awk '{ $1=$2=$3=""; print $0}' | cut -c4-
output :
documents/
documents/data/data.txt
documents/data/my code.txt
3. Removing "documents/" from result
$ aws s3 ls s3://bucket --recursive | awk '$0 !~ /\/$/ { $1=$2=$3=""; print $0}' | cut -c4-
output :
documents/data/data.txt
documents/data/my code.txt
它只是通過開始符號進行過濾。 “^-”表示行以“-”符號開始。 另一方面,目錄以字母“d”開頭
ls -Al | grep "^-"
如果您的文件沒有空格,那么這是最簡單的方法:
aws s3 ls s3://mybucket | cut -c32-
輸出是:
1.txt.gz
2.txt.gz
3.txt.gz
代替:
2021-12-15 23:05:44 36 1.txt.gz
2021-12-15 23:05:45 37 2.txt.gz
2021-12-15 23:05:46 39 3.txt.gz
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.