Append a text to the end of multiple files in Linux Append a text to the end of multiple files in Linux unix unix

Append a text to the end of multiple files in Linux


I usually use tee because I think it looks a little cleaner and it generally fits on one line.

echo "my text" | tee -a *.php


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" >> $iforeach> 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 *.phpdo    echo "my text" >> $idone

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

*/*.php

rather than *.php


BashFAQ/056 does a decent job of explaining why what you tried doesn't work. Have a look.

Since you're using bash (according to your error), the for command is your friend.

for filename in *.php; do  echo "text" >> "$filename"done

If you'd like to pull "text" from a file, you could instead do this:

for filename in *.php; do  cat /path/to/sourcefile >> "$filename"done

Now ... you might have files in subdirectories. If so, you could use the find command to find and process them:

find . -name "*.php" -type f -exec sh -c "cat /path/to/sourcefile >> {}" \;

The find command identifies what files using conditions like -name and -type, then the -exec command runs basically the same thing I showed you in the previous "for" loop. The final \; indicates to find that this is the end of arguments to the -exec option.

You can man find for lots more details about this.

The find command is portable and is generally recommended for this kind of activity especially if you want your solution to be portable to other systems. But since you're currently using bash, you may also be able to handle subdirectories using bash's globstar option:

shopt -s globstarfor filename in **/*.php; do  cat /path/to/sourcefile >> "$filename"done

You can man bash and search for "globstar" for more details about this. This option requires bash version 4 or higher.

NOTE: You may have other problems with what you're doing. PHP scripts don't need to end with a ?>, so you might be adding HTML that the script will try to interpret as PHP code.