Tag Archives: Linux How to

Unix Similar multi-user operating systems based on the Linux kernel and essentially on GNU software. Like CentOS, Debian, Ubuntu Fedora.

How to Search in Bash History

Using Bash History Reverse Search to find and re-execute command

Bourne-again shell is a free Unix shell under the GPL. Bash remembers the commands you enter and saves them to a history file. It is the default shell for terminal environments of most Linux distributions, on macOS the default shell can be set to Bash, the Bourne-again shell is also available for Windows 10 and can be installed with the WSL features.

BASH the Bourne-again terminal shell on Debian
Bash the Bourne-again shell on Debian

Most will know it, with the arrow up key you can scroll through the bash history to run a previously executed command again.

Reverse Search Bash History for a Command

What many do not know, it can be searched for a certain command in the history, so with Ctrl+R the reverse search can be called, here you enter the characters you remember.

Bash Reverse Search Ctrl+R

By repeatedly pressing Ctrl+R you scroll back step by step, to command lines that match to the search pattern in the history.

  • Ctrl+R: Search Bash History for a Command. Gets the last command that matches the characters you entered.
  • Ctrl+O: Run the found command, after Ctrl+R.
  • Ctrl+G: Exits history search mode without executing a command.

Bash History Common Scenario

A common scenario in which you find yourself is to enter part of a command and then find that you have run it before and could search the history for it.

The correct search method with what is already on the command line is to move the cursor to the beginning of the line with Ctrl+A to call the reverse history with Ctrl+R and to insert the current row into the search with Ctrl+Y and then search backwards again with Ctrl+R

Reviewing Previous Bash History

Each command is associated with a number for easy reference. The list of commands in the Bash history begins with an index number.

$ history

The command with the highest number is the last command executed.

495 vi /etc/bash.bashrc
496 cd /var/www/3w/dot/html/content
497 uname -promsvm
498 vi /etc/sysctl.conf
499 multitail -cS postfix /var/log/mail.log
500 multitail -cS apache /var/log/apache2/access.log
501 multitail -cS syslog /var/log/syslog
502 history

We can truncate the output by specifying a number after the command. For example, if we only want to see the last 5 commands entered, we can type the following.

$ history 5

Other possibilities are, for example, with forward to grep to filter the command history output.

$ history | grep hostname
365 hostname
369 hostnamectl status
484 hostnamectl set-hostname debian
486 cat /etc/hostname
516 history | grep hostname

Run Commands from Bash History

We can recall any of our previous command from history by its number preceded by an exclamation point (!). For instance, if the history looks like the above, we could run hostnamectl status quickly by typing !369.

$ !369
   Static hostname: debian
         Icon name: computer-vm
           Chassis: vm
        Machine ID: fc5cb9d7ace83e1c9c1fcbe4ec76d302
           Boot ID: 379bf41010125c63bbaa4f2b778ac54c
    Virtualization: vmware
  Operating System: Debian GNU/Linux 10 (buster)
            Kernel: Linux 4.19.0-17-amd64
      Architecture: x86-64

This will immediately recall and execute the command associated with the history number 369.

We can also execute commands relative to our current position. We can do this by using the !-n syntax, where n is replaced by the number of commands ago we want to recall.

For instance, if we want to recall and execute a command that we typed before our most recent one, we could type !-5. So we will run again, our session might look like this.

$ !-5

To re-execute the previous command, bash provides a shortcut that we can use. The shortcut is !! will re-execute the last recent command.

Delete Bash History

The command cleans up the history of the current user session.

$ history -c

Completely clear the saved bash history from the current user.

$ cat /dev/null > ~/.bash_history

The cat command clear the the content in file .bash_history of the currently logged on user.


This should now give a good idea of how the available follow-up operations can be used. Some of these will probably be more useful than others, but it’s good to know that bash has these skills in case you’re in a position where it would be helpful to dig them up.

Last but not least, the history command, the reverse search, and the simple history extensions should help speed up the workflow.

How to Forward Mails to Gmail using Postfix

Send email using Postfix (MTA) Mail Transfer Agent via Gmail Relayhost

Emails sending with Postfix (MTA) Mail Transfer Agent via Gmail relay host. E-mails are delivered via Gmail account, the clients in the local network use Postfix as a local SMTP gateway. This tutorial shows how to do this.

In situations like these, for example, multifunctional devices, or apps that do not support authentication via Mail Submission and STARTTLS (port 587), e-mails can send via local Postfix MTA without need to log on to a mail server.

If CentOS Linux is used, the required packages are installed as root.

yum update && yum install -y postfix mailx cyrus-sasl cyrus-sasl-plain

On a Debian and Ubuntu, the package installation is as follows.

apt-get update && apt-get install -y postfix mailutils

The Postfix configuration is to be edited by opening the file /etc/postfix/main.cf

mynetworks =
myhostname =
mydestination = $myhostname, localhost.$mydomain, localhost
inet_interfaces = all
inet_protocols = ipv4
relayhost = [smtp.gmail.com]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_security_options =
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_tls_CAfile = /etc/pki/tls/certs/ca-bundle.crt

Enter the local networks in mynetworks. As myhostname the PTR resource record that can be resolved on the Internet for determining the PTR entry, the following command can be executed in the terminal, as in the following example, or here.

$ curl -s ifconfig.co | xargs host domain name pointer

Next, a /etc/postfix/sasl_passwd file is created with the following content.

[smtp.gmail.com]:587 mein@gmail.com:password

Instead of mein@gmail.com use your own Gmail account, with password the Google password.

Use postmap to create the file sasl_passwd as Berkeley DB.

postmap /etc/postfix/sasl_passwd

Hint. Run postmap after each change.

Now Postfix is restarted to activate the configuration.

systemctl restart postfix

Test the Postfix configuration with this line in the terminal.

echo "Here is a text." | mail -v -s "Test subject" -r mein@gmail.com andere@domain.com

The mein@gmail.com be the valid Gmail address, and if andere@domain.com, enter a valid recipient email.

Note. This Google Account requires disabled settings under Security – Sign in to Google – go to Security Verification and set two factor Off, and access to the Google Account when accessed by less secure apps must be On.

Postfix logging with syslog in the log file /var/log/maillog in which you can check whether the e-mail was sent and accepted by smtp.gmail.com.

tail -25 /var/log/maillog

Use mailq to check the queue for any rejected mails.

If you find authentication errors in maillog, the Google Account security settings must be checked.

status=deferred (SASL authentication failed; server smtp.gmail.com[] said: 534-5.7.9 Application-specific password required. Learn more at?534 5.7.9

Hint. It can happen that after several sending attempts, then rejected (bounced) mails are in the queue, the command mailq gets them, with postsuper -d you remove mails from the queue.

postsuper -d ALL