There are lots of reasons you might track your work. Maybe you do an annual performance review. Maybe you want to post status updates online. In my case, I want to create a monthly mailing list and let people know what I’ve been up to lately.
But, right now, I’m not keeping great track of my work.
I spend much of my day on the command-line and I typically enjoy command-line tools, so lets fix that with a quick bash script.
The syntax I ultimately want to use is something like the following:
log "Something I did today"
At first, I’m going to keep this extremely simple. I’ll append the string to a file. The command to do that would be something like this:
echo "Something I did today" >> ~/monthly.log
To break this down:
means to redirect that to the end of a file, appending to it
Because I want a shorter command, just the word log
followed by my comments, I’m going to put this into my ~/.zshrc
file as an alias. I use a Mac and have my terminal set to the default zsh shell. Something similar should work in Linux and maybe even Windows Subsystem for Linux (WSL). Here’s the alias I’ll use:
alias log="echo $1 >> ~/monthly.log"
This sets up an alias with the name of log
that runs the echo command outlined above. You’ll notice that I added $1
this time. That’s a variable that brings in the first argument after your command. So, I can now use the following command:
log "Something I did today"
The result will be a monthly.log
file in my home directory with that line added to it.
That’s great, and I could stop there, but it will be a little bit manual right now. Every month I would need to rename that file. Instead, I want it to write to a file named after the month automatically.
One way I could do that is by running the date
command. Here’s a command that outputs the date in the format YYYY-dd
.
date +%Y-%m
I can use backticks to run the date command and return its value into the alias above. Here’s how my alias will look after I update it to do that.
alias log="echo $1 >> ~/log/`date +%Y-%m`.log"
This will echo the first argument ($1) to a file named ~/log/2024-10.log
when you run it in October. There is no longer a need to do any updates manually. Your log will be stored in the ~/log
directory as a series of files named after the current month and year.
We’ve added a log
directory to the path but that directory needs to exist before we can write to it. For now, I’ll create the directory manually with the following command.
mkdir ~/log
If you make your own logging command more complex, you’re probably getting to the point where you should write a function instead of an alias. Here’s an example of this updated to a function:
function log() {
if [[ -z "$1" ]]; then
echo "Usage: log \"<message>\""
return 1
fi
echo "- $1" >> ~/log/`date +%Y-%m`.log
}
That’s it. Happy logging!
In an upcoming post I’ll figure out how to send this log as an email to a small number of subscribers.
Written by Joel Dare on October 19, 2024. Originally posted on Hacker News.
Get a monthly digest of what I'm up to.