[英]How to replace a string in multiple files in multiple subfolders with different file extensions in linux using command line
我已經關注了這個查詢@( How to replace a string in multiple files in multiple files in linux command line )。
我的問題是相同的延伸。
我只想檢查子文件夾中的特定文件擴展名,而不是每個文件擴展名。
我已經嘗試過的:
grep -rli 'old-word' * | xargs -i@ sed -i 's/old-word/new-word/g' @
我的問題:它也在所有其他文件格式中發生變化。 我只想在一個文件擴展名中搜索和替換。
請添加另一個答案,我可以在其中更改文件的整行,而不僅僅是一個單詞。
提前致謝。
最簡單的解決方案是使用復雜grep
命令:
grep -rli --include="*.html" --include=".json" '舊詞' *
這種解決方案的缺點。 是您沒有明確控制掃描哪些文件。
find
命令以找到所需的文件。 使用 RegExp 過濾選項-regex
過濾文件名。
因此,您驗證掃描了正確的文件。
然后將find
命令結果提供給grep
掃描列表。
假設您正在尋找文件擴展名txt
pdf
html
。
假設您的搜索路徑從/home/user/data
開始
find /home/user/data -regex ".*\.\(html\|txt\|pdf\)$"
找到文件后。 grep
可以匹配上述find
命令中的每個文件:
grep -rli 'old-word' $( find /home/user/data -regex ".*\.\(html\|txt\|pdf\)$" )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.