简体   繁体   中英

How to follow latest log file with tail -f?

I have a dir that saves a fresh log file for each run, so I use the following command to follow the latest log file:

tail -f $(ls -t | head -1)

So my question would be if there is any way to jump from one file to the next, if there is a newer log file available without having to stop the latest tail -f and rerunning it?

What you could attempt is something like this:

# setup a link that points to the youngest file    
$ for f in *; do [[  ( ! -f last )  || ( "$f" -nt last) ]] && ln -sf "$f" last; done
# monitor that file continuously by filename
$ tail -F last

run the following in another shell

while :; for f in *; do [ "$f" -nt last ] && ln -sf "$f" last; done

You can also run this in a single command as:

while :; do for f in *; do [[  ( ! -f last )  || ($f -nt last) ]] && ln -sf $f last; done; done & tail -F last

The -F flag for tail is supported in the GNU version. (It is in the GNU CoreUtils collection.)

Apparently, IBM have provide download for the GNU Toolkit for AIX , and apparently it includes a version of tail that supports the -F option.

You could use that in conjunction with the other answers to this question. (For example, using tail -F with a symlink that you refresh regularly.)

Alternatively, if none of those solutions work for you, you could get hold of the GNU CoreUtils source code 1 , and modify the source for tail to do what you want, and build it yourself on AIX.


1 - I haven't checked, but I expect that the IBM devs will be contributing back any changes they make to CoreUtils to get it to work on AIX. If not, they are obligated to make the source code available to you on request. Either way, getting hold of AIX compatible source code should not be a problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM