loading...

Check which commands you typed in the command line and don’t repeat yourself!

flaviabastos profile image Flávia Bastos Originally published at flaviabastos.ca on ・2 min read

Recently I needed to repeat a series of long~ish commands in the command line but I never quite memorized them so I ended up resourcing to arrow-up-arrow-up-arrow-up until I found the command I was looking for.

But there is a better and super simple way: history

Typing history in the command line will list all the recent typed commands (I’m not sure how far it goes…) so you don’t need to remember anything. Now the problem is when you have a very long output. You end up scrolling-scrolling-scrolling </ad infinitum>

In this case you can use | grep (pipe (|), the thing on the same key as the \ in your keyboard) to limit the results. For example: I remember that the command I want has the word ‘prune’ in it but I can’t remember the exact command, so I run:

history | grep prune

and I will get only a list of commands that contain the word ‘prune’.

Now that I have the full command I can also save some typing. See that the history output has a number before the list of commands?

6050 docker ps6051 docker images6052 docker rmi c516053 docker ps -a6054 docker image ls6055 docker volume prune -f6056 docker system prune -a6057 docker images6058 docker ps

Well, if you type !command-number, that command will be executed. From my list above, if I type

!6055

The command docker volume prune -f will run again.

It took me a while to starting using these shortcuts. I didn’t see how searching for a command and calling its shortcut was useful until my history got super long and I started using some commands so rarely that it was hard to recall them from memory.

The post Check which commands you typed in the command line and don’t repeat yourself! was originally published at flaviabastos.ca

Discussion

pic
Editor guide
Collapse
manu_seoane profile image
Manu Seoane

There’s a good alternative too. If you remember part of the command, by typing ctrl + r and writing that part, it’ll autocomplete it. For example, if you remember it was something like ssh userX@IP but don’t remember the IP address, the command will search in your history and autocomplete it for you ☺️

Collapse
flaviabastos profile image
Flávia Bastos Author

I didn't know this! Thank you so much for sharing!

Collapse
incognitjoe profile image
Joe Butler

Typing history in the command line will list all the recent typed commands (I’m not sure how far it goes…)

You can control that by setting the HISTSIZE environment variable in your ~/.bashrc. export HISTSIZE=2000 would remember the last 2000 commands. Also note your history isn't automatically written after every command - your .bash_history is updated when the shell exits, if you want to definitely persist whatever you've just done you can force an update with history -a.

Also, minor note of caution, if you're typing passwords or other sensitive info into your shell, they'll go in the history file too :)

Collapse
flaviabastos profile image
Flávia Bastos Author

wow, thank you so much for this extra info. This is great!

Collapse
avalander profile image
Avalander

Nice trick! I didn't know about the history command, thanks for sharing!

Collapse
elijahcorleone profile image
£.j

this certain something nice