Which package is the command 'cd' in? [duplicate]

I want to look into the source code behind the command cd, but cd doesn't seem to have its own package so which package is it in?


Solution 1:

Find out what type a command is using the type command.

$ type cd
cd is a shell builtin
$ type ls
ls is aliased to `ls --color=auto'
$ type cat
cat is /bin/cat

You can see, cd is a shell builtin.

This means, it's a part of your shell which is by default Bash. That's of course also the software package it's contained in.

For installed commands that are not shell builtins but executable files, use dpkg -S to find out the package:

$ dpkg -S $(which cat)
coreutils: /bin/cat

To get help for built in commands, use the help command (which is also built in):

$ help cd
cd: cd [-L|[-P [-e]] [-@]] [dir]
    Change the shell working directory.

[... output shortened ...]

Solution 2:

cd is necessarily a shell built-in. If the shell spawned a child process that changed the working directory and then exited, the parent process (the shell itself) would not be affected.

As to the source code of cd, all it needs to do is call chdir(2), which changes the working directory of the process. See chdir at opengroup.org which states:

The chdir() function shall cause the directory named by the pathname pointed to by the path argument to become the current working directory; that is, the starting point for path searches for pathnames not beginning with '/'.

Solution 3:

Here is a man page for cd but it not an official one since cd is part of the "Shell Builtin Commands". Just like some other commands ...

alias, bg, bind, break, builtin, command, compgen, complete, 
continue, declare, dirs, disown, echo, enable, eval, exec, exit, 
export, fc, fg, getopts, hash, help, history, jobs, kill, let, local, 
logout, popd, printf, pushd, pwd, read, readonly, return, set, shift, 
shopt, source, suspend, test, times, trap, type, typeset, ulimit,
umask, unalias, unset, wait 

See the man page for bash. From the link cd states:

cd [-L|-P] [dir]

Change the current directory to dir. The variable HOME is the default dir. The variable CDPATH defines the search path for the directory containing dir. Alternative directory names in CDPATH are separated by a colon (:). A null directory name in CDPATH is the same as the current directory, i.e., ''.''. If dir begins with a slash (/), then CDPATH is not used. The -P option says to use the physical directory structure instead of following symbolic links (see also the -P option to the set builtin command); the -L option forces symbolic links to be followed. An argument of - is equivalent to $OLDPWD. If a non-empty directory name from CDPATH is used, or if - is the first argument, and the directory change is successful, the absolute pathname of the new working directory is written to the standard output. The return value is true if the directory was successfully changed; false otherwise.

Which package is the command 'cd' in?

That would be bash

Solution 4:

The actual source code of cd is for

  • Bash in builtins/cd.def at the end of the file
  • TCSH in sh.dir.c as the function dochngd
  • PDKSH (Public Domain Korn shell found in some BSDs) in c_ksh.c right at the top of the file
  • Korn 93 (Original Korn shell, old version) in src/cmd/ksh93/bltins/cd_pwd.c

The sys-call chdir for

  • Linux kernel is in fs/open.c
  • FreeBSD is where the link at the beginning of this item points to
  • other BSD-type OSs and OpenSolaris work quite similar

It might be of note that the sys-calls are still not the end of the chain, that would be down at the individual file system level.

Solution 5:

Right, so as everyone has written, cd is built into whatever shell you use, because a process cannot affect the working directory of its parent. cd has a few bells and whistles tacked on (I mean pushd and friends, cd without an argument, etc.), but if you take the trouble to dig through the source you'll be underwhelmed: the main thing is that cd calls the chdir(2) system call.

There's not much to see because unlike, say, an environment variable like HOME, the "working directory" is a system primitive: It's one of the attributes that every process has, like (real) user id or scheduling priority. It is the starting point when opening files with relative paths.