Thursday, May 9, 2024
 Popular · Latest · Hot · Upcoming
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 1034  / 2 Years ago, wed, october 5, 2022, 9:11:02

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?


More From » 10.04

 Answers
3

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



vim $( latest )


See man bash.


[#33401] Wednesday, October 5, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hirtieve

Total Points: 207
Total Questions: 104
Total Answers: 114

Location: Cook Islands
Member since Thu, May 21, 2020
4 Years ago
hirtieve questions
;