所以我想在每个包含特定字符串的文件上执行此命令。
这是我找到的脚本:
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/