182

I would like to read the actual source code which the linux commands are written with. I've gained some experience using them and now I think it's time to interact with my machine at a deeper level.

I've found some commands here http://directory.fsf.org/wiki/GNU. Unfortunately I wasn't able to find basic commands such as 'ls' which seems to me easy enough to begin.

How exactly do I read the source code of the simple shell commands like 'ls'? I'm running on Ubuntu 12.04

4

8 Answers 8

196

All these basic commands are part of the coreutils package.

You can find all information you need here:

http://www.gnu.org/software/coreutils/

If you want to download the latest source, you should use git:

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

To install git on your Ubuntu machine, you should use apt-get (git is not included in the standard Ubuntu installation):

sudo apt-get install git

Truth to be told, here you can find specific source for the ls command:

http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/ls.c

Only 4984 code lines for a command 'easy enough' as ls... are you still interested in reading it?? Good luck! :D

6
  • 2
    OMG! I thought it was a bit easier... Well the path is longer and harder than expected :-) Jul 17, 2012 at 23:26
  • 25
    Try the source for cat. It's only 767 lines. Sep 23, 2014 at 23:08
  • 3
    Only 4984 code lines ... -- It seems in 4 years and 3 months ls has become another 57 lines more complex.
    – sherrellbc
    Oct 14, 2016 at 13:43
  • 3
    I remember once I tried to understand why wc is so much faster than equivalent Java program. So I looked at the code ... OMG is it optimized for disk reads, buffering inputs, etc or what :)
    – yǝsʞǝla
    Dec 2, 2016 at 2:04
  • @yǝsʞǝlA I'm not proficient in C; how do you tell what it's optimizing for? Can you give me an example? Mar 30, 2017 at 18:13
151

Direct links to source for some popular programs in coreutils:

Full list here.

4
  • 9
    Thanks for listing with number of lines. :)
    – Gowtham
    Aug 23, 2016 at 5:52
  • Do you have su?
    – Aaron Esau
    Feb 9, 2018 at 0:15
  • @Drew, do you have any idea if there are shell commands actually written in shell script?
    – Rafael
    Sep 17, 2019 at 11:22
  • I don't see the find command in here like find . -type f -name '*.csproj' I am trying to see how that algorithm looks Aug 7, 2021 at 3:13
6

ls is part of coreutils. You can get it with git :

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

You'll find coreutils listed with other packages (scroll to bottom) on this page.

1
6

Actually more sane sources are provided by http://suckless.org look at their sbase repository:

git clone git://git.suckless.org/sbase

They are clearer, smarter, simpler and suckless, eg ls.c has just 369 LOC -

http://git.suckless.org/sbase/file/ls.c.html

After that it will be easier to understand more complicated GNU code.

2

You can have it on github using the command

git clone https://github.com/coreutils/coreutils.git

You can find all the source codes in the src folder.

You need to have git installed.

Things have changed since 2012, ls source code has now 5309 lines

1

CoreUtils referred to in other posts does NOT show the real implementation of most of the functionality which I think you seek. In most cases it provides front-ends for the actual functions that retrieve the data, which can be found here:

It is build upon Gnulib with the actual source code in the lib-subdirectory

2
  • Is this resource official or a third party repository?
    – Amiri
    Sep 9, 2020 at 15:12
  • @Merlin: it is official
    – slashmais
    Sep 11, 2020 at 8:13
0

BSD distributions are actually a nice way of reading the source code, by using their repositories, since it is all packed into one place, and you can view how historically the source code has evolved, or changed. So why not use BSD repos , such as NetBSD or OpenBSD for this task.

-1
    cd ~ && apt-get source coreutils && ls -d coreutils*     

You should be able to use a command like this on ubuntu to gather the source for a package, you can omit sudo assuming your downloading to a location you own.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.