linux - 查找和 sed 在 centos 7 中不起作用

标签 linux sed find centos7

我试图在我的整个项目中查找和替换一个词,我在 centos 7 中尝试了以下版本的 find 和 sed,但没有任何效果。

find ./ -name "*.php" -exec sed -i '' s/mysql_/mysqli_/g \;
find ./ -name "*.php" -exec sed -i '' s/mysql_/mysqli_/g {} \;
find ./ -name "*.php" -exec sed -i '' 's/mysql_/mysqli_/g' {} \;
find ./ -name "*.php" -ls | xargs sed -i '' 's/mysql_/mysqli_/g'

sed: can't read s/mysql_/mysqli_/g: No such file or directory

即使我从项目的根目录运行这些命令,以上所有命令都会在循环中给我这个错误。权限都是正确的。如果我只是单独使用 find 命令,它就可以工作

find ./ -name "*.php" -ls (This Works)

我尝试了 stackoverflow 中可用的解决方案,但没有任何效果。

最佳答案

sed 中的第一对引号不是必需的,尝试:

find ./ -name "*.php" -exec sed -i s/mysql_/mysqli_/g {} \;

语法是 -i'prefix'--in-place='prefix',而不是 -i 'prefix' ,因为您在前缀和参数之间添加了一个空格,所以 sed 使用前缀(空字符串)参数作为正则表达式并使用实际的正则表达式作为文件名参数,这显然找不到.

这就是为什么您会收到 can't read s/mysql_/mysqli_/g: No such file or directory 错误。

关于linux - 查找和 sed 在 centos 7 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31835646/

相关文章:

linux - 如何在 Perl 中监控远程 Linux 机器并检索已安装的软件?

linux - 通过 linux 文件命令获取 file1 和 file2 联合内容的更好方法?

c - 生成文件说明

xml - Sed 替换保留前导空格

ruby-on-rails - rails ,acts_as_taggable_on : how to get all aricles without tags

php - 如何在我的服务器上安装 Pdftk?

c - 如何从 C 在 Linux 中使用 sched_getaffinity 和 sched_setaffinity?

bash - 需要使用 sed 更改 json 文件的值

javascript - jQuery 查找并列出特定 DIV 中 UL 内的所有 LI 元素

Matlab:查找矩阵每一列首次出现的行索引(不使用循环)