The command line. That small place, where a lot can happen. And more so if you
are a Linux user… How to maximize it? Where to harness its power?
I discovered commandlinefu.com a
few years ago, while looking for a way to do… something. I don’t even enter
that often, although it is a brilliant place to discover how to do X in
Among its all time greats I found some gems, and some others I discovered
elsewhere, or even I made up.
Command line gems
Run the last command as root:
sudo !! as in
apt-get install something -> error, you have to be root
The double exclamation mark recovers the last command in the history.
Use the last argument for the last command:
!$ as in
Change to the previous directory:
cd - as in (after the previous example to
get back to where you started). Good paired with
popd (bash keeps a stack of
Backward history incremental search:
C-r in the terminal prompt, start writing
the command you want to backward-search. Press
C-r to keep on searching for that
term. Best combined with
Correct typo in previous command:
^typo^corrected as in
sudo apt-get isntall somepackage
Write command in editor:
C-e will fire the editor in
$EDITOR to write the
Open the last modified file of a type: I use this to open the most recent
file in a directory with Eye Of Gnome:
eog "$(ls -rt \*.ppm | tail -n 1)"
Use locate to search for a specific PDF and open it with evince:
"$(locate \*partofname\*.pdf)" This will only work if it results in only one
instance. If you want to open only the first occurrence
\*partofname\*.pdf | head -n 1)"
The same with find:
evince "$(find -name 'NameOfPdf.pdf')"
Mighty heads and tails:
head -n N file,
tail -n N file will return the first
N lines of file.
pid of a process by name:
ps ax | grep "firefox"
Follow changes on a incremental file:
tail -f filename as in following
from a running process.
Get all lines containing a string in a file:
grep -e "string" file > outputfile
Useful as intermediate step for plotting specific data lines from a
-r, change the string for a
regexp (consider buying this
book if you will use them more than once, I recommend it: Mastering Regular
I think I have a few more, but I just don’t remember them.
Note from 2019: This was written a long time ago, and
my list of tips and speed ups for bash/zsh is way larger now. Hit me on twitter
if you want to know more.