0

I want to run less -F command on latest updated log file of one binary (which creates logs with names which start with xtest*) which is in logs directory. I was able to create below alias in csh, but I think I can improve this.

find $LOG/tr/`date +"%Y%m%d"` -name xtest\* -print | xargs ls -rt | tail -1 | xargs less -F
2
  • You have tagged your question with csh. Are you using the csh shell?
    – Kusalananda
    Commented Jul 19, 2018 at 7:52
  • 1
    $ echo $0 -csh so YES I am.
    – BreakBadSP
    Commented Jul 19, 2018 at 7:53

1 Answer 1

1

Now that you are working in cshell then you would know that aliases are supposed to be defined in one line only. hence the alias that is shown overshooting the normal line length. That is cshell for you.

alias latest_log 'find "$LOG/tr/`date +%Y%m%d`" -name "xtest*" -printf "%Ts\t%p\0" | sort -z -k 1,1nr -k 2 | head -z -n 1 | cut -z -f2 | xargs -0 less -F'

Breaking it into chunks to show what it is doing:

  • find command prints , null-separated filenames with the numeric timestamp alongwith the filename. Note that, the quotes in the date command have been taken away for date can run very well without them , plus having them would have made the quoting needlessly wieldy for the alias.
  • The null (\0) separated duos (timestamp TAB filename) are then sorted starting from the first field in the reverse numeric fashion and ending in the second field. The -z option in sort command is to separate the input chunks around the null character rather than the default newline.
  • Once sorted in the proper order, we take out the topmost chunk, which would hold the filename with the latest timestamp by means of the head -z -n 1 command.
  • Then the cut command takes over and strips the timestamp since it's job is done now and it is no longer needed. We use the -z option to tackle the null separated input to cut. The -f2 option shall throw the filename + \0 to the next pipeline.
  • xargs -0 would be reading the filename separated by null and pass the filename to less -F on it's commandline.
1
  • +1 Very nice -z all the way through Commented Jul 22, 2018 at 21:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .