Append a text to the end of multiple files in Linux

前端 未结 6 1438
囚心锁ツ
囚心锁ツ 2021-02-05 01:05

I need to append the following code to the end to all the php files in a directory and its sub directory:

6条回答
  •  情话喂你
    2021-02-05 01:45

    You don't specify the shell, you could try the foreach command. Under tcsh (and I'm sure a very similar version is available for bash) you can say something like interactively:

    foreach i (*.php)
    foreach> echo "my text" >> $i
    foreach> end
    

    $i will take on the name of each file each time through the loop.

    As always, when doing operations on a large number of files, it's probably a good idea to test them in a small directory with sample files to make sure it works as expected.

    Oops .. bash in error message (I'll tag your question with it). The equivalent loop would be

    for i in *.php
    do 
       echo "my text" >> $i
    done
    

    If you want to cover multiple directories below the one where you are you can specify

    */*.php
    

    rather than *.php

提交回复
热议问题