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 want short way to access files, which i usually use in bash. For example, see logs. Now i use:

less /var/log/user/server.log
less /var/log/user/server2.log

I want shortcut, to do

less server.log
less server2.log

I try this variant in .bash_profile, but have mistake somewhere:

less() { if [[ $@ == "server.log" ]]; then command less /var/log/user/server.log; else command less "$@"; fi; }

Thanks in advice!

share|improve this question

migrated from stackoverflow.com May 13 '13 at 12:12

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

4 Answers 4

up vote 5 down vote accepted

you could symlink the files like so:

ln -s /var/log/user/server.log server.log
ln -s /var/log/user/server2.log server2.log

And then you can do whatever you want with these files as:

less server.log
tail -f server2.log
share|improve this answer
serverlog=/var/log/user/server.log
serverlog2=/var/log/user/server2.log

less $serverlog
share|improve this answer

I got bored so I wrote this bash shell function for you:

less()
{
    OIFS="$IFS"; IFS=:
    lesspath=('' $LESSPATH)
    IFS="$OIFS"
    for arg ; do
            for path in "${lesspath[@]}" ; do
                    file="${path:+$path/}${arg}"
                    if [[ -e "${file}" ]] ; then
                            command less "${file}"
                            continue 2
                    fi
            done
            command less "${arg}"
    done
}

Put this shell function into your .bashrc, set LESSPATH=/var/log/user and now you can run less as you want to.

You can add other paths to LESSPATH by separating them with colons, just like the other PATH environment variables. So, if you set LESSPATH=/var/log:/var/log/user, you could also easily run less syslog from anywhere to easily see /var/log/syslog.

share|improve this answer

To use aliases you could do something like this too..

add this to .bashrc

function lss { less "$1"; }
export lss
alias lf='echo /var/log/syslog'

run

. ~/.bashrc

Now you can do :

less $(lf)

or

lss $(lf)
share|improve this answer

Your Answer

 
discard

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