Find the newest file from shell

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

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.

Arne Hartherz