linux - 遍历到文件夹树linux的底部

标签 linux bash

所以我想在每个包含特定字符串的文件上执行此命令。

这是我找到的脚本:

replace $old_string $new_string -- path

它奏效了。问题是我必须这样做:

replace "import firebase" "import {firebase}" -- ./*/*

然后这个:

replace "import firebase" "import {firebase}" -- ./*/*/*

然后这个:

replace "import firebase" "import {firebase}" -- ./*/*/*/*

等等。最后,我走到了尽头,一切都成功了。

但现在我想写一个脚本以供将来使用,显然,我不想只是创建一个无限循环,试图越陷越深。

那么我怎样才能让脚本遍历树并在途中更改文件呢?

我现在的脚本:

echo "Give me the old string"
read old_string
echo "Give me the new string"
read new_string
echo "Give me the path"
read path

cd $path
replace $old_string $new_string -- #**/* something like this? I'm stuck here

~/Documents/Projects/uczichapp-react/src$ replace "import {firebase}" "import firebase" -- ./*/**

replace: Error reading file './Components/classes' (Errcode: 21 - Is a directory)

replace: Error reading file './Components/globals' (Errcode: 21 - Is a directory)

replace: Error reading file './Components/navigation' (Errcode: 21 - Is a directory)

replace: Error reading file './Components/shared' (Errcode: 21 - Is a directory)

replace: Error reading file './Components/solver' (Errcode: 21 - Is a directory)

replace: Error reading file './Components/student' (Errcode: 21 - Is a directory)

最佳答案

您可以使用 find 命令并将其输出通过管道传输到 xargs 来完成此操作:

find . -type f | xargs replace "import firebase" "import {firebase}" -- 

或者使用find直接执行:

find . -type f -exec replace "import firebase" "import {firebase}" -- {} +

关于linux - 遍历到文件夹树linux的底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54345630/

相关文章:

c - 链接到目标文件时出错

BASH:用于替换文件树中文本的递归程序

bash - 需要 grep 具有特定扩展名的文件并移动到另一个文件夹

linux - 在 Linux 中修改定时器中断

linux - 使用 crontab 压缩文件

linux - Bash 脚本从 rc.local 运行

linux - 将循环索引的值(增量)分配给bash中的变量

linux - 修补/替换内存中的linux内核

java - Jpos 异常 : Can't connect printer device (issue on linux only)

linux - 多个用户可以登录到同一个 tty