Linux echo append to file

How to Append Text to End of File in Linux

While working with configuration files in Linux, sometimes you need to append text such as configuration parameters to an existing file. To append simply means to add text to the end or bottom of a file.

In this short article, you will learn different ways to append 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.

# echo "/mnt/pg_master/wal_archives 10.20.20.5(rw,sync,no_root_squash)" >> /etc/exports

Alternatively, you can use the printf command (do not forget to use \n character to add the next line).

# printf "/mnt/pg_master/wal_archives 10.20.20.5(rw,sync,no_root_squash)\n" >> /etc/exports

You can also use the cat command to concatenate text from one or more files and append it to another file.

In the following example, the additional file system shares to be appended in the /etc/exports configuration file are added in a text file called shares.txt.

# cat /etc/exports # cat shares.txt # cat shares.txt >> /etc/exports # cat /etc/exports

Append Files to /etc/exports

Besides, you can also use the following here document to append the configuration text to the end of the file as shown.

# cat /etc/exports # cat >>/etc/exports /backups 10.20.20.0/24(rw,sync) > /mnt/nfs_all 10.20.20.5(rw,sync) > EOF # cat /etc/exports

Append Text Using here Document

Attention: Do not mistake the > redirection operator for >> ; using > with an existing file will delete the contents of that file and then overwrites it. This may result in data loss.

Append Text Using tee Command

The tee command copies text from standard input and pastes/writes it to standard output and files. You can use its -a flag to append text to the end of a file as shown.

# echo "/mnt/pg_master/wal_archives 10.20.20.5(rw,sync,no_root_squash)" | tee -a /etc/exports OR # cat shares.txt | tee -a /etc/exports

Append Text Using Tee Command

You can also use a here document with the tee command.

# cat /backups 10.20.20.0/24(rw,sync) >/mnt/nfs_all 10.20.20.5(rw,sync) EOF

Append Text Using Here and Tee Command

You might also like to read these related articles.

That’s it! You have learned how to append text to the end of a file in Linux. If you have questions or thoughts to share, reach us via the feedback form below.

Источник

How to append output to the end of a text file

If file_to_append_to does not exist, it will be created.

$ echo "hello" > file $ echo "world" >> file $ cat file hello world 

The problem is that echo removes the newlines from the string. How do you append to a file a string which contains newlines?

Читайте также:  Groupadd linux для чего

echo does not remove newlines from the string. If you fail to properly quote the argument, then the shell will split the string and pass arguments to echo and echo never even sees the newlines.

@Pmpr note that echo is not part of the solution, it is only part of the example I typed, and there are no escape sequences in the example.

echo "hello world" >> read.txt cat read.txt echo "hello siva" >> read.txt cat read.txt 

then the output should be

hello world # from 1st echo command hello world # from 2nd echo command hello siva 
echo "hello tom" > read.txt cat read.txt 

You can use the >> operator. This will append data from a command to the end of a text file.

echo "Hi this is a test" >> textfile.txt 

Do this a couple of times and then run:

You’ll see your text has been appended several times to the textfile.txt file.

Use command >> file_to_append_to to append to a file.

For example echo «Hello» >> testFile.txt

CAUTION: if you only use a single > you will overwrite the contents of the file. To ensure that doesn’t ever happen, you can add set -o noclobber to your .bashrc .

This ensures that if you accidentally type command > file_to_append_to to an existing file, it will alert you that the file exists already. Sample error message: file exists: testFile.txt

Thus, when you use > it will only allow you to create a new file, not overwrite an existing file.

Using tee with option -a (—append) allows you to append to multiple files at once and also to use sudo (very useful when appending to protected files). Besides that, it is interesting if you need to use other shells besides bash, as not all shells support the > and >> operators

echo "hello world" | sudo tee -a output.txt 

This thread has good answers about tee

Use the >> operator to append text to a file.

I often confuse the two. Better to remember through their output:

> for Overwrite

$ touch someFile.txt $ echo ">" > someFile.txt $ cat someFile.txt > $ echo ">" > someFile.txt $ cat someFile.txt > 

>> for Append

$ echo ">" > someFile.txt $ cat someFile.txt > $ echo ">" >> someFile.txt $ cat someFile.txt >> 

this will append 720 lines (30*24) into o.txt and after will rename the file based on the current date.

Run the above with the cron every hour, or

while : do cmd >> o.txt && [[ $(wc -l  

I would use printf instead of echo because it's more reliable and processes formatting such as new line \n properly.

This example produces an output similar to echo in previous examples:

printf "hello world" >> read.txt cat read.txt hello world 

However if you were to replace printf with echo in this example, echo would treat \n as a string, thus ignoring the intent

printf "hello\nworld" >> read.txt cat read.txt hello world 

I'd suggest you do two things:

  1. Use >> in your shell script to append contents to particular file. The filename can be fixed or using some pattern.
  2. Setup a hourly cronjob to trigger the shell script
Читайте также:  Copy windows folder to linux

For example your file contains :

 1. mangesh@001:~$ cat output.txt 1 2 EOF 

if u want to append at end of file then ---->remember spaces between 'text' >> 'filename'

 2. mangesh@001:~$ echo somthing to append >> output.txt|cat output.txt 1 2 EOF somthing to append 

And to overwrite contents of file :

 3. mangesh@001:~$ echo 'somthing new to write' > output.tx|cat output.tx somthing new to write 

This is misleading in many details. Spaces are not important and piping an empty output to cat is . just completely wacky. (It's empty because you just redirected standard output to a file.)

In Linux, You can use cat command to append file content to another file

In the previous command you will append content of fileName_1.txt to fileName_2.txt .

In Windows OS you can use type command

type fileName_1.txt >> fileName_2.txt

While all of these answers are technically correct that appending to a file with >> is generally the way to go, note that if you use this in a loop when for example parsing/processing a file and append each line to the resulting file, this might be much slower then you would expect.

A faster alternative might be this:

stringBuilder="" while read -r line; do # $'\n' prints a newline so we don't have to know what special chars the string contains stringBuilder+="$line"$'\n' done < "myFile.txt" echo "$stringBuilder" >$file 

WARNING: you are reading all lines into memory; memory is a limited resource, so don't go doing this for gigantic files.

Источник

How to append a line to a file in bash

Sometimes we need to work with a file for programming purposes, and the new line requires to add at the end of the file. This appending task can be done by using ‘echo‘ and ‘tee‘ commands. Using ‘>>’ with ‘echo’ command appends a line to a file. Another way is to use ‘echo,’ pipe(|), and ‘tee’ commands to add content to a file. How these commands can be used in the bash script are shown in this article.

Create a text file named books.txt with the following content to do the examples shown in the next part of this article.

Example-1: Append line to the file using ‘echo’ command and ‘>>’ symbol

In the following script, an existing file, books.txt is assigned to the variable, filename, and a string value will be taken as input from the user to add at the end of the file. If the input value is not empty, then the ‘echo’ command will append the value into the books.txt file by using ‘>>’ symbol.

Читайте также:  Переключить видеокарту на ноутбуке linux

# Define the filename
filename = 'books.txt'

# Type the text that you want to append
read -p "Enter the text that you want to append:" newtext

# Check the new text is empty or not
if [ " $newtext " ! = "" ] ; then
# Append the text by using '>>' symbol
echo $newtext >> $filename
fi

Learning JQuery‘ is taken as a new text value in the output that is appended at the end of the file.

Example-2: Append line to the file using ‘printf’ command and ‘>>’ symbol

>>’ symbol can be used with the ‘printf’ command to append formatted content to a file. Like the previous example, the filename and the string values are assigned to the variables, filename, and newtext. Next, ‘printf’ command will redirect the value of newtext with other text into the end of the books.txt file.

# Define the filename
filename = 'books.txt'

# Type the text that you want to append
read -p "Enter the text that you want to append:" newtext

# Check the new text is empty or not
if [ " $newtext " ! = "" ] ; then
# Append the text by using '>>' symbol
printf "Appended text is: %s \n " " $newtext " >> $filename
fi

Website by WordPress‘ is taken as a new text value in the output that is appended at the end of the file.

Example-3: Append line to the file using `tee` command

tee’ is another useful command to append any string into a file. In the following script, the filename and the new text values are assigned like the previous examples. If the text value is not empty, then the ‘echo’ command will send the value to the ‘tee’ command using ‘|’ symbol. ‘-a’ option is used with ‘tee’ command here to append the received input value to the file books.txt. ‘/dev/null’ is used in the script to prevent showing the output in the terminal.

# Define the filename
filename = 'books.txt'

# Type the text that you want to append
read -p "Enter the text that you want to append:" newtext

# Check the new text is empty or not
if [ $newtext ! = "" ] ; then
# Append the text by using `tee` command
echo $newtext | tee -a $filename > / dev / null
fi

Learning CSS3‘ is taken as a new text value in the output that is appended at the end of the file.

Conclusion:

Three different ways are shown in this article to append text at the end of a file using a bash script.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.

Источник

Оцените статью
Adblock
detector