BASH:递归程序来替换文件树中的文本 [英] BASH: recursive program to replace text in a tree of files

查看:13
本文介绍了BASH:递归程序来替换文件树中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Bash 的新手,但我似乎无法找到一种方法让它做我想做的事.

I am completely new at Bash but I just can't seem to find a way to make it do what I want.

假设您有一个包含 2 个文件的树目录:/top.php 和/test/bottom.php

Imagine you have a tree directory with 2 files: /top.php and /test/bottom.php

如何使我的函数看起来并在/top.php 和/test/bottom.php 中将 say "hello" 替换为 "bonjour"?

How do I make my function look and replace say "hello" into "bonjour" in /top.php AND in /test/bottom.php?

到目前为止,我发现的唯一方法是使用不同的深度级别调用相同的函数两次:

So far the only way I have found to do this is by calling the same function twice with a different depth level:

find ./*.php -type f -exec sed -i 's/hello/bonjour/' {} ;
find ./*/*.php -type f -exec sed -i 's/hello/bonjour/' {} ;

肯定有一种递归方式可以在一行中做到这一点吗?

Surely there's a recursive way to do this in one line?

推荐答案

使用实际模式代替 shell 通配符扩展:

Use an actual pattern for find instead of shell wildcard expansion:

find . -name '*.php' -type f -exec sed -i 's/hello/bonjour/' {} ;

这篇关于BASH:递归程序来替换文件树中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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