Writer and Free Thinker at Large
Navigate * LSA Book * Quotes/Reviews * Buy The Book * Chapter Links * WFTL Home
Other Info * Tips & Tricks * Knoppix CD Help * Linux Links * WFTL Chats * Join WFTL-LUG
Moving to Linux: Kiss the Blue Screen of Death Goodbye!
Buy the book from:

Download an excerpt from the book. Click here

An Automatic Daily Journal Script

Every once in a while, I get on this thing about how cool (and powerful) the shell is. With a little knowledge, you can do all sorts of great things. For instance, starting up the editor of your choice to open up a work file with the day's date automatically generated. This is actually the sort of thing I do all the time. For instance, today's journal would be in a directory called /home/marcel/Daily_Journal/Saturday_Jan_17_2004.

Let's step back. Open up a shell prompt and type the command "date". The result will look something like this.

     $ date
     Sat Jan 17 16:28:46 EST 2004

"Nothing special there", you say. Well, what if you wanted to know the date 12 days ago. Use the "-d" modifier.

     $ date -d "12 days ago"
     Mon Jan  5 16:33:01 EST 2004

It gets more interesting, doesn't it? You can also modify the output of the date command with the plus sign modifier. For instance.

     $ date +%A -d "12 days ago"
     Monday

The "%A" tells the date command to print out the long for of the day (ie: Monday instead of Mon). Check the man pages and you will see a whole lot of these modifiers. If I wanted to have the day in long form, the month in 3 letter short form, followed by the day and the year in 4 digit form, I would use the following command.

     $ date +"%A %b %d %G"
     Saturday Jan 17 2004

I put quotes around the modifiers because I wanted spaces in between each one. Had I wanted underscores, it would not have been necessary. So far so good. Let's say that I wanted a command to call my favorite editor (whatever it might be) to open a daily journal in the form of Saturday_Jan_17_2004, I could put the whole thing in a script file like this.

     #!/bin/bash
     #
     # A quick call to the KDE kwrite editor to open the daily journal
     #
     kwrite /home/marcel/Daily_Journal/`date +%A_%b_%d_%G`

Notice the back quotes around the date command -- very important. I used the kwrite command here, but you could use vi, pico, nedit, or whatever editor you feel comfortable with. When I wrote this script, I called it "think" and saved it in /usr/local/bin. Then, I made the file executable with "chmod +x /usr/local/bin/think". All I have to do now is call the command "think" and it magically opens a pre-named daily journal text file.

We now return you to our regularly scheduled Linux work.