centos下如何获取某个命令的源代码?

以ls命令为例,其它命令类似:

1.利用which命令获取命令可执行文件的位置:

[root@228 /]# which ls
alias ls='ls --color=auto'
    /usr/bin/ls

2.利用rpm命令获取命令所在的安装包:

[root@228 /]# rpm -qif /usr/bin/ls
Name        : coreutils
Version     : 8.22
Release     : 15.el7
Architecture: x86_64
Install Date: Mon 23 Apr 2018 10:04:15 PM CST
Group       : System Environment/Base
Size        : 14581114
License     : GPLv3+
Signature   : RSA/SHA256, Wed 25 Nov 2015 10:22:25 PM CST, Key ID 24c6a8a7f4a80eb5
Source RPM  : coreutils-8.22-15.el7.src.rpm
Build Date  : Fri 20 Nov 2015 01:36:57 PM CST
Build Host  : worker1.bsys.centos.org
Relocations : (not relocatable)
Packager    : CentOS BuildSystem <http://bugs.centos.org>
Vendor      : CentOS
URL         : http://www.gnu.org/software/coreutils/
Summary     : A set of basic GNU tools commonly used in shell scripts
Description :
These are the GNU core utilities.  This package is the combination of
the old GNU fileutils, sh-utils, and textutils packages.

3.利用yumdownloader命令下载对应的源安装包:

[root@228 /]# yumdownloader --source coreutils
Loaded plugins: fastestmirror, langpacks
Enabling updates-source repository
Enabling base-source repository
Enabling extras-source repository
base                                                                                                                                                                                       | 3.6 kB  00:00:00     
base-source                                                                                                                                                                                | 2.9 kB  00:00:00     
extras                                                                                                                                                                                     | 3.4 kB  00:00:00     
extras-source                                                                                                                                                                              | 2.9 kB  00:00:00     
updates                                                                                                                                                                                    | 3.4 kB  00:00:00     
updates-source                                                                                                                                                                             | 2.9 kB  00:00:00     
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirrors.tuna.tsinghua.edu.cn
 * updates: mirrors.tuna.tsinghua.edu.cn
coreutils-8.22-21.el7.src.rpm    

4.利用rpm2cpio命令提取源代码,得到源码压缩包(coreutils-8.22.tar.xz):

[root@228 /]# rpm2cpio coreutils-8.22-21.el7.src.rpm | cpio -id
11217 blocks

5.利用xz, tar命令解压即可得命令源代码:

[root@228 /]# xz -d coreutils-8.22.tar.xz 
[root@228 /]# tar xvf coreutils-8.22.tar

coreutils-8.22文件夹下即可找到所需要的源代码。

posted @ 2018-05-30 19:29  寒星n号  阅读(1548)  评论(0编辑  收藏  举报