如何实现在 rpm 包中查找和 grep 特定文件并在一个命令行执行中复制它?
我尝试使用 yum repoquery 来查看 rpm 包中的所有文件,并尝试使用 grep 查找特定文件。之后我想获取结果路径并将其复制到 /tmp 文件夹。
以下命令是错误的:
yum repoquery -l rpm_package | grep "specifc/file.txt" -exec cp "{}" /tmp \;
结果:
grep: specifc/file.txt: File or directory not found
grep: cp: File or directory not found
grep: {}: File or directory not found
grep: /tmp: Is directory
grep: ;: File or directory not found
我希望我已经正确理解了你的问题,这可能不是最好的解决方案,但无论如何都是如此。
我的测试基于
putty.rpm
二进制包。
有一个名为“rpmdev-extract”的命令(来自
rpmdevtools
包),它允许您(顾名思义)提取 RPM 的(全部)内容,但我没有找到一种方法来“过滤”特定文件名。
根据本页,RPM二进制包包含cpio格式的最终有效负载。因此,建议的方法是使用
rpm2cpio
提取该有效负载并将其提供给 cpio
命令,该命令允许根据给定模式选择单个源文件。
手册页摘录:
cpio {-i|--extract} (...) [pattern...] [< archive]
Any non-option command line arguments are shell globbing patterns;
only files in the archive whose names match one or more of those
patterns are copied from the archive.
您可以通过
rpm -q -l name
查询RPM包的内容。
对于“putty”,我只想从存档中提取文件“/usr/share/applications/putty.desktop”。
执行:
$ rpm2cpio putty.rpm | cpio -i --to-stdout '*/putty.desktop'
[Desktop Entry]
Name=Putty
Comment=A SSH, Telnet and Rlogin client
Exec=putty
Icon=putty
Terminal=false
Type=Application
Categories=Network;RemoteAccess;
StartupNotify=true
X-Desktop-File-Install-Version=0.26
尝试将上述内容推断为您的具体需求:
rpm2cpio path/to/rpm_package.rpm | cpio -i --to-stdout '*specifc/file.txt' >/tmp/output.txt
希望有帮助。