Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append file contents to the bottom of existing file in Bash [duplicate]

Tags:

bash

append

sed

awk

Possible Duplicate:
Shell script to append text to each file?
How to append output to the end of text file in SHELL Script?

I'm trying to work out the best way to insert api details into a pre-existing config. I thought about using sed to insert the contents of the api text file to the bottom of the config.inc file. I've started the script but it doesn't work and it wipes the file.

#!/bin/bash  CONFIG=/home/user/config.inc API=/home/user/api.txt  sed -e "\$a $API" > $CONFIG 

What am I doing wrong?

like image 958
Grimlockz Avatar asked Nov 01 '12 16:11

Grimlockz


People also ask

How do I append to the end of a 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 a file to an existing file in Linux?

You can use cat with redirection to append a file to another file. You do this by using the append redirection symbol, ``>>''. To append one file to the end of another, type cat, the file you want to append, then >>, then the file you want to append to, and press <Enter>.

Which command is used to append new contents at the end of the existing file?

The “>>” operator is used to append text to the end of a file that already has content.

How do I add text to the end of a file in Linux?

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.


1 Answers

This should work:

 cat "$API" >> "$CONFIG" 

You need to use the >> operator to append to a file. Redirecting with > causes the file to be overwritten. (truncated).

like image 75
William Pursell Avatar answered Oct 18 '22 09:10

William Pursell