Journaling With Linux

Standard

I have found journaling to have a lot of benefits. It’s a form of life documentation, and it helps me solidify and build memories of the experiences I have gone through. Also, it gives me a fun activity to keep my writing skills sharp on a daily basis.

I like creating digital copies of everything whenever I can. Information is stored more efficiently and conveniently on a memory cell or on a hard drive platter than it does on a notebook. Creating a journal on a computer can be as simple as using Microsoft Word and saving word documents; however, I personally prefer a more simplistic approach that isn’t dependent on Microsoft Word, Libre Office, or one of many journaling applications out there. Something I can create and have direct control over; something that follows the UNIX philosophy.

Getting Started: Basics of Journaling the Linux Way

A list of files in a directory; they are all plain text files.

Plain Text Files

All of my journals are stored in plain text files. It’s done that way, as it makes the files really easy to work with in the terminal. Whatever editor one likes works; I personally use vi, but I’ll have nano in the commands ahead. Simple, huh? To get started one just has to create the directories, and type in the command line:

nano ~/journal/generaljournal/2013-07-03.txt

There are some shortcuts that make this easier. Needing to type that long command to get started is not very efficient. There’s a better way using alias command.

alias dream="nano ~/journal/dreamlog/"$(date +"%Y-%m-%d")".txt"
alias grat="nano ~/journal/gratitudelog/"$(date +"%Y-%m-%d")".txt"
alias journal="nano ~/journal/generaljournal/"$(date +"%Y-%m-%d")".txt"

These aliases creates a quick shortcut that automatically starts editing the appropriate and current day’s journal. There’s a command substitution using the ‘date’ command to automatically name the file the correct date. To make the alias commands permanent, add it to one’s .bashrc file:

A .bashrc file with various alias commands

Alias commands in the .bashrc file

I have different commands for my different journals. The dream command is for my dream log, the grat command is for my gratitude log (Gratitude should be something felt on more than a single day of the year, Thanksgiving), and journal is for my general journal. Whenever I start a new shell, I can immediately get started by simply typing the name of the journal I want to work on.

Reading, Searching, and Getting Random Journals

There are additional handy shortcuts and techniques that can be used to make everything better.

What if I want to read one of my journals like I would in a notebook, and read through the entries in sequence? This is pretty easy with the right commands, and a little know-how on regular expressions. Here’s an example:

tail -n +0 ~/journal/generaljournal/2013-06-*.txt | less
Showing what tail piped into less looks like

Output of tail piped into less

The tail command can be used to follow a series of text files, -n +0 makes tail follow the entire file, and piping that into less reads through the entries one at a time. Regular expressions can be used to match a specific year, month, or set of days. In the example above, the regular expression matches everything in June 2013.

Moving along, what if I want to search one of my journals for a specific key term?

grep -i "Happy" ~/journal/generaljournal/2013-06-*.txt
A happy journal entry

Output of grep

The grep command searches through the series of text files for the term used. The -i option makes the search case insensitive. No index is needed. If I want to search for a specific place or person, I can do so without shuffling through hundreds of journal entries.

Let’s say I want to pick out a random journal entry to read? This command is a little more complex, but it can be done with a little cleverness:

less $(ls ~/journal/generaljournal/2013-06-*.txt | shuf -n1)

This command works using ls to list the directory contents. Piping that into shuf -n1 picks one of the files at random. The file that is picked gets read using less.

I know a lot of people who use the fortune command to get a little surprise whenever they open up the terminal. In my case, I use the above command to pick a random entry from my gratitude log to help remind me of the amazing life I’ve lived. Only difference is that cat is used instead of less to keep my shell usable when it starts up.

Shortcuts for Reading, Searching, and getting Random Journals

Remember how alias commands were used to make writing to each journal easier? What about the three examples up above? Alias commands won’t work very well since the commands used above are dependent on parameters and regular expressions. Thankfully, bash has something called a shell function that was made exactly for this purpose!

dreamread() { tail -n +0 ~/journal/dreamlog/$1.txt | less ; }
gratread() { tail -n +0 ~/journal/gratitudelog/$1.txt | less ; }
journalread() { tail -n +0 ~/journal/generaljournal/$1.txt | less ; }
dreamsearch() { grep -i $1 ~/journal/dreamlog/$2.txt ; }
gratsearch() { grep -i $1 ~/journal/gratitudelog/$2.txt ; }
journalsearch() { grep -i $1 ~/journal/generaljournal/$2.txt ; }
dreamrandom() { less $(ls ~/journal/dreamlog/$1.txt | shuf -n1) ; }
gratrandom() { less $(ls ~/journal/gratitudelog/$1.txt | shuf -n1) ; }
journalrandom() { less $(ls ~/journal/generaljournal/$1.txt | shuf -n1) ; }
All the shell functions showcased in the .bashrc file

All the shell functions in .bashrc

Everything so far is now in a nice shortcut. Here are examples of the new shell functions being used:

dreamread 2013-03-*
gratrandom 2013-03-*
journalsearch linux 2013-*

Notes About Securing the Journal

Naturally, a personal journal will probably have a lot of sensitive information that would be awful if it were released publicly. Just as a teenage girl doesn’t want her younger brother reading her private journal, I don’t want other users snooping and reading mine.

The file permissions for the journal should be set properly:

chmod 700 ~/journal -R
chmod g+s ~/journal -R

The first command sets the file permissions of the journal directory to read, write, and execute by the owner only. The second command enables the SGID bit, which causes all new files added to the journal to inherit the correct file permissions.

If setting file permissions isn’t enough, the next step is to look into encryption. I’m going to recommend using Truecrypt, and either securing the entire hard drive, or creating a Truecrypt Volume to contain the journal.

Obviously, there’s a tradeoff between security and convenience. If one encrypts the entire hard drive, they have to put in the password when you turn on your computer. If one creates a Truecrypt Volume, they have to mount it every time one wants to edit the journal.