Find the answer to your Linux question:
Results 1 to 6 of 6
the problem is that i want to alias my cd cmd in such a way that whensover i enter any directory, a ls cmd i automatically performed. i tried ' ...
  1. #1
    Just Joined!
    Join Date
    Mar 2010
    Posts
    4

    Lightbulb aliasing cd and ls

    the problem is that i want to alias my cd cmd in such a way that whensover i enter any directory, a ls cmd i automatically performed.
    i tried ' alias cd='cd $1;ls' , but it is not working.
    please help...

  2. #2
    Linux Enthusiast scathefire's Avatar
    Join Date
    Jan 2010
    Location
    Western Kentucky
    Posts
    616
    i do believe you have to call it something else, you can't alias an actual command that exists. so you can't do what you are doing, but this would work:

    Code:
    alias lmao='cd $1;ls'
    lmao /home
    linux user # 503963

  3. #3
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,970
    Quote Originally Posted by scathefire View Post
    i do believe you have to call it something else, you can't alias an actual command that exists. so you can't do what you are doing, but this would work:

    Code:
    alias lmao='cd $1;ls'
    lmao /home
    I've done some playing. The solution above won't work either. It seems that the aliased command will ignore the argument for all but the last item on the command line, so effectively what the alias does is this:

    cd ; ls

    Returning you to your home directory.

    If you were to do this:

    alias cd='ls $1 ; cd $1'

    then the command cd somedirectory would list your current directory, but leave you in the one specified. IE, it behaves like you did this:

    ls ; cd somedirectory

    The upshot of all this is that I don't think you can do what you want. Some may call this a bash bug, and others might just shrug and say "it has always been that way"...

    However, there IS a solution, though you can't use the name 'cd' - create a shell procedure, call it tcd, in your ~/.bashrc file. It would be defined like this:
    Code:
    tcd() {
    cd $1
    ls
    }
    alias cd='tcd $1'
    Note that you can then alias the function tcd() to cd and it will work like you want! You can't use 'cd' for the function name directly. If you do that, you end up with an endlessly recursing function - CRASH!
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  4. #4
    Linux Enthusiast scathefire's Avatar
    Join Date
    Jan 2010
    Location
    Western Kentucky
    Posts
    616
    the aliasing i talked about worked fine on one of my centos servers. but here is the thing, once you log out the alias disappears (only good for one session). in order to make it permanent, you'd have to add it to the user's bashrc. system-wide aliases must be added to /etc/bashrc and require a restart
    linux user # 503963

  5. #5
    Linux Guru Lazydog's Avatar
    Join Date
    Jun 2004
    Location
    The Keystone State
    Posts
    2,281
    scathefire's alias worked on my system with out issues. One thing you can do is add the alias that was given by scathefire amd then add another alias for cd that points to what ever you called the first alias. Some thing like;

    Code:
    alias lmao='cd $1;ls'
    alias cd=lmao
    Now when you use the cd command you get the effect you are looking for.

    Regards
    Robert

    Linux
    The adventure of a life time.

    Linux User #296285
    Get Counted

  6. #6
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    scathefire's alias does not work for me (Ubuntu 9.10). Rubberman's solution does, but it can be simplified even further:
    Code:
    function cd() {
        builtin cd "$1"
        ls
    }
    The "builtin" command was designed for exactly this purpose: it allows you to access a shell builtin even if the command itself has been redefined. As you can see, this allows you to avoid using an alias.
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...