aws s3 ls sort by date: list S3 bucket contents by last modified
Codemia Knowledge Hub ·
The S3 API cannot sort objects by modification date, so you retrieve the listing and sort it client-side. With the AWS CLI, pipe the output through sort (add -r for newest first). With Python and Boto3, sort the results in memory. For buckets with millions of objects, use S3 Inventory reports instead of repeated API calls, which are slow and costly at that scale.
Why S3 returns keys, not dates, in order
Every S3 object has a LastModified timestamp recording when it was created or last overwritten. It is returned by both ListObjectsV2 and HeadObject, as a UTC timestamp.
S3 does not keep a modification-date index. ListObjectsV2 returns objects in lexicographic key order (the object's path), so any date sorting happens after retrieval.
Sorting with the AWS CLI
List with dates
This prints three columns: modification date, size in bytes and key.
Sort by modification date
sort -k1 sorts by the first column (LastModified). S3 timestamps are ISO 8601, so lexicographic order is chronological order.
Newest objects first
-r reverses the order so the newest objects come first.
Filter by prefix
A prefix reduces the objects returned, which speeds up both the API call and the sort.
aws s3 ls (simpler, less flexible)
aws s3 ls prints date and time in the first two columns, so sorting on both (-k1,2) gives chronological order. It does not support JMESPath queries for filtering.
Sorting with Python and Boto3
Basic script
Paginating large buckets
list_objects_v2 returns at most 1,000 objects per call. Use the paginator for anything larger:
Filter by prefix and date range
The N most recently modified objects
The heap keeps memory proportional to the top N results instead of O(total_objects) for sorting the whole listing.
S3 Inventory for very large buckets
For millions of objects, repeated list_objects_v2 calls are slow and expensive. S3 Inventory produces a daily or weekly manifest of all objects as CSV, ORC or Parquet files in a destination bucket.
Configure S3 Inventory
Query the inventory with Athena
Once delivered, query the inventory with Amazon Athena:
For buckets with tens of millions of objects, this is dramatically faster and cheaper than paginating the list API.
Which method to use
| Method | Best For | Max Objects | Cost | Sort Support |
| AWS CLI + sort | Quick checks, small buckets | Up to 100K | Standard API pricing | Client-side |
| Boto3 paginator | Automation, scripting, filtering | Up to 1M+ | Standard API pricing | Client-side |
| Boto3 + heapq | Finding top N in large buckets | Any size | Standard API pricing | Heap-based (efficient) |
| S3 Inventory + Athena | Millions of objects | Unlimited | Inventory + Athena pricing | SQL ORDER BY |
IAM permissions required
list_objects_v2 needs s3:ListBucket, a bucket-level permission applied to the bucket ARN without /*, not an object-level one.
Mistakes that give wrong or partial results
- Expecting date order from the API. Objects always come back in lexicographic key order.
- Skipping pagination. Without it you only see the first 1,000 objects (by key), which may not include the ones you want.
- Sorting millions of objects in memory. It is slow and expensive; use S3 Inventory with Athena.
- Reading
LastModifiedas the completion time for multipart uploads. For multipart uploads, S3 sets it to when the upload was initiated, not when it completed. - Granting
s3:GetObjectinstead ofs3:ListBucket. Listing requiress3:ListBucketon the bucket resource. - Ignoring API costs. Each
list_objects_v2call is a LIST request. At $0.005 per 1,000 requests, a full scan of a 10 million object bucket takes 10,000 requests and costs about $0.05. The bigger cost is time, so S3 Inventory is still the better fit for repeated analysis.
Related reading
- How long should I wait after applying an AWS IAM policy before it is valid?
- How many databases can I create on a single Amazon RDS instance
- How many records can be in S3 put event lambda trigger?
- How many records i can insert using DynamoDb BatchWrite by Boto3
- How long the messages will be kept stored in topic/partition using kafka 0.9
- how many consecutive elements are smaller before each item in the array
- How redirect a domain to Amazon EC2 Machine?
- How reliable is AWS S3 Event Nofications on SQS?
System design interview problems on this topic
- Design Dropbox: solution and walkthrough
- Design a Global Content Distribution Network: solution and walkthrough
- Design a Serverless Architecture Framework: solution and walkthrough
- Design Youtube or Netflix: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.