LoginSignup
129
131

More than 3 years have passed since last update.

find + sed で一斉置換

Last updated at Posted at 2013-03-11
sh
# カレントディレクトリ以下のファイルすべての中の HOGE を MOGA に置換
$ find . -type f -print0 | xargs -0 sed -i -e "s/HOGE/MOGA/"
sh
# 対象ファイルをファイル名のパターンで限定する場合は find にオプションを
$ find . -type f -name "*.yaml" -print0 | xargs -0 sed -i -e "s/HOGE/MOGA/"
sh
# 置換時にバックアップファイルを作成する場合は sed の -i に拡張子をつける
$ find . -type f -print0 | xargs -0 sed -i.bak -e "s/HOGE/MOGA/"

おまけ

sh
# find + grep で HOGE を内容に含むファイルを探す(調べたファイル名を出力しつつ)
$ for file in `find /path/to/dir -name "*.gz" -type f`; do echo $file; less $file | grep 'HOGE'; done;
129
131
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
129
131