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.

This question already has an answer here:

In a bash sub shell I get the following error when running cd

sudo: cd: command not found

This is expected because I don't have a path. Usually to work around this I just provide the full path like so: (/usr/local/bin/foo)

Much to my surprise, cd does not appear to be in any of the normal places.

which cd
whereis cd
ls /bin | grep cd

By comparison, ls is right where I would expect.

which ls

Where is the cd command located? And why is different from all the other commands?


Another interesting tidbit, cd does not show up in hash

0   /bin/ls
2   /usr/bin/find
2   /sbin/ip
1   /usr/bin/updatedb
1   /usr/bin/apt-get
share|improve this question

marked as duplicate by Anthon, vonbrand, Timo, rahmu, jasonwryan Feb 27 '14 at 16:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Commands like cd and hash are internal to the shell because those commands need to access internal data of the shell process or change its status. –  Fernando Feb 26 '14 at 4:17
It's a builtin. As for why, see Why is cd not a program? Though some systems do have a cd external command, of very limited utility. –  Gilles Feb 26 '14 at 10:12
to answer your reason for asking but not your question, 'sudo cd /etc' isn't terribly useful anyway as it would switch back to the original directory when the sudo ended. if you need to run a command in a specific directory you will have to switch there before running the sudo command. if you don't have execute permission on that folder (or a parent), you can run link sudo and su -c as follows: "sudo su -c 'cd /root; ls'" –  jay Feb 26 '14 at 17:53

6 Answers 6

up vote 31 down vote accepted

What cd am I using?

If you're in Bash cd is a builtin. The type command even bears this out:

$ type -a cd
cd is a shell builtin
cd is /usr/bin/cd
cd is /bin/cd

The system will use the first thing in this list, so the builtin will be the preferred option, and the only one that works (see the section below on What is /bin/cd).

What's a builtin?

I like to think of builtins as functions that Bash knows how to do itself. Basically anything that you use a lot has been moved into the Bash "kernel" so that it doesn't have to go executing a process for each time.

You can always explicitly tell Bash that you want a builtin by using the builtin command like so:

$ builtin cd

See the help about builtin:

$ help builtin

Why isn't cd in hash?

The hash is meant only to "hash" (aka. "save" in a key/value pair) the locations of files, not for builtins or keywords. The primary task for hash is in saving on having to go through the $PATH each time looking for frequently used executables.


These are typically the commands that are part of Bash's programming language features.

$ type while
while is a shell keyword
$ type for
for is a shell keyword
$ type !
! is a shell keyword

Some things are implemented in multiple ways, such as [:

$ type -a [
[ is a shell builtin
[ is /usr/bin/[
[ is /bin/[    

...and cd as you've discovered.

What is /bin/cd?

On my Fedora 19 system /bin/cd is actually a shell script:

$ more /bin/cd
builtin cd "$@"

But it doesn't do what you think. See these other U&L Q&A's for more details:

Bottom line:

POSIX's requires that it's there and in this implementation, it acts as a test, confirming that you're able to change directories to X, but then returning a return code confirming or denying that this is possible.

share|improve this answer
What does /bin/cd do? Surely it can't change the working directory of its parent process? –  Graeme Feb 26 '14 at 13:23
@Graeme - See updates, it's a script. –  slm Feb 26 '14 at 14:00
"the builtin will be the preferred option" And the only one that works. –  Kevin Feb 26 '14 at 15:35
This answer doesn’t explain why precisely cd must be a built-in utility. (But the article Gilles linked does.) –  mirabilos Feb 27 '14 at 13:19
This is the link that mirabilos is referring to titled: Why is cd not a program?. The reasons are explained in the other 2 links I've provided, but Jlliagre's answer to the link in this comment explains exactly why cd cannot be a program! –  slm Feb 27 '14 at 13:26

It is a builtin. See man bash for the details of cd and the Bash Manual for a description of builtins:

Builtin commands are contained within the shell itself. When the name of a builtin command is used as the first word of a simple command (see Simple Commands), the shell executes the command directly, without invoking another program. Builtin commands are necessary to implement functionality impossible or inconvenient to obtain with separate utilities.
share|improve this answer
This answer doesn’t explain why precisely cd must be a built-in utility. (But the article Gilles linked does.) –  mirabilos Feb 27 '14 at 13:19
@mirabilos For the simple reason that the OP didn't know it was a builtin so couldn't possibly have asked why it must be a builtin; ergo I didn't include that in my answer... –  jasonwryan Feb 27 '14 at 16:20
Yes, but good answers would reasonably explain about the why. (Note I didn’t downvote, just comment.) –  mirabilos Feb 27 '14 at 16:22
@mirabilos Perhaps. I originally was going to just post as a comment; exegesis of man pages is not generally my thing... –  jasonwryan Feb 27 '14 at 16:24

type and whereis can show you that, e.g.

For grep:

$ type grep
grep is /bin/grep

For chown:

$ whereis chown
chown: /bin/chown /usr/share/man/man2/chown.2.gz /usr/share/man/man1/chown.1.gz

locate can also be useful for showing related files based on a wildcard search, e.g. for the chown command:

$ locate chown

Finally, when the result is that the command is 'builtin', as you saw for cd it means that the code for it is actually in the bash main program and not a different program located elsewhere on disk.

share|improve this answer
On my system, I get the following when I use type cd cd is a shell builtin –  spuder Feb 26 '14 at 1:24
Try and avoid using whereis. It's best to use type. –  slm Feb 26 '14 at 3:11
@slm citation needed. :-) –  Faheem Mitha Feb 26 '14 at 15:09
Citation as requested: unix.stackexchange.com/questions/85249/… –  slm Feb 26 '14 at 15:13
This answer doesn’t explain why precisely cd must be a built-in utility. (But the article Gilles linked does.) –  mirabilos Feb 27 '14 at 13:21

cd is built-ins function for shells, for eg. bash, csh, ksh.

There are many built-ins functions shells support, you can check them using the man bash command.

share|improve this answer
This answer doesn’t explain why precisely cd must be a built-in utility. (But the article Gilles linked does.) Additionally, it’s of low quality, for example restricted to GNU bash for the manual page. –  mirabilos Feb 27 '14 at 13:22
thanks, but i never thought 'cd' would otherwise be an external command, i was just trying to be simple, rather precise, will take care next time. –  OmPS Feb 28 '14 at 15:40

cd is a shell built-in function, as others have already mentioned:

$ type cd
cd is a shell builtin

This is because a process can't change the working directory of another process without using ptrace() (which gdb uses, for example). That would be an unnecessary overhead. Furthermore, its use is limited by some distributions such as Ubuntu.

share|improve this answer
How is it ridiculous? Ubuntu does limit ptrace by default (see /proc/sys/kernel/yama/ptrace_scope). –  nyuszika7h Apr 21 '14 at 18:52

Just like DOS' internal and external commands. Simple commands are implemented in the shell (most commonly command.com). More complex and less used commands are implemented in separate executable files to reduce the command interpreter's complexity and memory consumption, which then are external commands.

share|improve this answer
This is not mainly about complexity. It simply doesn't make sense to use external commands for certain tasks because their result cannot affect the shell. One example is changing the working directory of the shell. –  Hauke Laging Feb 27 '14 at 2:13

protected by slm Feb 27 '14 at 6:40

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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