如何查看linux命令源代碼

用linux一段時間了,有時候想看看ls、cat、more等命令的源代碼,在下載的內核源碼中用cscope沒能找到,在網上搜索了一下,將方 法總結以下:linux

  1. 以搜索ls命令源碼爲例,先搜索命令所在包,命令以下:
    lpj@lpj-linux:~$ which ls /bin/ls
  2. 用命令搜索該軟件所在包,代碼以下:
    lpj@lpj-linux:~$ dpkg -S /bin/ls coreutils: /bin/ls
  3. 從上一步中能夠知道ls命令的實如今包coreutils中,用apt安裝(說安裝有些歧義,主要是區分apt-get -d)該包的源代碼而後解壓,代碼以下:
    sudo apt-get source coreutils cd /usr/src/coreutils-XXX #XXX表示版本號 sudo tar zxvf coreutils-XXX.tar.gz


    或者只下載源碼,而後手動打補丁再解壓,代碼以下:
    sudo apt-get -d source coreutils cd /usr/src tar zxvf coreutils-XXX.tar.gz gzip -d coreutils-XXX.diff.gz #這一步會生成coreutils-XXX.diff文件 patch -p0 < coreutils-XXX.diff cd coreutils-XXX tar zxvf coreutils-XXX.tar.gz

OK,這幾步執行完後,就能夠進入/usr/src/coreutils-XXX/coreutils-XXX/src中查看各命令對應的源代碼了spa

相關文章
相關標籤/搜索