721

How do I append the output of a command to the end of a text file?

0

13 Answers 13

941

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
5
  • 8
    The problem is that echo removes the newlines from the string. How do you append to a file a string which contains newlines? Dec 15, 2017 at 21:25
  • 8
    @TimothySwan, I believe with the -e option. Mar 5, 2018 at 7:44
  • 2
    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. Jan 29, 2020 at 23:23
  • 1
    echo may not expand escape sequences. Use printf instead Apr 2, 2021 at 15:46
  • 1
    @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.
    – aioobe
    Apr 2, 2021 at 18:23
175

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

81

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.

60

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.

52

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

3
  • 3
    This is the best way to append something with a new line.
    – M_R_K
    Aug 2, 2019 at 12:00
  • 4
    was looking for a way to append to protected files and this solves it
    – Woootiness
    Dec 6, 2019 at 9:52
  • The addition of sudo is irrelevant here, and possibly dangerously misleading. Mar 6 at 17:14
28

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

24

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
  >>
15

for the whole question:

cmd >> o.txt && [[ $(wc -l <o.txt) -eq 720 ]] && mv o.txt $(date +%F).o.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 <o.txt) -eq 720 ]] && mv o.txt $(date +%F).o.txt
    sleep 3600
done
15

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
8

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
8

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
1
  • 1
    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.)
    – tripleee
    Oct 24, 2018 at 4:24
4

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

cat fileName_1.txt >> fileName_2.txt

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

See this gif image:

enter image description here

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" > $file

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.