In shell, how can I tail the latest file created in a directory?
| |||||||
feedback
|
migrated from stackoverflow.com Mar 8 '10 at 19:17
This question came from our site for professional and enthusiast programmers.
|
No no no! Do not parse the output of ls! If you must do this I would recommend find
Or anything that isn't ls. Parsing the output of ls is difficult and unreliable. EDIT: added -maxdepth 1 per the comments. Don't code in haste! | |||||||||||||||||||||
feedback
|
If you're worried about filenames with spaces,
| |||||||||
feedback
|
|
On POSIX systems, there is no way of getting the "last created" directory entry. Each directory entry has So the best you can get is to "tail the last recently modified file", which is explained in the other answers. I would go for this command: tail -f "$(ls -tr | sed 1q)" Note the quotes around the | |||||
|
feedback
|
|
You can use:
The The output of that command (the most recent file) is then passed to Keep in mind this runs the risk of getting a directory if that's the most recent directory entry created. I've used that trick in an alias to edit the most recent log file (from a rotating set) in a directory that contained only those log files. | |||||||||||
feedback
|
|
There are probably a million ways to do this, but the way I would do it is this:
The bits between the backticks (the quote like characters) are interpreted and the result returned to tail.
| |||
|
feedback
|
|
I have a stupid program which just creates a new logfile each night with names like 2010-04-12.log I want to have this log scrolling indefinitely in a window over the nightly "logchanges" (Can't call it rotation, as there would always be a newest.log, which tail -F could tail.) So I in crontab one minute past rotation time (at 1 and 2 at night because my timezone is UTC+1, UTC+2 DST) I do
ln -sF Then I can do tail -F newest.log I might miss a minute of log, but then I could run at zero minutes and add a sleep 5 sec or so before the link. | |||
|
feedback
|
|
Someone posted it, and then erased it for some reason, but this is the only one that works, so...
| |||||||||
feedback
|
Explanation:
| |||||||||
feedback
|
