where is `cd` located?
Solution 1
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.
Keywords?
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
#!/bin/sh
builtin cd "$@"
But it doesn't do what you think. See these other U&L Q&A's for more details:
- What is the point of the `cd` external command?
- "Why can't I redirect a path name output from one command to "cd"?
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.
Solution 2
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.
Solution 3
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
/bin/chown
/home/durrantm/.rvm/gems/ruby-1.9.3-p194/doc/rubyzip-0.9.8/ri/Zip/ZipFileSystem/ZipFsFile/chown-i.ri
/usr/lib/pt_chown
/usr/share/man/man2/fchown32.2.gz
/usr/share/man/man2/fchownat.2.gz
/usr/share/man/man2/lchown.2.gz
/usr/share/man/man2/lchown32.2.gz
/usr/share/zsh/functions/Completion/Unix/_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.
Solution 4
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.
Solution 5
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.
Related videos on Youtube
Tony Anderson
Automation Engineer Deving all of the Ops in Lehi, Utah. Primarily focused in: Puppet Chef Docker #SOreadytohelp
Updated on September 18, 2022Comments
-
Tony Anderson over 1 year
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 /bin/ls
Where is the
cd
command located? And why is different from all the other commands?Update
Another interesting tidbit, cd does not show up in
hash
hash 0 /bin/ls 2 /usr/bin/find 2 /sbin/ip 1 /usr/bin/updatedb 1 /usr/bin/apt-get
-
Fernando over 10 yearsCommands like
cd
andhash
are internal to the shell because those commands need to access internal data of the shell process or change its status. -
Gilles 'SO- stop being evil' over 10 yearsIt'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. -
jay over 10 yearsto 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'"
-
-
Tony Anderson over 10 yearsOn my system, I get the following when I use type cd cd is a shell builtin
-
slm over 10 yearsTry and avoid using
whereis
. It's best to usetype
. -
Graeme over 10 yearsWhat does
/bin/cd
do? Surely it can't change the working directory of its parent process? -
slm over 10 years@Graeme - See updates, it's a script.
-
Alen Milakovic over 10 years@slm citation needed. :-)
-
slm over 10 yearsCitation as requested: unix.stackexchange.com/questions/85249/…
-
Hauke Laging over 10 yearsThis 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.
-
mirabilos over 10 yearsThis answer doesn’t explain why precisely
cd
must be a built-in utility. (But the article Gilles linked does.) -
mirabilos over 10 yearsThis answer doesn’t explain why precisely
cd
must be a built-in utility. (But the article Gilles linked does.) -
mirabilos over 10 yearsThis answer doesn’t explain why precisely
cd
must be a built-in utility. (But the article Gilles linked does.) -
mirabilos over 10 yearsThis 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 GNUbash
for the manual page. -
slm over 10 yearsThis 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! -
jasonwryan over 10 years@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...
-
mirabilos over 10 yearsYes, but good answers would reasonably explain about the why. (Note I didn’t downvote, just comment.)
-
jasonwryan over 10 years@mirabilos Perhaps. I originally was going to just post as a comment; exegesis of
man
pages is not generally my thing... -
OmPS over 10 yearsthanks, 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.
-
nyuszika7h about 10 yearsHow is it ridiculous? Ubuntu does limit
ptrace
by default (see/proc/sys/kernel/yama/ptrace_scope
).