Is there a way to write a BASH script that will append a string to every file in a directory?
e.g., I want to append the string "test" to every .html file in the current working directory I'm in; something like:
echo "test" >> *.html
But of course this doesn't work.
Type the cat command followed by the file or files you want to add to the end of an existing file. Then, type two output redirection symbols ( >> ) followed by the name of the existing file you want to add to.
bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.
To make a new file in Bash, you normally use > for redirection, but to append to an existing file, you would use >> . Take a look at the examples below to see how it works. To append some text to the end of a file, you can use echo and redirect the output to be appended to a file.
Nevermind, I figured it out.
#!/bin/sh
for f in *.html ; do
echo "test" >> $f
done
tee is good for these sorts of things.
echo "test" | tee -a *.html
sed -i.bak '$a append' *.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With