2

I set up an alias this morning to help finding the latest log file in a directory. My alias looks like

alias latest="ls -lat | grep ^- | awk '{print \$8}' | head -1"

In my research so far this morning I've read a blog - ParsingLS which tells me this is probably not the best way to do it - let's ignore that for now, I can look at making a better alias using find later. For now assume I have a perfectly working command expected to output a filename from the current directory.

I thought I was going to be able to do latest | vi, to open the latest modified file in the directory, but it seems this won't work - vi complains that the output is not a terminal. I saw a stackoverflow question which suggested the issue is related to subshells and piping and so tried

latest | vi < `tty` > `tty`

But that just gets me

`tty`:ambiguous redirect

I'm sure there's something basic I am not getting about pipes here - I only recently started trying to use them in earnest, any ideas?

2 Answers 2

4

It's simple. If latest produces a filename on stdout, $() is the answer:

vim $( latest )

See man bash.

1
  • facepalm I dunno how I managed to miss this - I use that syntax all over the place in scripts, but never even thought of it 'at the prompt'. Thanks very much, this works perfect. Jan 3, 2013 at 2:51
1

I assume a pager is not sufficient?

I don't think vi has support for this (at least not nvi or recent FreeBSD vi), vim does however:

latest | vim -

One workaround is to use a named pipe:

mkfifo vi.fifo
latest > vi.fifo & vi vi.fifo
rm vi.fifo

Not very pretty and FreeBSD vi seems to need an extra :e before it reads the fifo.

1
  • Pagers were worth me finding out about, even if I went Waltinator's way here. Thanks for the answer Jan 3, 2013 at 2:50

You must log in to answer this question.

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