LCOD – 10.15.03 – BASH working with your command history


Command history searching in bash:
If you’re at the command line, and in Linux you’ll find yourself there often, and you want to find a command you had previously typed (so you don’t have to type it again, say), hit CTRL-R and start tying some letters from that command. Hit CTRL-R to get the next match. It will show them to you on the command line, and in order from most recent to least recent.
So, say you had recently scp’d a file and you want to do the same thing again, hit CTRL-R then type scp, if you’d recently done multiple scp’s or there are other commands with the letters scp in them, hit CTRL-R again and again till you find the one you want.

Last command substitution in bash:
You’re at the command line and you just typed a very long command, but when you hit enter it gave you an error, pressing the up arrow shows you the command, and you see a typo. You could use the left/right arrows to get to the typo and fix it, OR, you could use a simple character substitution. To redo your previous command with a simple substitution, use the carat key ^, that’s shift-6. The first carat will denote what you’re deleting, and the second carat will denote what you’re replacing it with.
So, you typed
scp fillename.tar.gz root@10.1.1.1This e-mail address is being protected from spam bots, you need JavaScript enabled to view it :/usr/portage/distfiles/
^ll^l
will redo the command with 1 less ‘l’
You can also leave off the second carat if you’re just deleting letters
so
^l
would do the same thing.
A couple caveats for carat use, first is that it’s a simple match, no regular expressions going on, and second is that it will match exactly and only once. So if there was a single l before the first one in fillename.tar.gz it would be deleted by the second command.

If you’ve got CTRL-R down and the ^^ substitutions, you’re well on your way to speedy command input. But what if it’s a new command, and you’re too lazy to type the full path, or you don’t know where something is and don’t want to have to retype your whole command again. Use the TAB key when typing a command. One tab will try to finish the current path or command, two tabs will list any possible commands or paths. From your bash prompt, type sc and hit TAB. You’ll either get scp or a beep indicating there are more than one possible commands. Hit TAB again and you’ll get a list of the possible commands.

Hope these help!

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

Easy AdSense by Unreal