Find the newest file from shell

Updated . Posted . Visible to the public.

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
Last edit
Arne Hartherz
Keywords
bash
License
Source code in this card is licensed under the MIT License.
Posted by Arne Hartherz to makandra dev (2010-10-13 13:52)