複数のファイルに行を追加する

複数のファイルに行を追加する

200,000を超えるファイルにテキストを追加したいです。今しようとしています。

find . -name *.txt -print | xargs -I % echo "hello world" >> %

しかし、何も起こりませんでした。実行するとfind . -name *.txt自然になりました。echo "hello world" >> myfile.txt

ベストアンサー1

シェルは見る>> %前に部品をxargs拡張しています。

シェルリダイレクトを実行する必要がある場合は、次のことを試してください。

find . -name "*.txt" -exec sh -c '
    echo "hello world" >> "$0"
    ' {} \;

仕組み:

  1. find{}一致するすべてのファイルに置き換える
  2. bash -c "some command" arg0...$0...スクリプト内で設定"some command"

sed>>または、たとえば、依存しないコマンドを使用できます。

find . -name "*.txt" -exec sed -i -e '$a\
hello world' {} \;

引用:

おすすめ記事