What's the quickest way to add text to a file from the command line?

Write yourself a shell script called "n". Put this in it:

#!/bin/sh
notefile=/home/me/notefile
date >> $notefile
emacs $notefile -f end-of-buffer

I recommend this instead of cat >> notefile because:

  1. One day you'll be in such a hurry that you'll fumblefinger the >> and type > instead and blow away your file.
  2. Emacs starts in five one-hundredths of a second on my Mac Mini. It takes a tenth of a second to start on a ten year old Celeron-based system I have sitting around. If you can't wait that long to start typing, then you're already a machine and don't need to take notes. :)

If you insist on avoiding a text editor, use a shell function:

n () { date >> /home/me/notefile; cat >> /home/me/notefile; }

which should work in all shells claiming Bourne shell compatibility.


Also, to write multiple lines to a file from the command line, do:

cat >> sometextfile.txt << EOF
text
more text
and another line
EOF

Just use echo:

echo $(date) Hi. >> notes.txt

You can use >> to append to a file, or use > to overwrite it.