I would like to create an alias in bash where if I type 'logs' it should take me to the latest log file. My folder is structured such that ~/logs/date/time. After googling for sometime I found out the below command and it is working fine if i give in the bash prompt
cd ~/logs && cd `ls -tr | tail -1` && cd `ls -tr | tail -1`
alias logs="cd ~/logs && cd `ls -tr | tail -1` && cd `ls -tr | tail -1`"
The problem is that backquoted commands are evaluated at once when defining the alias.
Either escape them:
alias logs="cd ~/logs && cd \`ls -tr | tail -1\` && cd \`ls -tr | tail -1\`"
Or a function, that works too and is more readable, so you can add robustness more easily as Charles suggested (when creating an alias, you just do a quick & hack job):
logs()
{
cd ~/logs || return
cd "`ls -tr | tail -1`" || return
cd "`ls -tr | tail -1`" || return
}