Adding a tag to a Linux command can make it a little easier to reuse it. If you're struggling to remember complex commands or important locations in the file system, tags might help you out. Credit: Getty Images 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 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 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 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. Related content how-to Compressing files using the zip command on Linux The zip command lets you compress files to preserve them or back them up, and you can require a password to extract the contents of a zip file. By Sandra Henry-Stocker May 13, 2024 4 mins Linux opinion NSA, FBI warn of email spoofing threat Email spoofing is acknowledged by experts as a very credible threat. By Sandra Henry-Stocker May 13, 2024 3 mins Linux how-to The logic of && and || on Linux These AND and OR equivalents can be used in scripts to determine next actions. By Sandra Henry-Stocker May 02, 2024 4 mins Linux how-to Using the apropos command on Linux By Sandra Henry-Stocker Apr 24, 2024 3 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe