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 How to find files on Linux There are many options you can use to find files on Linux, including searching by file name (or partial name), age, owner, group, size, type and inode number. By Sandra Henry Stocker Jun 24, 2024 8 mins Linux opinion Linux in your car: Red Hat’s milestone collaboration with exida With contributions from Red Hat and critical collaborators, the safety and security of automotive vehicles has reached a new level of reliability. By Sandra Henry Stocker Jun 17, 2024 5 mins Linux how-to How to print from the Linux command line: double-sided, landscape and more There's a lot more to printing from the Linux command line than the lp command. Check out some of the many available options. By Sandra Henry Stocker Jun 11, 2024 6 mins Linux how-to Converting between uppercase and lowercase on the Linux command line Converting text between uppercase and lowercase can be very tedious, especially when you want to avoid inadvertent misspellings. Fortunately, Linux provides a handful of commands that can make the job very easy. By Sandra Henry Stocker Jun 07, 2024 5 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe