Differences between revisions 1 and 2
Revision 1 as of 2009-03-18 05:20:23
Size: 541
Editor: localhost
Comment: created by Samus_
Revision 2 as of 2009-03-18 05:24:18
Size: 511
Editor: localhost
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
Describe BashFAQ/099 here.

How can I get the newest file from a directory?

Parsing the output of 'ls' is a bad practice, you should create a loop and compare the timestamps:

saved_ts=0
for f in *; do
  f_ts=$(stat --format=%Y "$f")

  if ((f_ts > newest_ts)); then
    newest_ts=$f_ts
    newest_f=$f
  fi
done

there you'll have the newest file at $newest_f, notice that when a file changes also changes its timestamp so this will get the last created or modified file in the directory.

BashFAQ/099 (last edited 2016-03-23 09:25:56 by geirha)