2016-07-07 18:34:59
Whether you re new to the Linux command-line or you re a seasoned veteran, these tricks will help turn your text-based meanderings into full-blown marathons. Save time, speed up your productivity, and enhance your Linux-Fu, all at once!
History Commands
The majority of these tricks utilize the history command in some way, so let s cover that one first. Just type:
history
It s that simple! You ll output similar to the following:
To execute a specific command from your history, you can just type an exclamation point followed by the number of the command as listed by history. Here, I ll re-execute command number 510.
!510
By the way, an exclamation point is referred to colloquially as a bang.
You can also refer to a command by how long ago it was run. Next, let s execute whatever we typed three commands ago.
!-3
Quick Substitutions
Let s say you want to rerun your previous command. Just type two exclamation points. This is perfect for when you run a command that needs super-user privileges and you forgot to do that. Just give it the old sudo bang bang treatment:
sudo !!
Let s say you want to run a command with the last argument you used. Typing it out is too tedious. Just use a bang dollar to substitute your last argument automatically.
cd !$
What if you ran a command with two arguments and you want to run the first one? Bang caret to the rescue! This works well when you make a backup of a config file and then want to edit it.
nano !^
Next, let s search for a specific command from your history. Ctrl+R will search backwards for whatever you type. It will autocomplete as you type, and you can scroll through with the arrow keys to find the specific instance you re looking for.
ctrl r
If you know the last command you ran with a specific keyword, you can skip the search process and pare down your key presses by at least one.
!keyword
Now, for the power substitution: you can substitute an argument for your last specific command using something similar to the previous method. Just add a colon and the number of the argument.
ls !ln:2
Cleaning Up Your Tracks
If you want to clear your history, use this:
history c
And if you want to disable history altogether, use this:
HISTSIZE=0
To re-enable it, you can just change that value from 0 to something else (the defaults are usually 500 or 1000).
But why should you choose between all or nothing? Use the following command to make sure that any command that leads with a space does not get recorded in your history.
HISTCONTROL=ignorespace
No Duplicates Here
I love using Bash s history. The one thing I don t like is duplicates. Use the following command to ignore duplicate entries:
HISTCONTROL=ignoredups
If you want to use both ignorespace and ignoredups, you ll need to use ignoreboth .
HISTCONTROL=ignoreboth
Pretend to Be Busy
As a bonus for you How-To Geek readers, I ll include the following, completely irrelevant command:
cat /dev/urandom | hexdump -C | grep ca fe
This will continually generate random characters and values in a specific format, which is nice to stare at for therapeutic reasons.
It also can come in handy in case you want to look busy or take a quick coffee break at work. Don t be too responsible with this one, guys. ;-)