How to print only file names from an S3 bucket

Run the below command from AWS CLI to print only the names of objects in an s3 bucket where bucketname is the name of the bucket.

aws s3 ls s3://bucketname/ | awk '{print $4}'

To print names of objects within a folder under s3 bucket, use the below command where prefix is a folder within your s3 bucket.

aws s3 ls s3://bucketname/prefix/ | awk '{print $4}'

Use the below command to print names of all the objects in an s3 bucket and all it’s prefixes(recursively).

s3cmd ls -r s3://bucketname/ | awk '{print $4}'

You can redirect output of any of the above commands to a file as shown below:

aws s3 ls s3://bucketname/prefix/ | awk '{print $4}'> listfile
See also  How to modify "Delete on Termination" attribute of an EC2 Instance from AWS CLI