I want to be able to see the code that gets executed whenever I type 'ls' or 'cd' in my terminal. With that I would like to get into modifying existing programs and writing new ones. In what language are those programs written?


1 Answer 1


Where to find the code for the terminal commands?

Where to find it depends on the command. There are several locations.

Regarding the two you mention:

GNU coreutils

ls is part of GNU Coreutils. You can get the source code for all the GNU core utilities (so not just for ls: wikipedia list of core utilities) on www.gnu.org. From the command line using git (git needs to be installed):

git clone git://git.sv.gnu.org/coreutils

The source for ls can also be found online on git.savannah.gnu.org. All the other commands are accessible there too.

  • Shell builtins

cd is a built-in in the shell so is part of bash. You can find the source code for bash on ftp.gnu.org. That will also be the source code for all builtins.

bash defines the following built-in commands: :, ., [, alias, bg, bind, break, builtin, case, cd, command, compgen, complete, continue, declare, dirs, disown, echo, enable, eval, exec, exit, export, fc, fg, getopts, hash, help, history, if, 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, until, wait, while.

In what language are those programs written?

C or C++

With that I would like to get into modifying existing programs and writing new ones

You are better off using the existing tools and adding to them: create a script called ls2 and then add functionality to that script to mimic ls + your additions. Your additions could be extra letters as parameters that trigger functions. Biggest advantage: you can write it in the language you prefer. Safer since you do not mess with the normal commands if your intention is to code new functionality for your these tools you should join their respective mailing lists.

  • Also, these programs are already compiled, so to modify them internally, it needs to be recompiled and replace current exist files (for example, if ls must display ls -l by default, ...); I think also that it could be dangerous for the OS to do that
    – damadam
    Dec 4, 2019 at 11:52
  • Nice answer! Thanks! Do you have a good resource for getting started with scripting ('ls2' example). And can I see some example scripts somewhere on my computer to mimic their behaviour? Dec 4, 2019 at 20:21
  • not really :-P The ls2 is just me being a coder and thinking on how -I- would approach this :D Like docs.python.org/3/library/configparser.html can be used to read an config file and argparse can be used to pass options to your script docs.python.org/3/library/argparse.html ls2 could be a python script reading all the options and if those are part of ls you print the results of the command to the display and if those are your new options you code something around it to then print the results.
    – Rinzwind
    Dec 4, 2019 at 20:32
  • ah ok, thanks :). I know how to code with java and python. But don't you start python scripts with 'python' keyword and java with 'java -jar'? I mean in those cases even python or java needs to be installed on the machine. Maybe I just read the src links that you posted and find it out myself. Dec 5, 2019 at 10:03
  • @PaulErlenmeyer You could add a python shebang to the beginning of your script, which would make it directly executable from the command line (as long as it's (a) in your $PATH and (b) executable [think chmod u+x])
    – MTCoster
    Dec 5, 2019 at 15:22

You must log in to answer this question.

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