1. Intro

Bash is IMHO pretty much what makes linux usable on the nitty-gritty level.

Here I'll collect some of the bash tips and tricks I use.

 

Search history for a particular command

# history | grep ssh-c
ssh-copy-id -i ~/.ssh/id_rsa.pub This email address is being protected from spambots. You need JavaScript enabled to view it.m -p 1234

 

2. Prettify and make the root prompt stand out

Add the following line to your /root/.bashrc, then save the file and restart the terminal to activate the change.

export PS1="\[$(tput bold)$(tput setab 0)$(tput setaf 1)\]\u@\h:\w # \[$(tput sgr0)\]"

The above will make your prompt bold red, so as to make it different from a plain whitish luser-user account.

 

3. Take control of the history file

Typing history in the terminal should return a long list of commands you've issued. It can grow pretty long too... Take control with these settings.

Add either, or all, to your /home/$USER/.bashrc file. Save the file after and restart your terminal.

 

3.1. Ignore collecting passwords, ID, uptime, resize, ls, clear and history.

export HISTIGNORE=":pwd:id:uptime:resize:ls:clear:history:"

 

3.2. History will ignore logging any commands beginning with a space as well as any duplicates.

HISTCONTROL=ignorespace:erasedups

or use

HISTCONTROL=ignoreboth

 

3.3. Tag history output with date and time.

HISTTIMEFORMAT="%F %T  "

It will produce something like this.

# history

764 2018-11-08 09:50:44 ansible-playbook enable.repo.yml --check
...

 

3.4. Increase amount of history commands

The default is around 500 commands. Sometimes this is not enough.

Add the following to your /root/.bashrc to increase to 1000 remembered commands. Then save, exit and restart the terminal.

HISTSIZE = 1000

 

3.5. Show the last ten commands in history

# history | tail 10

 

4. Read a log file in real time

There may be times when you're troubleshooting and need to see the logs in real-time. Use the below command to achieve this

# tail -f /var/log/httpd/error_access_log

 

4.1. Look for a particular keyword

# tail -f /var/log/httpd/error_access_log | grep 504

 

 

5. Prettify ls for easier reading

Add the below to to your /root/.bashrc file, save, exit and restart the terminal.

ls='ls .color=auto' 

 

 

 

 

 

Sources

Various internet sites