How to append output to the end of a text file
Use >>
instead of >
when directing output to a file:
your_command >> file_to_append_to
If file_to_append_to
does not exist, it will be created.
Example:
$ echo "hello" > file
$ echo "world" >> file
$ cat file
hello
world
To append
a file use >>
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
To 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.txt
Do this a couple of times and then run:
cat textfile.txt
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.