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: Last Updated on 4.3K 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

James Birkett October 23, 2019 - 11:02 am

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.

Reply
sk October 23, 2019 - 12:01 pm

Good point. I will update this guide with your input. Thanks for pointing it out. Good day!!

Reply
healer August 20, 2021 - 1:12 pm

awesome! i will use this skill in my daily usage

Reply
Ashok Kumar May 29, 2023 - 8:20 pm

it is helpful for freshers to understand

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