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.

9 Answers 9

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
1
  • 3
    Or cat > sometextfile.txt << EOF to overwrite the existing file.
    – mbomb007
    Jul 18, 2019 at 18:52
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.

6
  • 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, 2012 at 3: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, 2012 at 4:03
  • I don't necessarily agree about the UUOC, but you have you point back for the date...
    – jasonwryan
    Jan 27, 2012 at 4:28
  • 6
    +1 nice defensive coding and rationale against the inevitable typo.
    – msw
    Jan 27, 2012 at 8:41
  • 2
    Kyle, you obviously don't have a 90MB .emacs.d. Yikes, I need to clean that up. Jan 30, 2012 at 19:35
21

Just use echo:

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

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

1
  • thank you, echo is what i needed
    – John D
    Sep 3, 2020 at 12:46
3

Lots of neat bells and whistles here!

KISS method:

date >>filename; cat >>filename
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

1
  • Well, screenshots look often very well, but if you can, it is much better if you use copy-paste.
    – peterh
    Nov 23, 2017 at 9:37
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
echo "`date` text here" >> filename

or

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

more on tee

2
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
    

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .