gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:50:23 24 4
gpt4 key购买 nike

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

这是我找到的脚本:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com