Display a message when you enter a directory

| 2 min read

A few days ago, I was doing something I do from time to time with a CLI tool. The command requires a bunch of parameters, and I usually go through my command history to find the previous usage, change only the part that needs to be changed and run the command. That’s efficient, and that works, but every time I do this, I think to myself: "I should have something simpler, just an easy command that takes only one argument; you should create a bash script."

Last time I finally decided to create that batch script. I opened a new file in my text editor, copied and pasted the command from my bash history, and looked one more time at how to use arguments in a bash script and what is the proper environment-based shebang. Relatively easy. I tried to save my new script, "download" would be a good name for that command, so I entered "download" in the file name input and tried to save.

Error.

Another file already existed with that name. I opened the file and compared it to my new file; they looked identical. It turns out I already automatized my task away and made it simpler but always failed to remember that I did it. This means every time I used the original script, I was losing time and feeling bad about not taking care of it despite having already dealt with the issue.

I already have that script but keep forgetting about it. What could be the next step to avoid the issue and fix the problem once and for all?

I usually entered that directory only to run that one task[1]. Is it possible to be reminded that I already have a script and can use it directly without needing to go through the bash history?

It turns out you can.

The solution is to alias the cd command to a custom command that enters the directory, looks for a file, and displays its content. That’s a dead simple solution to my problem.

Here is the code I took from StackOverflow for the new command and alias creation that you should place in your alias file.

reminder_cd() {
builtin cd "$@" && { [ ! -f .cd-reminder ] || cat .cd-reminder 1>&2; }
}

alias cd=reminder_cd

After that, I added a .cd-reminder file in my project directory with a message explaining that I could use the download command instead of searching and editing past commands.

As a freelancer, I can think of a few usages for that tool. Because I go from project to project, I constantly need to remember which toolkit is used or what command I need to start a project. Now, I can place the information I usually need when I enter the directory in the .cd-reminder file, and boom.

By the way, it’s a good idea to add the .cd-reminder in a global .gititgnore.

I think we can find other usages of that tool for teams. From the top of my mind, I think the quick how-to can work here as well - even if teams in a company should invest in standardizing their tooling to reduce cognitive load. I also believe it could be used to display some messages about the project. Just another way to broadcast information to team members.

It’s still in the experiment phase on my side, but let me know if you find that idea useful and what you’re using it for.


  1. No, I can’t automate it further and run it automatically as I can’t possibly know the parameter before. ↩︎