- Upgrade to Windows 11 Pro for $18 - the lowest price this year
- One of the most reliable power banks I've tested can even inflate car tires (and get 50% off in this Black Friday deal)
- This is the smartest electronic precision screwdriver I've ever tested (and now get 10% off for Black Friday)
- How Ransomware Jeopardizes Healthcare Organizations
- Buy a Microsoft Office 2019 license for Mac or Windows for $27
Tagging commands on Linux
Tags provide an easy way to associate strings that look like hash tags (e.g., #HOME) with commands that you run on the command line. Once a tag is established, you can rerun the associated command without having to retype it. Instead, you simply type the tag. The idea is to use tags that are easy to remember for commands that are complex or bothersome to retype.
Unlike setting up an alias, tags are associated with your command history. For this reason, they only remain available if you keep using them. Once you stop using a tag, it will slowly disappear from your command history file. Of course, for most of us, that means we can type 500 or 1,000 commands before this happens. So, tags are a good way to rerun commands that are going to be useful for some period of time, but not for those that you want to have available permanently.
To set up a tag, type a command and then add your tag at the end of it. The tag must start with a # sign and should be followed immediately by a string of letters. This keeps the tag from being treated as part of the command itself. Instead, it’s handled as a comment but is still included in your command history file. Here’s a very simple and not particularly useful example:
$ echo "I like tags" #TAG
This particular echo command is now associated with #TAG in your command history. If you use the history command, you’ll see it:
$ history | grep TAG 998 08/11/20 08:28:29 echo "I like tags" #TAG <== 999 08/11/20 08:28:34 history | grep TAG
Afterwards, you can rerun the echo command shown by entering !? followed by the tag.
$ !? #TAG echo “I like tags” #TAG “I like tags”
The point is that you will likely only want to do this when the command you want to run repeatedly is so complex that it’s hard to remember or just annoying to type repeatedly. To list your most recently updated files, for example, you might use a tag #REC (for “recent”) and associate it with the appropriate ls command. The command below lists files in your home directory regardless of where you are currently positioned in the file system, lists them in reverse date order, and displays only the five most recently created or changed files.
$ ls -ltr ~ | tail -5 #REC <== Associate the tag with a command drwxrwxr-x 2 shs shs 4096 Oct 26 06:13 PNGs -rw-rw-r-- 1 shs shs 21 Oct 27 16:26 answers -rwx------ 1 shs shs 644 Oct 29 17:29 update_user -rw-rw-r-- 1 shs shs 242528 Nov 1 15:54 my.log -rw-rw-r-- 1 shs shs 266296 Nov 5 18:39 political_map.jpg $ !? #REC <== Run the command that the tag is associated with ls -ltr ~ | tail -5 #REC drwxrwxr-x 2 shs shs 4096 Oct 26 06:13 PNGs -rw-rw-r-- 1 shs shs 21 Oct 27 16:26 answers -rwx------ 1 shs shs 644 Oct 29 17:29 update_user -rw-rw-r-- 1 shs shs 242528 Nov 1 15:54 my.log -rw-rw-r-- 1 shs shs 266296 Nov 5 18:39 political_map.jpg
You can also rerun tagged commands using Ctrl-r (hold Ctrl key and press the “r” key) and then typing your tag (e.g., #REC). In fact, if you are only using one tag, just typing # after Ctrl-r should bring it up for you. The Ctrl-r sequence, like !?, searches through your command history for the string that you enter.
Tagging locations
Some people use tags to remember particular file system locations, making it easier to return to directories they”re working in without having to type complete directory paths.
$ cd /apps/data/stats/2020/11 #NOV
$ cat stats
$ cd !? #NOV <== takes you back to /apps/data/stats/2020/11
After using the #NOV tag as shown, whenever you need to move into the directory associated with #NOV, you have a quick way to do so – and one that doesn’t require that you think too much about where the data files are stored.
NOTE: Tags don’t need to be in all uppercase letters, though this makes them easier to recognize and unlikely to conflict with any commands or file names that are also in your command history.
Alternatives to tags
While tags can be very useful, there are other ways to do the same things that you can do with them.
To make commands easily repeatable, assign them to aliases.
$ alias recent=”ls -ltr ~ | tail -5”
To make multiple commands easily repeatable, turn them into a script.
#!/bin/bash echo “Most recently updated files:” ls -ltr ~ | tail -5
To make file system locations easier to navigate to, create symbolic links.
$ ln -s /apps/data/stats/2020/11 NOV
To rerun recently used commands, use the up arrow key to back up through your command history until you reach the command you want to reuse and then press the enter key.
You can also rerun recent commands by typing something like “history | tail -20” and then type “!” following by the number to the left of the command you want to rerun (e.g., !999).
Wrap-up
Tags are most useful when you need to run complex commands again and again in a limited timeframe. They’re easy to set up and they fade away when you stop using them.
Copyright © 2020 IDG Communications, Inc.