简体   繁体   中英

ls in awscli returns "PRE". Why and how to get rid of it

Using awscli in git bash, the command

aws s3 ls "s3://directory/"

returns a list of

PRE "filename"

This is inconvenient as I need to do further commands on the output and I only need the file/directory names within the given directory.

For instance, it would be nice to be able to do:

for dir in $(aws s3 ls s3://directory/) do
 aws s3 ls $dir | grep .json;
done

Any suggestions to work around this?

What is "PRE"?

You can think of prefixes as a way to organize your data in a similar way to directories. However, prefixes are not directories.

How do I get rid of "PRE" in the output?

  • Use the aws s3api list-objects-v2 command which supports querying and output formatting instead of the aws s3 ls command.
aws s3api list-objects-v2 --bucket <mybucket_name> --prefix <path> --query "Contents[?contains(Key, '.json')].Key"

Note that <bucketname> here is just the name of the bucket. Do not include s3:// or any additional / .

The --query parameter is what gives you the powerful output.

For example

  • --query Contents[].Key would print all of the keys.
  • --query Contents[?contains(Key, '.json')].Key specifies additional criteria on the Key (the Key contains .json (eg the extension)) and then the final .Key tells it what attribute of the json output to return.

你可以用类似的东西做到这一点

aws s3 ls s3://directory --recursive | awk '{print $4}' | grep .json

To list all folders:

aws s3 ls s3://bkt --recursive | tr -s ' ' | cut -d ' ' -f4- | grep "\/$" | sed 's/\/$//'

To list all files:

aws s3 ls s3://bkt --recursive | tr -s ' ' | cut -d ' ' -f4- | grep -v /$

To list all .json files:

aws s3 ls s3://bkt --recursive | tr -s ' ' | cut -d ' ' -f4- | grep "\.json$"

To list all .json and all .yaml files:

aws s3 ls s3://bkt --recursive | tr -s ' ' | cut -d ' ' -f4- | grep -E "(\.yaml|\.json)$"

To list all files except .json files:

aws s3 ls s3://bkt --recursive | tr -s ' ' | cut -d ' ' -f4- | grep -v "\.json$"

Just by using the --recursive option is enough to get rid of the PRE string. In my case still was not good because it will recursively list all subdirectories and files. More info here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM