Ubuntu/Debian/Mint news and tutorials | Linux stuff
Linux Cheat Sheet
Have a look at our Linux Cheat Sheet for quick one-liners, commands and tips.
Quick Tip
Disable Overlay Scrollbars in GNOME
gsettings set com.canonical.desktop.interface scrollbar-mode normal
Quick Tip
Find Files Containing a Text Pattern
find . -iname "*.txt" -exec grep -l "hello" {} +
Online Readers
Advertise on TuxArena

Using !!
This command is used to bring back and automatically execute the last command in history. It is the same as pressing C^P followed by Enter). Here’s an example:

Using !text
Replacing ‘text’ with any command will call the last command in the history which starts with ‘text’. Example:

As you can see after issuing the first ls command we printed the working directory, then we called back the last ls command with !ls.

Using !n
This will bring up the nth command in history. This will vary depending on your history. Here’s an example:

Using !?text?
This will execute the most recent command that contains the word ‘text’. Example:

The most recent command containing the text ‘xjf’ was executed. This trick should be applied carefully though, especially for sensitive commands like rm.

Using !! in combination with another command
!! can also be used in combination with some other command, because the shell expands it first and then it executes the current command. For example, this can be very useful in combination with sudo, since sometimes we forget to use administrative privileges for commands that need it. For example:

Changing the color of the Bash prompt
There are many ways of customizing your Bash prompt, and I will list here only a few pre-defined sets.
To make the user’s prompt green, put this inside your ~/.bashrc file, where ~ is your home directory:

Now run source ~/.bashrc or . ~/.bashrc to read the settings again. Here’s how your prompt should look like:

Here’s another example, which will make your prompt look really fancy:

And this is how it will look like:

A pretty good tutorial on this can be found here (Bash Prompts How-To) and several prompt schemes on the Arch Wiki, here. The Bash Reference manual section on this includes some useful information too.

Catch the exit status of a command with $?
If a command is successful, its exit status will be 0, otherwise it will be different from 0. This can be useful in scripts.

Using reversed search: Ctrl-R
Ctrl-R will prompt you to enter a pattern for a command, and will search the history in reversed order for any the first command that contains the pattern and execute it. Example

In the above example we issued the ls -lh command, the pressed Ctrl-R and typed in the letter L. The command was brought up and then executed with Enter.

Using cd – to go to the previous working directory
This command will have the same effect as cd $OLDPWD, where $OLDPWD is a variable that holds the previous working directory.

Using grep -e -pattern to show the lines that start with a – sign
This will be useful if piped to commands like man, for example:

This will query the manual page for gcc and will only print lines that contain the text -ansi.

Share this!Share on RedditShare on StumbleUponShare on FacebookTweet about this on TwitterShare on TumblrShare on Google+
L Duperval says:

What I would really like is the equivalent of Esc-P in tcsh.

> ls -rt
> ls -a
> ls -la
> ls -C
> ls -ra
> ls -g
> ls -r

displays ls -ra

Hit Esc-P again and it shows ls -rt. Not quite the same as Ctrl-R and more user-friendly, I find.


daemox says:

Neat, I think I’ll be making heavy usage of bang bang, and sudo !! from now on.


Lawrence D’Oliveiro says:

The usual way to deal with arguments beginning with “-” is to precede them with “–”, e.g.

man gcc | grep — -ansi

Another way, that only works with file names, is to prepend “./”, e.g.

ldo@theon:~> touch — -rf
ldo@theon:~> ls -l — -rf
-rw-r–r– 1 ldo users 0 Dec 21 19:17 -rf
ldo@theon:~> rm -v ./-rf
removed `./-rf’

Craciun Dan says:

The — and ./ are great tips Lawrence D’Oliveiro, thank you for sharing these.

Replika says:

Thanks, really like “sudo !!”

alex dekker says:

Every machine I install, I edit /etc/inputrc and add the following lines:

“\e[5~”: history-search-backward
“\e[6~”: history-search-forward

This modifies PgUp and PgDn, so I can type:

> ls

hit PgUp, and it searches for the last command starting ‘ls':

> ls /path/to/some/place/i/dont/want/to/type/again

In Debian derivatives this is usually commented out. In RedHat, it’s usually mapped to first and list history entry. I think you’ll agree that wanting to repeat a command without hitting up 10 times is a lot more useful than jumping to the first entry in your bash history :-)

Craciun Dan says:

Thanks you for suggesting those, I think they’ll be in part 2 I’m working at.

sans_core says:

I know that I am not the only person to have been burned by the bang commands. Think ‘!rm’ which could, accidently, recall ‘rm -rf *’. But, it could be as simple as calling the wrong script or opening a random document in vim.

So, what I do to prevent annoying or destructive scenarios is run ‘echo !vim’ or ‘echo !rm’ which will, of course, output the last matching command.

Leave a Comment