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 ' ...
- 03-12-2010 #1Just Joined!
- Join Date
- Mar 2010
- Posts
- 4
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...
- 03-12-2010 #2
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:
lmao /homeCode:alias lmao='cd $1;ls'
linux user # 503963
- 03-15-2010 #3Linux Guru
- 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
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:
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!Code:tcd() { cd $1 ls } alias cd='tcd $1'Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 03-15-2010 #4
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
- 03-16-2010 #5
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;
Now when you use the cd command you get the effect you are looking for.Code:alias lmao='cd $1;ls' alias cd=lmao
- 03-16-2010 #6
scathefire's alias does not work for me (Ubuntu 9.10). Rubberman's solution does, but it can be simplified even further:
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.Code:function cd() { builtin cd "$1" ls }DISTRO=Arch
Registered Linux User #388732


Reply With Quote
