Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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

vim $( latest )

See man bash.

share|improve this answer
    
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. –  Chris O'Kelly Jan 3 '13 at 2:51

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.

share|improve this answer
    
Pagers were worth me finding out about, even if I went Waltinator's way here. Thanks for the answer –  Chris O'Kelly Jan 3 '13 at 2:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.