Increment !:1 value in csh alias

29 Views Asked by At

I'm trying to create a shortcut to find the n'th most recently updated folder at a location in a cshell terminal. Currently I have one to find the latest one.

alias latest    'cd `ls -dt1 -- */ | head -1`'

I'm looking to add an argument so it can calculate the nth most recent folder with something like

alias latestn 'cd `ls -dt1 -- */ | head -[expr !:1 + 1] | tail 1`'

I've tried searching on the site but I can't find an example for performing math on input argument.

1

There are 1 best solutions below

0
norbjd On

You don't really need to perform mathematical operations on your input argument. By doing this:

alias latestn 'cd `ls -dt1 -- */ | tail -n+\!:1 | head -1`'

The command latestn 1 will go to the newest directory (1st most recent), latestn 2 will go to the 2nd most recent directory, etc.

This is because tail -n+x is retrieving lines starting from line x, and head -1 is getting the first one: hence, chaining both is equivalent to getting the xth line.