How do I read the source code of shell commands?

前端 未结 7 1820
遇见更好的自我
遇见更好的自我 2020-12-04 04:30

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

相关标签:
7条回答
  • 2020-12-04 04:52

    Direct links to source for some popular programs in coreutils:

    • cat (767 lines)
    • chmod (570 lines)
    • cp (2912 lines)
    • cut (831 lines)
    • date (570 lines)
    • df (1718 lines)
    • du (1112 lines)
    • echo (272 lines)
    • head (1070 lines)
    • hostname (116 lines)
    • kill (312 lines)
    • ln (651 lines)
    • ls (4954 lines)
    • md5sum (878 lines)
    • mkdir (306 lines)
    • mv (512 lines)
    • nice (220 lines)
    • pwd (394 lines)
    • rm (356 lines)
    • rmdir (252 lines)
    • shred (1325 lines)
    • tail (2301 lines)
    • tee (220 lines)
    • touch (437 lines)
    • wc (801 lines)
    • whoami (91 lines)

    Full list here.

    0 讨论(0)
  • 2020-12-04 05:00

    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

    0 讨论(0)
  • 2020-12-04 05:00
        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.

    0 讨论(0)
  • 2020-12-04 05:01

    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

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

    0 讨论(0)
  • 2020-12-04 05:03

    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.

    0 讨论(0)
  • 2020-12-04 05:05

    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

    0 讨论(0)
提交回复
热议问题