Home > Software engineering >  Easiest way to "tail" the latest out file
Easiest way to "tail" the latest out file

Time:04-26

At work, I run many simulations from the command line, and I want to capture the output into a file, but also to continue to see the output in the command line. My current workflow is to run something like this:

run sim_0425 > sim_0425.log & tail -f sim_0425.log

This does exactly what I want, but the issue is that the logfile will be named something different each time depending on the simulation parameters (for example, here it's "sim_0425"), and it's kinda kludgey to manually rename it in all locations each time. I'll often forget to rename it one of the locations, and then I'll rewrite a file somewhere.

I can't imagine that there isn't some built-in way to output to a file and to the std out stream simultaneously. Googling this is challenging because I'm basically searching for "> & tail" and that doesn't turn much up :/

CodePudding user response:

You can use the tee command for this (if you have it installed, which is likely; it's part of the Posix standard). tee takes its standard input and writes (or appends) it to one or more files, and also sends it to stdout:

run sim_0425 | tee sim_0425.log | tail -f

If you want to append to the log file instead, use the -a option:

run sim_0425 | tee -a sim_0425.log | tail -f
  •  Tags:  
  • bash
  • Related