How Do I Grep the Latest File in Unix?
Contents

How do I grep the latest file?

How it works:

  1. find /var/log/folder -type f -printf ‘%[email protected] %p ’ This looks for files and prints their modification time (seconds) followed by a space and their name followed by a nul character.
  2. sort -rz. This sorts the null-separated data.
  3. sed -Ezn ‘1s/[^ ]* //p’ …
  4. xargs –null grep string.

How do I find the latest file in Unix?

Get most recent file in a directory on Linux

  1. watch -n1 ‘ls -Art | tail -n 1’ – shows the very last files – user285594 Jul 5 ’12 at 19:52.
  2. Most answers here parse the output of ls or use find without -print0 which is problematic for handling annoying file-names.
Robert Thorne

Robert Thorne

Automotive & Future Transportation Editor

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.

Share this article
Twitter Facebook Pinterest