8

I have found myself keep doing cd some_dir quickly followed by ls for quite some time now, and have been trying to write a bash alias to let me do this, such as:

alas cd="cd $@; ls";

Problem with this is you can't have an input argument in the middle of an alias (correct me if I'm wrong).

I've also tried defining my own shell function as suggested by this page:

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

But the problem with this one is if I try to use it like so:

cd Documents

the shell exits with [Process completed], rendering the shell useless... So how should I get around this?

4
  • 1
    I'd love a shell that had alas as a builtin.
    – Rob Davis
    Nov 17, 2012 at 19:45
  • I recommend you don't do this. Make your alias named "cdl" or something else. Otherwise, you'll get into this habit. Worse: you could encounter times when you wouldn't want to ls. e.g., when handling a remote filesystem, it may be slow. When running make, a script might cd a lot, and there's no point in slowing the script down. Simply, out of habit, you're best off trying to not conflict with widely deployed native commands you know about.
    – TOOGAM
    Jun 13, 2016 at 18:53
  • @RobDavis What is this "alas" program of which you spoke of? (I realize it's been over three and a half years since you made that comment.) Is this a third party program with a home page?
    – TOOGAM
    Jun 13, 2016 at 18:56
  • @TOOGAM Given that Rob Davis appears to no longer have an active account, you probably won't get an answer from him. But I suspect he was referring to the meaning of the word "alas" as an expression of sorrow and not any specific program.
    – 8bittree
    Jun 13, 2016 at 19:05

3 Answers 3

20

Try this:

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

builtin makes the cd inside the function invoke the builtin cd command rather than trying to call the function recursively.

8
  • Is recursion actually a risk? I do alias ls='ls --color', and it doesn't recurse. Nor does 'alias rm='rm -i'
    – David Yaw
    Nov 17, 2012 at 13:53
  • 5
    Those are aliases, not a functions. They don't work the same way. Try foo() { echo hello ; foo; }; foo; (have your Ctrl-C ready).
    – Mat
    Nov 17, 2012 at 13:54
  • Ah, quite right, wasn't paying enough attention.
    – David Yaw
    Nov 17, 2012 at 13:58
  • 4
    Might be better to have builtin cd "$@" && ls; instead so that ls doesn't run if there is problem entering the directory (non-existent etc.), like what John Ledbetter mentioned in his answer.
    – doubleDown
    Nov 17, 2012 at 14:13
  • 1
    @anishsane: a recursive function can eat up ram, yes. This answer isn't recursive though.
    – Mat
    Nov 19, 2012 at 14:08
3

I use a Bash function to automatically do an ls after I cd:

# Automatically do an ls after each cd
cd() {
  if [ -n "$1" ]; then
    builtin cd "$@" && ls --group-directories-first
  else
    builtin cd ~ && ls --group-directories-first
  fi
}

Just add the above to your ~/.bashrc

Personally I add it to my ~/.bash_functions (which is sourced by my ~/.bashrc)

2

In GNU Bash,

PROMPT_COMMAND=ls
1
  • While this technically answers the question, as in it performs ls after a cd, it achieves this by naively running ls after every command regardless of whether it's cd or not May 20, 2023 at 10:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .