Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell script to append text to each file?

I have a folder full of text files. I need to append the same block of text to each of them (and of course overwrite the original file).

I was wondering what the correct Bash shell syntax would be for this. Would I use cat?

I have done a few batch scripts but I'm not a Bash expert. Any suggestions appreciated.

like image 623
Steve Avatar asked Apr 07 '11 19:04

Steve


People also ask

How do you append to a file in shell?

Append Text Using >> Operator The >> operator redirects output to a file, if the file doesn't exist, it is created but if it exists, the output will be appended at the end of the file. For example, you can use the echo command to append the text to the end of the file as shown.

How do I append to a text file in bash?

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.

How do I append to a text file in Linux?

You need to use the >> to append text to end of file. It is also useful to redirect and append/add line to end of file on Linux or Unix-like system.


1 Answers

Use append redirection.

for f in *.txt do   cat footer >> "$f" done 
like image 102
Ignacio Vazquez-Abrams Avatar answered Oct 13 '22 02:10

Ignacio Vazquez-Abrams