Top Ten One-Liners from CommandLineFu Explained
I love working in the shell. Mastery of shell lets you get things done in seconds, rather than minutes or hours, if you chose to write a program instead.
In this article I’d like to explain the top one-liners from the commandlinefu.com. It’s a user-driven website where people get to choose the best and most useful shell one-liners.
But before I do that, I want to take the opportunity and link to a few of my articles that I wrote some time ago on working efficiently in the command line:
- Working efficiently in bash with vi keyboard shortcuts.
- Working efficiently in bash with emacs keyboard shortcuts.
- The definitive guide to bash command line history.
- Fun article on set operations in the shell.
And now the explanation of top one-liners from commandlinefu.
#1. Run the last command as root
$ sudo !!
We all know what the sudo
command does — it runs the command as another user, in this case, it runs the command as superuser because no other user was specified. But what’s really interesting is the bang-bang !!
part of the command. It’s called the event designator. An event designator references a command in shell’s history. In this case the event designator references the previous command. Writing !!
is the same as writing !-1
. The -1
refers to the last command. You can generalize it, and write !-n
to refer to the n-th previous command. To view all your previous commands, type history
.
I wrote about event designators in much more detail in my article “The Definitive Guide to Bash Command Line History.” The article also comes with a printable cheat sheet for working with the history.