Most of the time when I do a cd
command I also do a ls
to see what other files or folder are in that directory. So I want to execute ls
everytime I change the directory. I know I can do cd foo && ls
but is it really necessary to do && ls
everytime?
There are 2 ways to do this. You can create an alias as LnxSlck posted or another one is creating a function in .bash_aliases
I use the following, which doesn't involve adding any new commands:
This makes
cd
work the same as before, except that there's also anls
.Note the following implications: The other examples don't permit arguments to
cd
. While admittedly those arguments are uncommon, this is a 100% compatible drop-in replacement. And because it returnscd
's exit status, it won't break scripts.There's one caveat here. If you ever don't want this functionality, you have to call
builtin cd
. But my experience in using this function for many years is that such occurrances are rare, occurring primarily in scripts, and the convenience of not having to type something long likemycd
every time you want to change directories greatly outweighs the minor drawback of typingbuiltin cd
once in a blue moon.You can make an
alias
forcd
, and add it to your *bash_profile* or bashrc, something like:put that in your
.bashrc
. It is however recommended that you store all your aliases in~/bash_aliases
file, so create the file usingtouch ~/.bash_aliases
if it's not already present and use it to store all your aliases.