15

How can I append a new line to a text file followed by current date and time?

2 Answers 2

16
echo "text" >> file
date >> file

If you want to have just in one line

echo -n "text" >> file
date >> file
3
  • How can I append line breaks? Is there a way that I can pull this off with just one line of commands?
    – Jon Doe
    Jan 6, 2011 at 13:41
  • 4
    @jon-doe You can append line breaks by using echo - by default, it sends a line break; if you want more, you would do (for adding 4 line breaks) echo -e '\n\n\n'. To do it in one line, you can use braces { echo "text"; echo date; } >> file. Jan 6, 2011 at 14:07
  • Adding curly braces didn't work for me. But adding parenthesis worked out. I'm on Ubuntu 12.04. This is what I used - (echo -n "test : "; date;) >> file.log
    – JohnP
    Nov 8, 2014 at 6:37
2

You can also use printf instead. A one line solution would be

printf "\nsome text\n$(date)" >> file.txt

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .