10

Examples of commands I'm referring to are ls, pwd, and cd. Also, how are these built? Do you have an example?

Mat
  • 52,586
fronthem
  • 5,107

2 Answers2

26

It's usually plain C. The commands ls and pwd come from the GNU Coreutils package in (most?) Linux distributions (and maybe some other systems). You can find the code on their homepage.

For coreutils specifically, you build them with the usual steps: after unpacking the source, issue:

./configure --prefix=/some/path
                   # type ./configure --help to get the available options
make
make install       # could require root access depending on the path you used

Be carefull - installing base utilities like those over your distribution's copy of them is a bad idea. Use whatever package manager your system comes with for that. You can install to a different prefix though (installing somewhere into your home directory is a good idea if you want to experiment).

Note that although there is a cd executable, the cd you'll be using in most circumstances isn't a separate executable. It has to be a shell build-in (otherwise it could not change the shell's current directory - this has to be done by the process itself), so it is written in the same language as the shell (which is often C too).

Other examples:

  • OpenSolaris pwd source.
  • FreeBSD ls

You can find many more of these online.

Mat
  • 52,586
5

Just to add a bit more to Mat's answer, although Unix implements these as standalone programs, many commands are also implemented within bash (to save it having to spawn a new process) see the bash man page for more details, or have a look at the tag.

bash is also written in C

It may be worth noting that these commands do not have to be written in C - indeed, several mini linux distribution use lua implementations of these and others.

symcbean
  • 5,540