I often want to perform a function on the most recent file in the current directory. Essentially I want a more general version of a method to open last modified file in the directory using vi.
I am able to write a global alias in zsh that does part of what I need:
alias -g lafi='`ls -rt|tail -n 1`'
Now I can execute something like
cat lafi
and I will see the content of the most recent file in the current dir. Or I can issue echo lafi to figure out what the last file was (or I could even say ls -rt|tail -n 1).
Is there a way to modify the alias definition such that it outputs the last file (to STDERR?) and then hands it on like lafi above for further consumption in the commandline?. So for the above cat lafi I would hope for this output.
last file: <name of last-file>
<content of last-file>
I suspect this involves tee but my shell kung fu doesn't cover this in sufficient detail.
Perhaps
alias -g lafi='`ls -rt | tail -n 1 | tee >({ printf "last file: "; cat; } >&2)`'
I think zsh has process substitutions like that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With