Labels, command labeling in Gnu / Linux to make work easier

about command tags

In the next article we are going to take a look at how can we use labels in terminal. Adding a tag to a Gnu / Linux command can make it a bit easier to reuse. If you are having trouble remembering complex commands or important file system locations, labels can be of great help.

Tags offer users a simple way to associate strings that look like hash tags (#HOME) with the commands we execute on the command line. Once a label is established, we can re-execute the associated command without having to retype it. Instead, we are simply going to have to write the label. The idea is to use labels that are easy to remember, for commands that are complex or that can be annoying to retype.

Unlike setting an alias, the labels are associated with the command history. For this reason, they only remain available if you continue to use them. Once you stop using a tag, it will slowly disappear from the command history. For most users, that means we will be able to type 500 or 1000 commands before this happens. Therefore, tags are a good way to rerun commands that will be useful for a certain period of time, but not for those that we want to be permanently available.

Configure labels in Ubuntu

To configure a label, in a terminal (Ctrl + Alt + T) we will only need to write a command and then add its label at the end. The tag must begin with a # sign and must be immediately followed by a string of letters. This prevents the tag from being treated as part of the command, instead it is handled as a comment that is included in our history file of commands. This is a simple example, although not very useful:

sample tag

echo "Esto es un ejemplo de etiqueta" #TAG

This particular command is now associated with the #TAG tag in our command history. Now if we use the history command, we will see it available:

history tag

history | grep TAG

Then we can rerun this command by typing!? followed by the tag:

reuse TAG command

!? #TAG

The real utility of this is use it when the command we want to execute repeatedly is so complex that it is difficult to remember or simply annoying to type. For example, to list the most recently updated files or directories, we could for example use a tag like #RECIENT and associate it with the appropriate ls command. The following command lists the files in our home directory, regardless of where we are currently on the file system. It lists them in reverse order of date, and shows only the five most recently created or modified files.

command ls -ltr

ls -ltr ~ | tail -5 #RECIENTE

We can re-execute labeled commands using Ctrl + r (hold down the Ctrl key and press the 'r' key) and then write the label (for example, # RECENT). In fact, if you're only using one tag, just typing # after hitting Ctrl-r, the command should appear automatically. The Ctrl + r sequence, as with!?, Searches our command history for the string we write.

Tagging locations

Some users use tags to remember particular file system locations. This makes it easy to get back to the directories we are working on, without having to write the full directory paths.

locations tag

cd /var/www/html #LOCALHOST

In this example, as shown in the previous screenshot, whenever we need to move to the directory associated with #LOCALHOST, we will have a quick way to do it.

It has to be said that Labels do not need to be in uppercase, although this makes them easier to recognize. Also, they are unlikely to conflict with commands or file names that are also in the command history.

Alternatives to labels

While labels can be very useful, there are other ways to do the same things that we can do with them. So that the commands can be repeated in a simple way, also we can assign them to a alias:

recent aliases

alias recientes=”ls -ltr ~ | tail -5”

To make multiple commands easy to repeat, we can also convert them into a script. If we open a .sh file with the following command:

sudo vim archivosActualizados.sh

And inside we place the following lines, we can see the same result as if we used the previous alias:

#!/bin/bash
echo “Most recently updated files:”
ls -ltr ~ | tail -5

We can also rerun recent commands by searching for it with the history command:

tail history command

hitory | tail -20

Once located just write! followed by the number to the left of the command that we want to rerun (for example; ! 8).


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   computer scientist on call said

    Look, I spend hours in the terminal but, of course, I did not know the possibility of using labels?

    Which, on the other hand, seems like a very smart (and useful) way to use comments (I mean it by the #) on the command line.

    Thank you very much for opening this window for me. Surely I am going to use it a lot?