如何查看Linux系统的命令的源码

要想看源码,需要有源文件,那就需要下载包含源文件的rpm源码包,下载rpm源码包需要用到rpm包管理工具yumdownloader.

安装yumdownloader工具:

# yum install yum-utils

怎么查看某个命令的源代码

比如想查看uname这个命令的源代码

[#144#[email protected] /usr/src]# whereis dd 
dd: /usr/bin/dd /usr/share/man/man1/dd.1.gz
[#145#[email protected] /usr/src]# rpm -qf /usr/bin/dd
coreutils-8.22-21.el7.x86_64
[#146#[email protected] /usr/src]# yumdownloader --source coreutils-8.22-21.el7.x86_64

[#149#[email protected] /usr/src]# ls
coreutils-8.22-21.el7.src.rpm  debug  kernels

到这一步后有两种方法可以查看到源码包,第一种方法

[#151#[email protected] /usr/src]# rpm -qpl  coreutils-8.22-21.el7.src.rpm |grep tar ##查询rpm中的tar文件,源码一般放在这
coreutils-8.22.tar.xz

##从rpm包中提取指定的文件

[#155#[email protected] /usr/src]# tar xvf coreutils-8.22.tar.xz 

[#156#[email protected] /usr/src]# ls 
coreutils-8.22  coreutils-8.22-21.el7.src.rpm  coreutils-8.22.tar.xz  debug  kernels

##到解压出来的coreutils-8.22/src/中就能找到相关的源码了

如何查看Linux系统的命令的源码

第二种方法:安装源码包

[[email protected] tmp]$ rpm -i coreutils-8.5-7.fc14.src.rpm ##安装源码包

warning: user mockbuild does not exist - using root

warning: group mockbuild does not exist - using root

##centos和rhel一般安装后的源码在/usr/src/redhat下的各个目录下,但fedora是在~/rpmbuild/SOURCES/

##可以通过locate命令查询代码包被安装到哪里了

[[email protected] tmp]$ locate *coreutils*tar*

/home/workhard/tmp/coreutils-8.5.tar.xz

/home/workhard/rpmbuild/SOURCES/coreutils-8.5.tar.xz

[[email protected] tmp]$ cd /home/workhard/rpmbuild/SOURCES/c

[[email protected] SOURCES]$ tar Jxvf coreutils-8.5.tar.xz

[[email protected] src]$ cd coreutils-8.5/src/

[[email protected] src]$ ll uname*

-rw-r--r--. 1 workhard workhard 48 Jan 1 2010 uname-arch.c

-rw-r--r--. 1 workhard workhard 10024 Jan 1 2010 uname.c

-rw-r--r--. 1 workhard workhard 146 Jan 1 2010 uname.h

-rw-r--r--. 1 workhard workhard 49 Jan 1 2010 uname-uname.c

安装*.src.rpm后怎么卸载

[[email protected] SOURCES]$ cd /home/workhard/rpmbuild/SOURCES

[[email protected] SPECS]$ rpmbuild --rmsource --rmspec coreutils.spec

**相关说明

一、yumdownloader comes from the yum-utils package, if you don't have that installed already.

#yum install yum-utils

二、RPM用法

rpm {-i|--install} [install-options] PACKAGE_FILE ...

-f, --file FILE

Query package owning FILE.

-p, --package PACKAGE_FILE

Query an (uninstalled) package PACKAGE_FILE.

-l, --list

List files in package.

-qf:查找指定文件属于哪个RPM软件包

-qpl:列出RPM软件包内的文件信息;

三、rpm2cpio - Extract cpio archive from RPM Package Manager (RPM) package.

顾名思义,rpm2cpio是将rpm包文件转换成cpio归档文件

四、CPIO用法

cpio - copy files to and from archives

-i 抽取的意思,和"--extract"等同

-d 建立目录,和"--make-directories"等同

-v 冗余信息输出,和"--verbose"等同

五、rpmbuild用法

--rmsource

Remove the sources after the build

--rmspec

Remove the spec file after the build