4
whereis pwd
pwd: /bin/pwd /usr/include/pwd.h /usr/share/man/man1/pwd.1.gz

The pwd in the binary folder is not the same as

type pwd
pwd is a shell builtin

so wouldn't it be harmless to do some experimentation on the external pwd? Like, adding a "Hello shell!" cout just to demonstrate the principle?

Where is the source for pwd? Do you usually get it with the distribution (I'm on Debian) or do you somehow install or download it? Is it in C? Do I compile it like any other file with gcc and put the result (with fitting chmod) in a folder encompassed by the path? What about upgrades? As you understand, I'm missing the big picture here.

(By the way, the header file seems to be unrelated: pwd as in password, not print/present working directory.)

2
  • You're likely to have your question closed or heavily edited if it has so many questions in it. As a rule, please post one question per question. Apr 29, 2012 at 11:34
  • "How do I edit /bin/pwd ?" Apr 29, 2012 at 11:38

3 Answers 3

7

wouldn't it be harmless to do some experimentation on the external pwd?

It's a bad bet that nothing uses /bin/pwd. It's good practice in shell scripts — particularly those commonly run by root — to give full paths to external programs, for security reasons.

Still, you could safely build a custom pwd and put it under in your home directory somewhere. If the package uses Autoconf, this is usually enough to configure a package to be installed under your home directory:

$ ./configure --prefix=$HOME

You might say something like --prefix=$HOME/pwd-test instead, to avoid any possibility of conflict.

As long as the package's build system is set up correctly, when you've configured it like that you can safely say make install without being root, because all files it writes should go under the prefix you gave.

Where is the source for pwd?

pwd is part of coreutils. You find such things out with the Debian package search engine.

Do you usually get it with the distribution (I'm on Debian)

You probably haven't downloaded the distribution sources yet, but yes, it's considered part of the Debian distribution. They're separated out into a six-disk (!) source DVD set, comprising about 25 GB, which is why most people never download them.

Unless you're trying to do something like rebuild the whole Debian distribution or create a derivative distribution, though, you probably shouldn't download them even now. A la carte downloads are probably a better idea at this stage.

do you somehow install or download it?

Yes, you can also use apt-get to install source code for packages. There's a whole chapter in the APT HOWTO on this.

(That document is marked Obsolete, but I'm not seeing a replacement document.)

Is it in C?

In all likelihood, yes.

Do I compile it like any other file with gcc and put the result (with fitting chmod) in a folder encompassed by the path?

You probably don't run gcc directly, you probably do the standard configure ; make ; make install dance. If you download the source tarball from the Debian package search page, you will probably find an INSTALL or README file in the tarball, which will contain build instructions.

What about upgrades?

What about them? The package search engine will help you find any version of the software you're likely to want, and apt-get will help you track changes to the sources just as it will for binaries.

I'm missing the big picture here.

You might want to take a look at the Debian Documentation, then.

the header file seems to be unrelated: pwd as in password, not print/present working directory.)

Yup. It's a utility header for C programmers, for getting access to the user database. Say man 3 getpwent to get some idea of what's available through that interface.

5

On Linux, pwd is usually provided by the coreutils package. You can download the source from that website, or from your distribution's source repository (here for Debian).

You can compile pwd and the other tools in that package the usual way: unpack the source and:

./configure
make

You can modify the source code for pwd and recompile as you wish (it is C code, in src/pwd.c). But I would not recommend you install anything you've modified yourself from coreutils over your distribution's copy.

If you want to "play" with it, install it to a location somewhere in your home directory, and prepend that to your path (or to a test user's path). Only overwrite executables managed by your distribution/package manager if you know exactly what you're doing (and know how to undo that).

This isn't specific to coreutils, you can do this with all open source packages that your distribution provides (and even ones that it doesn't). But the warning about not munging files managed by your distribution is more important for low-level "system" packages where you're more likely to screw up your system completely.

3

Why /bin/pwd and a builtin

/bin/pwd is an independent executable that prints the current directory. Most shells have a pwd builtin for two reasons:

  • This is a common, simple operation; a builtin is faster.
  • Several shells keep track of symbolic links in the current directory. Consider:

    $ pwd
    /home/gilles
    $ ln -s /tmp sym       # Create a symlink /home/gilles/sym -> /tmp
    $ cd sym
    $ pwd
    /home/gilles/sym       # the directory tracked by the shell
    $ /bin/pwd
    /tmp                   # the "real" location of the current directory
    $ pwd -P               # pwd -P also shows the "real" location
    /tmp
    $ cd ..
    $ pwd
    /home/gilles           # The shell stripped one level off its current directory
    $ set -P               # Turn off symlink following (in bash)
    $ cd sym
    $ pwd
    /tmp
    $ cd ..
    $ pwd
    /
    

Some scripts explicitly call /bin/pwd in order to avoid any symlink following that the shell might do. Overriding /bin/pwd to do something else is likely to break stuff.

Where to find the source

On Debian and derivatives (including Ubuntu and derivatives), and any other system that uses dpkg and APT to manipulate packages, you can find in which package a file is by running

dpkg -S /bin/pwd

You'll see that this file is provided by the coreutils package.

In general, to find out the source package that this binary package is built from, run dpkg -s coreutils or apt-cache show coreutils or aptitude show coreutils; here, they do not show a Source: line, which means that the source package has the same name as the binary package (this isn't always the case, mostly when a source package is split into several binary packages).

If you want to get the source, you don't even need to find out the name of the source package. Just run

apt-get source coreutils

This will download and unpack the sources of the coreutils package.

See also

Warren Young and Mat's answers, which make some complementary points.

1
  • Thanks for your answer, and you can be 100% relaxed that I read those other answers very carefully :) Apr 30, 2012 at 14:36

You must log in to answer this question.

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