AWS S3
AWS CLI
Command Line Tools
File Management
Cloud Storage

How to display only files from aws s3 ls command?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

When you run aws s3 ls, the output often includes both object entries and PRE lines that represent common prefixes, which look like folders. If you want only files, the simplest fix is filtering out PRE lines, but the more reliable approach for scripts is often switching to aws s3api and querying actual object keys directly.

Understand What aws s3 ls Is Showing

Amazon S3 is object storage, not a real hierarchical filesystem. What looks like a directory in aws s3 ls output is usually just a key prefix rendered for convenience.

bash
aws s3 ls s3://my-bucket/

Typical output looks like this:

text
                           PRE images/
2026-03-01 10:00:00       1024 notes.txt
2026-03-01 10:01:00       2048 report.csv

The PRE line is not a file object in the same sense as notes.txt. It is the CLI showing a prefix.

Filter aws s3 ls Output with grep

If you are staying with the high-level aws s3 command, removing PRE lines is often enough.

bash
aws s3 ls s3://my-bucket/ | grep -v '^ *PRE '

That works because directory-like prefixes are printed with PRE, while real file entries have timestamps and sizes. This is a quick answer for interactive use.

If you want just the object names, add awk:

bash
aws s3 ls s3://my-bucket/ | grep -v '^ *PRE ' | awk '{print $4}'

That gives you just the final key segment in the displayed listing.

Prefer s3api for Scripted Reliability

For automation, aws s3api list-objects-v2 is usually cleaner because it returns structured object metadata instead of human-formatted text.

bash
1aws s3api list-objects-v2 \
2  --bucket my-bucket \
3  --query 'Contents[].Key' \
4  --output text

This lists actual object keys and avoids parsing the PRE display format entirely.

If you want keys under a specific prefix:

bash
1aws s3api list-objects-v2 \
2  --bucket my-bucket \
3  --prefix logs/2026/ \
4  --query 'Contents[].Key' \
5  --output text

That is usually the better choice for CI jobs, shell scripts, and data pipelines.

Distinguish Real Objects from Folder Markers

Some buckets contain zero-byte objects whose keys end with /, often called folder markers. Those are real S3 objects even though they look like directories. If you want only non-marker files, filter them out explicitly.

bash
1aws s3api list-objects-v2 \
2  --bucket my-bucket \
3  --query 'Contents[?!ends_with(Key, `/`)].Key' \
4  --output text

That matters because PRE lines and folder-marker objects are not the same thing. One is CLI presentation, the other is an actual stored object.

Format the Output for Downstream Commands

If the next step is another shell command, prefer one-key-per-line output.

bash
1aws s3api list-objects-v2 \
2  --bucket my-bucket \
3  --query 'Contents[].Key' \
4  --output text | tr '\t' '\n'

That avoids brittle tab-parsing later in the pipeline.

Choose the Right Command for the Job

Use aws s3 ls when you want a quick human-readable listing. Use aws s3api list-objects-v2 when the output will feed another program. The second approach is usually more correct for long-term scripts because it does not depend on formatting conventions intended for people.

Common Pitfalls

  • Treating S3 prefixes as real directories instead of display-friendly key grouping.
  • Parsing aws s3 ls output in scripts when s3api would provide structured object data.
  • Assuming PRE lines and zero-byte folder-marker objects are the same thing.
  • Forgetting to filter keys that end with / when folder markers should be excluded.
  • Using human-oriented output formats where downstream tools need one key per line.

Summary

  • 'aws s3 ls shows both objects and PRE prefix lines.'
  • 'grep -v '^ *PRE ' is a quick way to hide prefix lines interactively.'
  • 'aws s3api list-objects-v2 is a better choice for scripts.'
  • Folder-marker objects may need separate filtering with key-based conditions.
  • Choose structured output when the result feeds another command or program.

Course illustration
Course illustration

All Rights Reserved.