Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am very new to Linux. I just found the alias command which is very useful as I quite often have to dig through log files

So I need to regularly open text files they are generally stored in the format ...\somename\somename_yyyymmdd.log

how can I create a dynamic alias that can take the current folder name as input as well as the current date to create a command like? less somename_20121024.log

Thanks

share|improve this question

migrated from stackoverflow.com Oct 24 '12 at 15:52

This question came from our site for professional and enthusiast programmers.

    
This can't be done with symlinks (= alias) only, you have to write a shell script for this (or a script in any other programming language). Have a look here en.wikipedia.org/wiki/Symbolic_link and here en.wikipedia.org/wiki/Shell_script –  23tux Oct 24 '12 at 8:07
    
zshell uses also dynamic aliases, with bash and others it will fill alias variables on logon, with zshell it will load it on call –  Oliver Stutz Oct 24 '12 at 8:18

2 Answers 2

The alias command is for making shortcuts for regularly issued commands. The most commonly used linux shell, bash, does not support parameter expansion within aliases, which makes it a little less suitable for your purpose. What you're trying to do is better accomplished using a function. This one is in bash:

lesslog() {
   local dir=$1
   local mydate=${2:-$(date +%Y%m%d)}
   if [[ -f ${dir}/${dir}_${mydate}.log ]]; then
       less ${dir}/${dir}_${mydate}.log
   else
       echo "Could not find log file: ${dir}/${dir}_${mydate}.log"
   fi
}

This function will look for the logfile in the directory passed. If you don't pass in a second parameter, it will default to today's date.

share|improve this answer
    
great, thanks. this looks exactly like what I was looking for –  user1734278 Oct 24 '12 at 10:40

If you are using Bash you can use functions in your ~/.bashrc for this. For example:

function less_log() {
  less log-`date +%Y%m%d`
}
export -f less_log

export -f means that you export a function.

share|improve this answer
    
thanks. those links were pretty helpful as I am very new to this –  user1734278 Oct 24 '12 at 10:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.