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!
The majority of these tricks utilize the ‘history’ command in some way, so let’s cover that one first. Just type:
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.
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.
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:
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.
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.
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.
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.
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.
Here, I substituted the second argument from my last ‘ln’ command.
As you can see, I used a Ctrl+R lookup to help illustrate where that argument came from.
Cleaning Up Your Tracks
If you want to clear your history, use this:
And if you want to disable history altogether, use this:
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.
In the fourth line down, I lead the ‘cd’ command with a space.
Also, notice that I listed only the last 4 history entries by using a pipe (|) and the ‘tail’ command. You can change 4 to anything else to get the same effect yourself.
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:
If you want to use both ‘ignorespace’ and ‘ignoredups,’ you’ll need to use ‘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. ;-)
Which of these tricks do you like best? Have a favorite we didn’t list here? Share your thoughts in the comments!
Yatri Trivedi is a monk-like geek. When he's not overdosing on meditation and geek news of all kinds, he's hacking and tweaking something, often while mumbling in 4 or 5 other languages.
- Published 03/4/11