Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

global alias for last file in current directory

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.

like image 285
DrSAR Avatar asked Dec 01 '25 10:12

DrSAR


1 Answers

Perhaps

alias -g lafi='`ls -rt | tail -n 1 | tee >({ printf "last file: "; cat; } >&2)`'

I think zsh has process substitutions like that.

like image 139
glenn jackman Avatar answered Dec 04 '25 17:12

glenn jackman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!