42

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.

Brian Dant
  • 1,005

9 Answers9

36

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

cat >> sometextfile.txt << EOF
text
more text
and another line
EOF
Renan
  • 17,136
30

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.

Kyle Jones
  • 15,015
  • 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 03:54
  • 1
    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 04:03
  • I don't necessarily agree about the UUOC, but you have you point back for the date... – jasonwryan Jan 27 '12 at 04:28
  • 6
    +1 nice defensive coding and rationale against the inevitable typo. – msw Jan 27 '12 at 08: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
  • Esepcially good because it doesn't force neophytes to memorize that >> means append, because if they had to type that in over and over, soon the day would come when they typed > instead. Oops! – Michael Dillon Feb 01 '12 at 07:59
23

Just use echo:

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

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

iggy12345
  • 123
3

Lots of neat bells and whistles here!

KISS method:

date >>filename; cat >>filename
Mat
  • 52,586
2

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 09:37
1
echo "`date` text here" >> filename

or

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

more on tee

manatwork
  • 31,277
  • single backtick only – Balaswamy vaddeman Jan 27 '12 at 05:37
  • 1
    For what it's worth, I would recommend using $(..) over the backticks for command substitution. More info: http://unix.stackexchange.com/questions/11576/why-the-double-quotes-and-backquotes-in-a-shell-script – rahmu Jan 27 '12 at 10:24
1

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
1

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.

1

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