Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
add comment

8 Answers

up vote 19 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.

share|improve this answer
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
3  
+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
show 1 more comment

Just use echo:

echo $(date) Hi. >> notes.txt
share|improve this answer
add comment

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

cat >> sometextfile.txt << EOF
text
more text
and another line
EOF
share|improve this answer
add comment

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

share|improve this answer
add comment

Lots of neat bells and whistles here!

KISS method:

date >>filename; cat >>filename
share|improve this answer
add comment

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
share|improve this answer
add comment
echo "`date` text here" >> filename

or

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

more on tee

share|improve this answer
    
single backtick only –  Balaswamy vaddeman Jan 27 '12 at 5:37
    
For what it's worth, I would recommend using $(..) over the backticks for command substitution. More info: unix.stackexchange.com/questions/11576/… –  rahmu Jan 27 '12 at 10:24
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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