如何将 .zip 文件解压缩到所有包含 .php 文件的目录中,其中包含文本“house”?
就像是:
grep -rl --include='index.php' 'house' /directory/path/of_the_index_file* | xargs unzip /directory2/path/file.zip -d /directory/path/of_the_index_file/
澄清:我只是想将一个包含 2 个文件和一个目录的简单 .zip 文件解压缩到服务器上的所有目录,这些目录包含一个 index.php 文件,在其代码中包含一个特定的字符串。
我会非常感谢任何帮助。
您可以xargs sh -c '...'
与$(dirname "$1")
以下一起使用:
grep -rlZ --include='index.php' house /directory/path/of_the_index_file* \
| xargs -0 -I{} sh -c 'unzip /directory2/path/file.zip -d "$(dirname "$1")"' xargs-sh {}
或者更简单,使用 GNU parallel
,它具有以下dirname
功能{//}
:
grep -rlZ --include='index.php' house /directory/path/of_the_index_file* \
| parallel -0 -n1 unzip /directory2/path/file.zip -d {//}
如果需要,可以通过添加-j1
到parallel
.
如果parallel
未安装,请使用以下命令安装:
sudo apt install parallel
您可能会考虑unzip
在开始时运行一次到临时目录并使用cp
or ln
inside xargs
or parallel
。
注意:对于这两种解决方案,我grep -Z
一起使用withxargs/parallel -0
来避免路径中某处换行符出现问题。就算不影响你,照顾它也是一个好习惯。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句