Read more

Find the newest file from shell

Arne Hartherz
October 13, 2010Software engineer at makandra GmbH

This can be helpful when you need the latest file inside a directory for processing in a shell script:

ls -1tr * | tail -1

Used switches

Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

The -1 switch makes ls return 1 file per line, -t orders by modification time and -r causes sorting from oldest to newest. tail -1 then returns the last line, containing the newest file's name.

If you require only a subset of files, adjust the "*" mask accordingly or use other switches of ls.

Posted by Arne Hartherz to makandra dev (2010-10-13 15:52)