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.
You don't really need to perform mathematical operations on your input argument. By doing this:
The command
latestn 1will go to the newest directory (1st most recent),latestn 2will go to the 2nd most recent directory, etc.This is because
tail -n+xis retrieving lines starting from linex, andhead -1is getting the first one: hence, chaining both is equivalent to getting thexth line.