6

I accidentially destroyed my cd command.

I tried to automatically execute ls after cd is called.

I found a post saying that I have to execute alias cd='/bin/cd && /bin/ls', but now I get -bash: /bin/cd: No such file or directory and can't change directoy anymore.

9
  • 17
    There is no /bin/cd; cd is a shell built-in. Commented Jun 25, 2018 at 14:32
  • Enjoy unix.stackexchange.com/questions/354123 and what it hyperlinks to M. Dalton. (-:
    – JdeBP
    Commented Jun 25, 2018 at 14:41
  • 3
    A POSIX compliant platform has a /bin/cd, but that only exists for formal reasons and is not usable for anything.
    – schily
    Commented Jun 25, 2018 at 14:41
  • 1
    Wow /bin/cd must be the most useless command. Commented Jun 25, 2018 at 17:15
  • cd isn't a file on disk, It's a shell built-in command:type cd Commented Jun 25, 2018 at 17:26

3 Answers 3

23

Your system (like many Unix systems) does not have an external cd command (at least not at that path). Even if it had one, the ls would give you the directory listing of the original directory. An external command can never change directory for the calling process (your shell)1.

Remove the alias from the environment with unalias cd (and also remove its definition from any shell initialization files that you may have added it to).

With a shell function, you can get it to work as cd ordinarily does, with an extra invocation of ls at the end if the cd succeeded:

cd () {
    command cd "$@" && ls -lah
}

or,

cd () { command cd "$@" && ls -lah; }

This would call the cd command built into your shell with the same command line arguments that you gave the function. If the change of directory was successful, the ls would run.

The command command stops the shell from executing the function recursively.

The function definition (as written above) would go into your shell's startup file. With bash, this might be ~/.bashrc. The function definition would then be active in the next new interactive shell session. If you want it to be active now, then execute the function definition as-is at the interactive shell prompt, which will define it within your current interactive session.


1 On systems where cd is available as an external command, this command also does not change directory for the calling process. The only real use for such a command is to provide POSIX compliance and for acting as a test of whether changing directory to a particular one would be possible.

10
  • Where do I have to put the function?
    – Black
    Commented Jun 25, 2018 at 14:37
  • 1
    @Black You may put it wherever you would have put the alias definition. In a bash shell, that would probably be in ~/.bashrc.
    – Kusalananda
    Commented Jun 25, 2018 at 14:38
  • I added the function to ~/.bashrc but it does not work. ls -lah is not executed at all.
    – Black
    Commented Jun 25, 2018 at 14:41
  • @Black No, you add it exactly the way I wrote it. Then it will be active in any new shell session.
    – Kusalananda
    Commented Jun 25, 2018 at 14:41
  • 1
    Note that POSIXly, you'd use cd() { command cd "$@" && ls -lah; }, though that wouldn't work with zsh except in sh emulation. Not all shells have a builtin builtin. In ksh93, it does something different. Commented Jun 25, 2018 at 16:30
7

I was able to solve it by removing the alias again with unalias cd

2
  • If you remove the stuck-out text, then this is the only answer that answers the question. Commented Jun 25, 2018 at 17:18
  • 1
    @ctrl-alt-delor Note there's no question in the question. We could only guess what the implicit question is. In this situation "why?" is as good as "how to revert?" Commented Jun 26, 2018 at 10:26
5

That happened because:

$ type cd

cd is a shell builtin

You must log in to answer this question.

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