LoginSignup
95
86

More than 5 years have passed since last update.

sed一括置換術(Gitにも対応)

Last updated at Posted at 2013-08-03

Linuxのターミナルで作業しているとき、あるディレクトリ以下のファイルすべてについて一括置換したいなぁって思うときありますよね。
そのときのTips。

普通のやつ

find . -type f -print0 | xargs -0 sed -i -e 's/foo/bar/g'

Gitバージョン管理のリポジトリ

Linuxのとき

find を使うと .git ディレクトリも巻き込んでリポジトリをぶっ壊すので ls-files を使います。

git ls-files -z | xargs -0 sed -i -e 's/foo/bar/g'

Macのとき

-i オプションの後に空文字列をつけるのがポイント。じゃないとハマる。

git ls-files -z | xargs -0 sed -i '' -e 's/foo/bar/g'

また、 sed: RE error: illegal byte sequence エラーが出た時の対処は以下のようにします。

export LANG=C
95
86
0

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
95
86