查找和xargs正确处理名称中带有空格的文件名 [英] Find and xargs to correctly handle filenames with spaces in their names

查看:66
本文介绍了查找和xargs正确处理名称中带有空格的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我使用以下代码搜索和替换当前文件夹下任何 .php 文件中的字符串.

Currently I use the following code to search and replace strings in any .php files under the current folder.

find . -name "*.php" -print | xargs sed -i 's/string1/string2/g'

但是当遇到名称中带有空格的文件时,会抛出错误,提示没有这样的文件或目录.我将如何修改此行以包含带空格的文件?

But it throws an error as No such file or directory when it encounters a file with a space in its name. How would I modify this line to include files with spaces?

我认为 find 命令的输出必须在文件路径周围输出双引号,但是我不知道该怎么写.

I would assume the output from the find command would have to output double quotes around the file path, but I don't know how to write that myself.

推荐答案

find . -name "*.php" -print0 | xargs -0 sed -i 's/string1/string2/g'

您也可以完全不使用xargs:

You can also do it without xargs at all:

find . -name "*.php" -execdir sed -i 's/string1/string2/g' {} +

这篇关于查找和xargs正确处理名称中带有空格的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆