Home Linux Tips & Tricks Apply Tags To Linux Commands To Easily Retrieve Them From History

Apply Tags To Linux Commands To Easily Retrieve Them From History

By sk
Published: Updated: 4.5K views

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.

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

Here, #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

The ! (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:

Apply Tags To Linux Commands To Easily Retrieve Them From History On Linux
Apply Tags To Linux Commands To Easily Retrieve Them From History On Linux

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.

Update:

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:

$ ListFilesBiggerThanXSize

It is simple as that.

You May Also Like

15 comments

Experienced Admin October 22, 2019 - 9:59 am

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?

Reply
sk October 22, 2019 - 12:36 pm

That’s point of this guide. If you don’t want to use additional software, just tag them as described in this article.

Reply
lie October 22, 2019 - 5:17 pm

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.

Reply
Andrew Woodhead October 22, 2019 - 5:21 pm

Just make an alias or script if it’s something you use a lot

Reply
Charles Martin October 22, 2019 - 6:40 pm

It would probably help if your example of how to tag commands actually including the tag

Reply
Andrew October 22, 2019 - 6:57 pm

Yes aliases are a way to do this as well but I love that there is another way to do this

Reply
Cj October 22, 2019 - 8:34 pm

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…

Reply
Deys October 22, 2019 - 11:03 pm

Thanks for sharing. Cool technique!

Reply
Experienced Admin October 22, 2019 - 11:05 pm

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.

Reply
Bill October 23, 2019 - 2:35 am

Edit .bascrc and add an alias.

Reply
LT 'syreal' Jones October 23, 2019 - 4:55 am

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!

Reply
1 2

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More