14

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

15
echo "text" >> file
date >> file

If you want to have just in one line

echo -n "text" >> file
date >> file
  • 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 '11 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. – Shawn J. Goff Jan 6 '11 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 '14 at 6:37
2

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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