Dr. Torq: Timely Tips for the Linux History Command
Linux history is one of those very useful built-in functions that may not be entirely obvious to new Linux users. History saves mucho time and frustration when entering and executing commands in a Linux terminal window.
I do a lot of hardware gadget testing and frequently have to run a collection of command lines over and over again, often changing options as I go. Re-typing each line is extremely tedious and error-prone. Linux command-line recall capabilities sidestep all that and lets you easily work with programs you ran earlier.
Today we’ll explore using history and related topics to help you streamline your Linux command-line activities and look like a pro.
Embrace the up Arrow
Open a Linux terminal window and type the “ls” command followed by the “Enter” key. You’ll get a list of the files in your current directory. Now type “dmesg,” again with an “Enter.” You’ll get a detailed printout of all the system-level activities on your machine, including boot ups, initializations, errors and hardware changes. Now try “df -h” and “Enter.” A neat display of disk information pops up on your screen.
OK, now tap the up-arrow key three times and watch what happens at the cursor. There’s that “ls” command again. Press “Enter” to display the file listing again. Cool. Now type “cd /” and “Enter” to move up to the root directory. This time press the arrow key four times. Poof, “ls” pops up at the cursor once more. Push “Enter” to get another file listing. Now you’ll see your top-level system level directories and files. You are using the same ‘ls‘ command, in an altogether different directory.
This is the Linux history function in action. Every time we type a command, Linux logs it in an ongoing list, that we can recall when needed. We move through that list using the up and down arrow keys.
You can set the number of commands in the list, but I’ve never really bothered changing the default. My Linux notebook and various Raspberry Pi machines hold two or three hundred commands, out of the box. The commands are available even after a logout or reboot. Just open a terminal and use the up or down arrow, to find previously executed tasks.
grep Through History
I’ll admit it. Sometimes I lean on the up/down-arrow keys a bit too much. Say I typed this long, complex command-line an hour ago and now need to run it again. No problem. Simply up-arrow to the command and hit enter.
This works great if the command is only ten or fifteen steps back. If I’m really pounding on the keyboard that choice command might be a hundred and fifty steps back. I’m a pretty fast reader, but rarely can pick out what I need as it scrolls by while holding down the up-arrow key. Surely, there’s a way around this situation.
That’s a job for the “history” command. Just run “history” followed by the “Enter” key to get a nice listing of the commands you typed previously.
Notice the listing has a number, followed by a few spaces and then the command.
You can scroll up and down to find that long, complex command you want to re-execute. Run the command by returning to the command-line (just hit the “Enter” key)and typing the bang (!) symbol followed by the command-line’s number. Your previous command will magically run again.
Here’s an example. The above graphic shows a portion of my current history list. To run the luvcview program with a video resolution of 640×498 I’d just pick that line from the list. My chosen line shows up as “luvcview -d /dev/video1 -s 640×498” with a corresponding command number of 1908. Type it with the bang.
It gets even better, by piping the history command through grep. A Linux pipe is signified with the vertical bar symbol (|) and connects the output of one program to the input of another.
grep is the Linux file search utility. It looks for text strings in files. We can find a command in our history by doing something like the following.
rob% history | grep luvcview
Here’s the printout, showing all the luvcview command-lines in our history list and their corresponding command numbers.
Pick out the number, add the bang symbol to the front and we’re off to the races. Using grep with history is a tremendous time saver when doing repetitive development and testing jobs.
Another way to use history is with the mouse copy/paste shortcuts, so you can edit the line.
For example take that same 1908 listing and highlight it. Left-click the mouse at the beginning of the luvcview command and drag the highlighting to the end of the line. Release the left mouse button and click the right mouse button. Select “Copy” from the drop-down menu. Click at the cursor and again hit the left mouse button. Select “Paste”. Touch “Enter” and the command will run.
What’s interesting is that right after pasting, you can use the right and left arrow keys to move around on the line. For example you might want to arrow over and use the delete key to get rid of the “640×498” resolution and replace it with “640×240”. Finish with “Enter” and the luvcview will run with the new resolution.
How easy is that?
The cut and past capability is particularly helpful when I have lengthy, complicated command-lines, that only need a tweak, like switching from a video input of “/dev/video0” to “/dev/video1.” I’ll just replace the “0” with the “1”. Re-typing everything can be a drag.
Once you get used to using Linux command recall with the up/down arrows, history with grep and the cutting and pasting mouse shortcuts, you’ll wonder how you ever did it any other way. If you touch-type quickly using those techniques, others will look at you with amazement as you effortlessly hop around the command-line and expertly get things done in record time.
Feature Image by Pavlofox from Pixabay.