Occasionally I have a thought that I want to write into a file while I am at the terminal. I would want these notes all in the same file, just listed one after the other. I would also like a date / time tag on each one.

Is it possible to do this without having to open the file each time? Can I just enter it into the terminal and have it appended to the file each time with a command or script?

I am using GNU BASH.

up vote 22 down vote accepted

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.

  • 1
    How does the n function write to the file? Don't you mean something like a echo "$*" rather than UUOC? How does it incorporate the date/time tag? – jasonwryan Jan 27 '12 at 3:54
  • Not quite useless. It reads from stdin, which is where the user will be typing their note. I did forget the datestamp, though. – Kyle Jones Jan 27 '12 at 4:03
  • I don't necessarily agree about the UUOC, but you have you point back for the date... – jasonwryan Jan 27 '12 at 4:28
  • 4
    +1 nice defensive coding and rationale against the inevitable typo. – msw Jan 27 '12 at 8:41
  • 2
    Kyle, you obviously don't have a 90MB .emacs.d. Yikes, I need to clean that up. – quodlibetor Jan 30 '12 at 19:35

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

Lots of neat bells and whistles here!

KISS method:

date >>filename; cat >>filename

If you have ruby installed on your computer you can use https://github.com/minhajuddin/taskr . This way you get a nice view of your notes with tags and the hours elapsed.

Taskr screenshot

  • Well, screenshots look often very well, but if you can, it is much better if you use copy-paste. – peterh Nov 23 '17 at 9:37

Depending on your needs, syslogd might be another tool to peruse. The command

theuser@tetrad:~$ logger This message is sent to syslog

will log the message with the facility user.notice. With many Linux systems this will be enough to have a file /var/log/user.log opened and appended to, with others you may need to define a handling for that facility and log level (or, whichever facility you choose - the local0 to local7 facilities are usually free to assign to things like this.

It's got the benefit of being able (aka configurable) to send notes from client machines to a central logging server, something I like to use for keeping track of administrative action since it preserves timestamp, user and host information automagically, while keeping actions in order.

resulting output in local file:

theuser@tetrad:~$ tail /var/log/user.log
Jan 31 07:18:37 tetrad theuser: This message is sent to syslog

Example for syslog configuration line on Solaris:

local5.notice             ifdef(`LOGHOST', /var/log/diary, @loghost)

Note: The ifdef is preprocessed with m4, on the machine with the hostname/hostalias "loghost", the messages will be logged to the file /var/log/diary, on all others, they will be sent to the remote syslog service at loghost. To test this kind of configuration, the config file can be sent through m4 for expansion (leave away the -D LOGHOST to see how it would look on a system not called loghost:

theuser@solstice$ /usr/ccs/bin/m4 -D LOGHOST /etc/syslog.conf

Taskwarrior may do what you wanted a little better than a simple shell script. The 30-second Tutorial should tell you if I am right or wrong.

echo "`date` text here" >> filename

or

echo "text here `date`"|tee -a filename

more on tee

Combining a few different answers to achieve what I sought out, which was multiple options.

  1. Quick
    user@hostname:~/Documents$ echo text >> filename
    user@hostname:~/Documents$ cat filename
    text
    
  2. Multiple Lines
    user@hostname:~/Documents$ cat >> filename2 << EOF
    > text line 1
    > text line 2
    > text line 3
    > EOF
    user@hostname:~/Documents$ cat filename2  
    text line 1
    text line 2
    text line 3
    
  1. Modify a file at a given line number

    user@hostname:~/Documents$ sed -i '2iinserting new line' filename2
    user@hostname:~/Documents$ cat filename2
    text line 1
    inserting new line
    text line 2
    text line 3
    

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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