Get the name of the newest file in a directory on the linux shell

If you have a directory with many files (a few thousand in my case) and need to know which one is the newest, then the following command might be useful. It returns the name and details of the newest file in a directory where it is executed:

ls -tl | sed -n 2p

facebooktwittergoogle_plusredditlinkedinmail

4 thoughts on “Get the name of the newest file in a directory on the linux shell”

  1. Khurram says:

    how about ‘ls -ltr’ ?

  2. James Cage says:

    And how to get the oldest file in a directory?

    1. Dhruv says:

      ls -t | tail -n 1

      This will give you the oldest file or directory in the current working directory.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>