Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I find that I often do the following:

%> cd bla/bla
%> ls

I would like it that whenever I cd into a directory it automatically does an ls.

I fiddled with my .bashrc for a while, but couldn't figure out how to make it happen.

share|improve this question
add comment

4 Answers

You can do this with a function:

$ cdls() { cd "$@" && ls; }

The && means 'cd to a directory. If successful (e.g. the directory exists), then run ls'.

$ cdls /var/log
CDIS.custom     fsck_hfs.log    monthly.out     system.log
$ pwd
/var/log

In general, it is a bad practice to rename a command which already exists, especially for a commonly called command like cd. Instead, create a new command with a different name. If you overwrite cd with a function or alias which is also named cd, what would happen when you enter a directory with 100,000 files? There are many utilities which use cd, and they may get confused by this unusual behavior. If you use a shared account (Such as root when you are working with other system administrators), it can be very dangerous to replace an existing command because the environment is different from what people expect.

share|improve this answer
    
That command really change directory? From bash's man page: "There is no mechanism for using arguments in the replacement text. If arguments are needed, a shell function should be used" –  enzotib Sep 9 '11 at 18:58
    
@enzotib : Yes, this really does change directory, at least for me. I updated my answer to show the output of pwd. Not sure if this is a best practice, but it is commonly done. See tldp.org/LDP/abs/html/aliases.html for some examples. –  Stefan Lasiewski Sep 9 '11 at 19:05
1  
First: it does not work here. Second: in that page they use variables, not positional parameters. Third: ABS is a common source of bad practices. –  enzotib Sep 9 '11 at 19:07
    
Ok fine, I added a function also. Maybe ABS is full of bad practices (Some people say this about shell scripting, in general), but at least they are advanced bad practices. –  Stefan Lasiewski Sep 9 '11 at 19:10
1  
The alias works for me on Snow Leopard but not on CentOS5 or CentOS6. I updated my answer to use a function only. No aliases. –  Stefan Lasiewski Sep 10 '11 at 0:37
show 3 more comments

I have this in my .bashrc, and it works fine.

function cd {
    builtin cd "$@" && ls -F
    }

Earlier in my .bashrc I have: [ -z "$PS1" ] && return, and everything after that line only applies to interactive sessions, so this doesn't affect how cd behaves in scripts.

share|improve this answer
add comment

off-topic, since the question is tagged /bash, but as some questions are closed as duplicate of this one that don't mention bash:

With zsh:

chpwd() ls

The chpwd() function is called by zsh whenever the current directory changes (by way of cd, pushd, popd...). tcsh has a similar feature and is probably where zsh got it from.

share|improve this answer
add comment

In bash you cannot recur to aliases for action that require parameter. For this there are functions. So put in your ~/.bashrc the following

mycd() {
  cd "$1"
  ls
}
share|improve this answer
4  
cd "$1" && ls would be better. –  Gilles Sep 9 '11 at 23:39
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.