How to append output to the end of a text file
Matthew Martinez
How do I append the output of a command to the end of a text file?
012 Answers
Use >> instead of > when directing output to a file:
your_command >> file_to_append_toIf file_to_append_to does not exist, it will be created.
Example:
$ echo "hello" > file
$ echo "world" >> file
$ cat file
hello
world 5 To append a file use >>
echo "hello world" >> read.txt
cat read.txt
echo "hello siva" >> read.txt
cat read.txtthen the output should be
hello world # from 1st echo command
hello world # from 2nd echo command
hello sivaTo overwrite a file use >
echo "hello tom" > read.txt
cat read.txt then the out put is
hello tom
You can use the >> operator. This will append data from a command to the end of a text file.
To test this try running:
echo "Hi this is a test" >> textfile.txtDo this a couple of times and then run:
cat textfile.txtYou'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.txtThis thread has good answers about tee
2Use 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 >> for the whole question:
cmd >> o.txt && [[ $(wc -l <o.txt) -eq 720 ]] && mv o.txt $(date +%F).o.txtthis 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 <o.txt) -eq 720 ]] && mv o.txt $(date +%F).o.txt sleep 3600
done 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 worldHowever 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:
- Use
>>in your shell script to append contents to particular file. The filename can be fixed or using some pattern. - Setup a hourly cronjob to trigger the shell script
For example your file contains :
1. mangesh@001:~$ cat output.txt 1 2 EOFif 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 appendAnd to overwrite contents of file :
3. mangesh@001:~$ echo 'somthing new to write' > output.tx|cat output.tx somthing new to write 1 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" > $fileWARNING: you are reading all lines into memory; memory is a limited resource, so don't go doing this for gigantic files.