<p>In my namespace, I have several pods named with the same prefix, followed by the random string. There are also other pods, named differently. The result of <code>kubectl get pods</code> would look something like this:</p>
<pre><code>service-job-12345abc
service-job-abc54321
other-job-54321cba
</code></pre>
<p>I need to find the nameof the most recently created pod starting with &quot;service-job-&quot;.</p>
<p>I found <a href="https://stackoverflow.com/questions/51829475/get-name-of-most-recently-created-pod">this thread</a>, which helps getting the name of the most recent pod in general. <a href="https://stackoverflow.com/questions/70282381/kubectl-get-pods-how-to-filter-pods-by-partial-name">This one</a> gets me the complete names of pods starting with a specific prefix.</p>
<p>What I struggle with is combining these two methods. With each one, I seem to lose the information I need to perform the other one.</p>
<p>Note: I am not an administrator of the cluster, so I cannot change anything about the naming etc. of the pods. The pods could also be in any possible state.</p>

<p>This works as you expect:</p>
<pre><code>kubectl get pods --sort-by=.metadata.creationTimestamp --template '{{range .items}}{{.metadata.name}}{{&quot;\n&quot;}}{{end}}' | grep service-job- | head -1 
</code></pre>
