DEV Community

loading...

Dirty tip: How to remember weird commands you sometimes need

Cserei Zoltán
mens et manus
・2 min read

We all have these cases where an annoying problem pops up every now and then, fixing them thoroughly is not necessarily a priority, you don't have the time to dig into them, you just need a quick fix.

And sometimes these issues repeat themselves and, again, you just have a thousand more important things to do.

Let me give you an example.

Every now and then PostgreSQL stops working on my Mac.

As it turns out, it's caused by a dirty shutdown, like the battery dying. From the scope of this article, it doesn't really matter.

I've learned that the solution to my problem is the following command:

rm -f /usr/local/var/postgres/postmaster.pid

Now I don't know about you, but my brain is not compatible with learning this path. Yet, I have to type it every two weeks or so.

What I do is prefix it with a non-command token. It looks like this:

POSTGRESFIX rm -f /usr/local/var/postgres/postmaster.pid

Now, every time I need this, I just have to type history | grep POSTGRESFIX and voilà. It yield a command not found with no side effects, but it gets stored in your history.

This could mean turning one problem into another: you have to remember the tokens you've used.

What I do is I include FIX in all of them. That way, history | grep FIX will bring up a list of all the commands that I need but I don't like or don't bother to learn.

This is dirty, it might not sound like a professional solution. But sometimes we need to move fast and these things are okay.

Discussion (5)

Collapse
pcmagas profile image
Dimitrios Desyllas

AFAIK Unix-based systems that rely on bash such as Linux and FreeBSD allows you to provide aliases just edit your .bashrc and place the following allias:

alias POSTGRESFIX="rm -f /usr/local/var/postgres/postmaster.pid"

So once you type POSTGRESFIX in shell environment, problem gets fixed.

Collapse
pcmagas profile image
Dimitrios Desyllas • Edited

Also, most GNU/Linux distros using bash, have a specified test file for aliases in your home folder named .bash_aliases, allowing to place any alias declaration there.

Though, I am not too sure in MacOs because Idk how MasOs has setup bash on their OS.

Collapse
moopet profile image
Ben Sinclair

Turn them into aliases!

 history | grep '[A-Z]FIX' | sed -e 's/^[ 0-9]*//' -e 's/ /="/' -e 's/$/"/' >> ~/.bashrc
Collapse
zcserei profile image
Cserei Zoltán Author

this one is really nice, thanks!

Collapse
pcmagas profile image
Dimitrios Desyllas

So I think you can try it on the MAC.