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.
Typical output looks like this:
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.
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:
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.
This lists actual object keys and avoids parsing the PRE display format entirely.
If you want keys under a specific prefix:
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.
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.
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 lsoutput in scripts whens3apiwould provide structured object data. - Assuming
PRElines 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 lsshows both objects andPREprefix lines.' - '
grep -v '^ *PRE 'is a quick way to hide prefix lines interactively.' - '
aws s3api list-objects-v2is 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.

