title: Grab latest AWS S3 Folder Object name with AWS CLI
body: <p>I tried using this post to look for the last modified file then awk for the folder it's contained in: <a href="https://stackoverflow.com/questions/31062365/get-last-modified-object-from-s3-using-aws-cli">Get last modified object from S3 using AWS CLI</a></p>
  <p>But this isn't ideal for over 1000 folders and by <a href="https://awscli.amazonaws.com/v2/documentation/api/latest/reference/s3api/list-objects-v2.html" rel="nofollow noreferrer">documentation</a>, should be failing. I have 2000+ folder objects I need to search through. My desired folder will always begin with an D and be followed by a set of incrementing numbers. Ex: D1200</p>
  <p>The results from the answer led me to creating this call which works:</p>
  <pre><code>aws s3 ls main.test.staging/General_Testing/Results/ --recursive | sort | tail -n 1 | awk '{print $4}'
  </code></pre>
  <p>but it takes over 40 secs to search through thousands of files and I then need to regex parse the output to find the folder object and not the last file modified within it. Also, if I try to do this to find my desired folder (which is the object right after the <code>Results</code> object):</p>
  <pre><code>aws ls s3 main.test.staging/General_Testing/Results/ | sort | tail -1
  </code></pre>
  <p>Then my output will be <code>D998</code> because the sort function will order folder names like this:</p>
  <pre><code>D119
  D12
  D13
  </code></pre>
  <p>Because technically <code>D12</code> is greater than <code>D119</code> because it has a <code>2</code> in the 2nd position. Following this strange logic, there's no way I can use that call to reliable retrieve the highest numbered folder and therefore the last one created. Something to note is that folder objects that contain files do not have a <code>Last Modified</code> tag that one can use to query.</p>
  <p>To be clear of my question: What call can I use to look through a large amount of S3 objects to find the largest numbered folder object? Preferably the answer is fast, can work with 1000+ objects, and won't require a regex breakdown.</p>