i was wondering weather is it possible to append data in a file without using cat command.
I've considered using sed to append data , but as of my knowledge sed only operates after loading the full data into the memory. please do correct me if i'm wrong on this.
2 Answers
If you want to append data to a file, you can simply use the append I/O-redirection >>. For instance:
echo "first line" > file
echo "next line" >> file
Or you could append an entire file
echo "$(<otherfile)" >> file
This command is however not advisable since it will load the entire file first into memory.
A better way is to use tee:
tee < otherfile >> file
3 Comments
bongboy
but echo to display the content of a file, shouldn't you have to pass that content as an argument to echo ie. echo "$cat file" >>filename ??
mata
echo does NOT read from stdin, so echo < somefile >> file won't work, it will only append an empty line to file.willeM_ Van Onsem
@mata: That was indeed a mistake, don't know why that happened. Anyway now the answer provides a way to do this with
echo although that's not advisable; and shows to use tee as well..
>>? >> is used for appending data rather than > which will override.cat). Both tasks are slightly different...