Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


share|improve this question

migrated from 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 and here – 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

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
       echo "Could not find log file: ${dir}/${dir}_${mydate}.log"

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

You must log in to answer this question.