如何更改几个子目录中多个文件中的一个字符串?
How can I change one string in multiple files in few subdirectories?
我的问题是BASH一个目录和多个子目录下的多个txt文件中的一个单词怎么改?我做了如下(检查所有类似的主题)但它仍然无法正常工作。 changePhrase 是子目录和文件所在目录的名称。在该文件中有一个我想更改的字符串。我必须用 for 循环来完成它(这是一项任务)。
我的错误在哪里?谢谢。
#!/bin/bash
for file in changePhrase; do
if [[ -f $file ]] && [[ -w $file ]]; then
sed -i -- 's/old/new/g' "$file"
fi
done
你可以
find changePhraseDir -type f -a -writeable|xargs sed -i 's/foo/bar/'
-type f
-> 文件
-a
-> 和
-writable
-> 你的 -w
我认为只要将 find 添加到 for 循环中,您的代码就可以工作:
#!/bin/bash
for file in `find changePhrase`; do
if [[ -f $file ]] && [[ -w $file ]]; then
sed -i -- 's/old/new/g' "$file"
fi
done
如果 changePhrase 是您目录的名称,请尝试向其附加 /**{,/*}
。使用 globbing 将使循环遍历所有文件。
我的问题是BASH一个目录和多个子目录下的多个txt文件中的一个单词怎么改?我做了如下(检查所有类似的主题)但它仍然无法正常工作。 changePhrase 是子目录和文件所在目录的名称。在该文件中有一个我想更改的字符串。我必须用 for 循环来完成它(这是一项任务)。 我的错误在哪里?谢谢。
#!/bin/bash
for file in changePhrase; do
if [[ -f $file ]] && [[ -w $file ]]; then
sed -i -- 's/old/new/g' "$file"
fi
done
你可以
find changePhraseDir -type f -a -writeable|xargs sed -i 's/foo/bar/'
-type f
-> 文件-a
-> 和-writable
-> 你的-w
我认为只要将 find 添加到 for 循环中,您的代码就可以工作:
#!/bin/bash
for file in `find changePhrase`; do
if [[ -f $file ]] && [[ -w $file ]]; then
sed -i -- 's/old/new/g' "$file"
fi
done
如果 changePhrase 是您目录的名称,请尝试向其附加 /**{,/*}
。使用 globbing 将使循环遍历所有文件。