I need to append the following code to the end to all the php files in a directory and its sub directory:
You can use sed
combined with find
. Assume your project tree is
/MyProject/
/MyProject/Page1/file.php
/MyProject/Page2/file.php
etc.
/MyProject/
. Call it append.txt
From /MyProject/
run:
find . -name "*.php" -print | xargs sed -i '$r append.txt'
Explain:
find
does as it is, it looks for all .php, including subdirectoriesxargs
will pass (i.e. run) sed
for all .php that have just been foundsed
will do the appending. '$r append.txt' means go to the end of the file ($) and write (paste) whatever is in append.txt there. Don't forget -i
otherwise it will just print out the appended file and not save it.Source: http://www.grymoire.com/unix/Sed.html#uh-37