⇤ ← Revision 1 as of 2009-03-18 05:20:23
541
Comment: created by Samus_
|
511
|
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.