We can't remember the Linux commands that we don't use regularly. Also, it is bit difficult to remember a long command with many flags and arguments. Luckily, there are a few good tools exists to easily retrieve the commands that we rarely use. For instance, the Keep utility allows you to save the Linux commands and use them on demand. The Bashpast CLI tool will help to bookmark the Linux commands for easier repeated invocation. There is also another command line snippet manager named Pet to save the important, long and complex command snippets. All of the aforementioned tools can help anyone who struggles to remember the Linux commands. Today, I've learned a new trick to remember any lengthy Linux commands. In this method, we don't need to install any additional applications. All we have to do is just apply tags to Linux commands to easily retrieve them from history.
The tag is nothing but a comment. The comment can be any word(s) that reminds you about a Linux command. After applying tags to the Linux commands, we don't even need to type the actual command. Instead, we simply type the tag and easily retrieve the tagged commands from the Bash history and run them immediately. Allow me to show you some examples.
How to apply tags to Linux commands
Let us take the following one-liner Linux command as an example.
$ find . -size +10M -type f -print0 | xargs -0 ls -Ssh | sort -z
For those wondering, the above command will find and list files bigger than 10 MB in the current directory and sort them by size. I admit that I couldn't remember this command. I guess some of you can't remember this command either. This is why we are going to apply a tag to such kind of commands.
To apply a tag, just type the command and add the comment ( i.e. tag) at the end of the command as shown below.
$ find . -size +10M -type f -print0 | xargs -0 ls -Ssh | sort -z #ListFilesBiggerThanXSize
#ListFilesBiggerThanXSize is the tag name to the above command. Make sure you have given a space between the command and tag name. Also, please use the tag name as simple, short and clear as possible to easily remember it later. Otherwise, you may need another tool to recall the tags.
To run it again, simply use the tag name like below.
$ !? #ListFilesBiggerThanXSize
! (Exclamation mark) and
? (Question mark) operators are used to fetch and run the command which we tagged earlier from the BASH history.
Here is the output of the above command:
See? You don't need to type the full command. Just type these two charterers
!? followed by the tag name.
Use different and simple tag names for complex and lengthy commands. If you use the same tag name to multiple commands, it will run the most recently tagged command.
I have been using this simple trick from time to time. It may not be an effective method to all of you, but I find it easier and it's enough for me to easily recall a forgotten Linux commands.
Do you know any other tips and tricks? Please share them in the comment section below. I will check and update this guide accordingly.
As many readers have pointed out in the comment section below, we can also use alias command to make this task easier and simpler. I am aware of alias command and how and where to use it. However, it is not the scope of this guide. I have amended this guide as many users have suggested to create aliases to commands hard to remember.
Create an alias for commands which are hard to remember like below.
$ alias ListFilesBiggerThanXSize='find . -size +10M -type f -print0 | xargs -0 ls -Ssh | sort -z'
Now, run the following command to list files bigger than 10 MB in current directory and sort them by size:
It is simple as that.
And what’s the benefit of using additional software that doesn’t come out of the box and may not exist for every distribution instead of simply creating aliases?
That’s point of this guide. If you don’t want to use additional software, just tag them as described in this article.
Rather than !?, it’s much easier to use Ctrl-R (reverse-i-search), which is also a standard feature that’s built into most shells. Ctrl-R gives you live preview of what will run during recall as you type, and you can modify the commands before running them as well.
Just make an alias or script if it’s something you use a lot
It would probably help if your example of how to tag commands actually including the tag
Yes aliases are a way to do this as well but I love that there is another way to do this
you can just use alias’ to run the hard to remember commands, won’t need the !? Prefix
For longer tasks that may need additional arguments/parameters, use a bash script…
Thanks for sharing. Cool technique!
I may not have read accurately how you proposed to do this, I apologize: It is indeed a neat trick. In any case, since I haven’t read past articles in which you may have mentioned this already, you may want to point out that aliases are a viable alternative, too.
Edit .bascrc and add an alias.
Cool trick! Out of the box thinking in multiple ways 😉
I addressed this issue by maintaining a repo of knowledge that I’ve found useful, but I can see how your trick here would be a great companion to that!
I appreciate things that can be done without apt-get!
You can also use ctrl+r to search for the comment tag interactively, allowing you to edit the command before running it again, which is useful if you just need to change a parameter such an an IP address in a complex command.
Good point. I will update this guide with your input. Thanks for pointing it out. Good day!!
awesome! i will use this skill in my daily usage
it is helpful for freshers to understand