Tags present a simple approach to affiliate strings that appear like hash tags (e.g., #HOME) with instructions that you simply run on the command line. As soon as a tag is established, you’ll be able to rerun the related command with out having to retype it. As a substitute, you merely kind the tag. The concept is to make use of tags which can be simple to recollect for instructions which can be advanced or bothersome to retype.
In contrast to organising an alias, tags are related together with your command historical past. Because of this, they solely stay accessible when you preserve utilizing them. When you cease utilizing a tag, it can slowly disappear out of your command historical past file. After all, for many of us, meaning we will kind 500 or 1,000 instructions earlier than this occurs. So, tags are a great way to rerun instructions which can be going to be helpful for some time frame, however not for those who you need to have accessible completely.
To arrange a tag, kind a command after which add your tag on the finish of it. The tag should begin with a # signal and ought to be adopted instantly by a string of letters. This retains the tag from being handled as a part of the command itself. As a substitute, it is dealt with as a remark however remains to be included in your command historical past file. Here is a quite simple and never significantly helpful instance:
$ echo "I like tags" #TAG
This explicit echo command is now related to #TAG in your command historical past. In case you use the historical past command, you may see it:
$ historical past | grep TAG 998 08/11/20 08:28:29 echo "I like tags" #TAG <== 999 08/11/20 08:28:34 historical past | grep TAG
Afterwards, you’ll be able to rerun the echo command proven by coming into !? adopted by the tag.
$ !? #TAG echo “I like tags” #TAG “I like tags”
The purpose is that you’ll doubtless solely need to do that when the command you need to run repeatedly is so advanced that it is onerous to recollect or simply annoying to kind repeatedly. To checklist your most lately up to date information, for instance, you would possibly use a tag #REC (for “latest”) and affiliate it with the suitable ls command. The command beneath lists information in your house listing no matter the place you’re at the moment positioned within the file system, lists them in reverse date order, and shows solely the 5 most lately created or modified information.
$ ls -ltr ~ | tail -5 #REC <== Affiliate 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 solutions -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 related to 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 solutions -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 too can rerun tagged instructions utilizing Ctrl-r (maintain Ctrl key and press the “r” key) after which typing your tag (e.g., #REC). The truth is, in case you are solely utilizing one tag, simply typing # after Ctrl-r ought to carry it up for you. The Ctrl-r sequence, like !?, searches via your command historical past for the string that you simply enter.
Some folks use tags to recollect explicit file system areas, making it simpler to return to directories they”re working in with out having to kind full listing paths.
$ cd /apps/knowledge/stats/2020/11 #NOV
$ cat stats
$ cd !? #NOV <== takes you again to /apps/knowledge/stats/2020/11
After utilizing the #NOV tag as proven, every time it’s essential transfer into the listing related to #NOV, you’ve got a fast means to take action – and one that does not require that you simply suppose an excessive amount of about the place the information information are saved.
NOTE: Tags do not should be in all uppercase letters, although this makes them simpler to acknowledge and unlikely to battle with any instructions or file names which can be additionally in your command historical past.
Options to tags
Whereas tags will be very helpful, there are different methods to do the identical issues that you are able to do with them.
To make instructions simply repeatable, assign them to aliases.
$ alias latest=”ls -ltr ~ | tail -5”
To make a number of instructions simply repeatable, flip them right into a script.
#!/bin/bash echo “Most lately up to date information:” ls -ltr ~ | tail -5
To make file system areas simpler to navigate to, create symbolic hyperlinks.
$ ln -s /apps/knowledge/stats/2020/11 NOV
To rerun lately used instructions, use the up arrow key to again up via your command historical past till you attain the command you need to reuse after which press the enter key.
You too can rerun latest instructions by typing one thing like “historical past | tail -20” after which kind “!” following by the quantity to the left of the command you need to rerun (e.g., !999).
Tags are most helpful when it’s essential run advanced instructions many times in a restricted timeframe. They’re simple to arrange they usually fade away if you cease utilizing them.
Copyright © 2020 IDG Communications, Inc.
Leave a Reply