Overview

A collection of useful tips and tricks for the linux shell that I have stumbled across over the years. I'll keep updating this post as I come across something of value. I use bash, so these apply to bash unless noted otherwise but my work in other shells.

cat

The cat command can be used with a HERE doc to redirect multi-line content to a file.


cat << EOF > /some/filename
Some content
  indented content
more content
EOF
          
credit

echo

The echo command can be used to redirect content to a file.


echo "some content" > /some/filename
          

Warning

Using the single > operator will overwrite the target file.

Use the double >> operator to append content to an existing file.


echo "some more content" >> /some/filename
          

If the file requires root permissions pipe | the content to the tee command.


echo "some content" | sudo tee /some/file
          

Warning

This will overwrite the target file.

Use the --append flag to append the content to the end of the target file.


echo "some other content" | sudo tee --append /some/file
          

Credit

grep | egrep

Search a file and ignore lines that start with a #, space or tab.


egrep -v '(^#|^\s*$|^\s*\t*#)' /some/filename
          

Credit

Get some context around a match with the grep/egrep command using the -C <num_of_context_lines> flag.


egrep string-to-match -C 5 /some/filename
          

ping

Ping that shows a timestamp for each echo and also lost pings.


ping -i 1 -W1 -D -O 9.9.9.9 | while read row ; do awk '{ sub(/[0-9]{10}/, strftime("%Y-%m-%d %H:%M:%S",  substr($0,2,10))) }1' <<< "$row"; done

ping6 -i 1 -W1 -D -O 2620:fe::9 | while read row ; do awk '{ sub(/[0-9]{10}/, strftime("%Y-%m-%d %H:%M:%S",  substr($0,2,10))) }1' <<< "$row"; done
          

I adapted the above into a bash function that can be placed in your ~/.bashrc file. Call the function with tping <ip-address>


function tping () {
  ping -i 1 -W1 -D -O "$1" |
    while read row ; do
      awk '{ sub(/[0-9]{10}/, strftime("%Y-%m-%d %H:%M:%S",  substr($0,2,10))) }1' <<< "$row";
    done
}
          

You can also do something similar for IPv6. Call the function with tping6 <ipv6-address>


function tping6 () {
  ping6 -i 1 -W1 -D -O "$1" |
    while read row ; do
      awk '{ sub(/[0-9]{10}/, strftime("%Y-%m-%d %H:%M:%S",  substr($0,2,10))) }1' <<< "$row";
    done
}
          

Credit

Shortcuts

Search your history file with the CTRL + r keyboard combination.


(reverse-i-search)`':
          

Expand the last command of the previously run set of commands with the ALT + . keyboard combination.


tail -f /var/log/some/nested/log/file
tail: cannot open ‘/var/log/some/nested/log/file’ for reading: Permission denied

sudo tail -f <alt . expands> /var/log/some/nested/log/file
          

A space at the start of a set of commands stops the line being written to your ~/.bash_history. Good for keeping things like passwords and API keys out of your ~/.bash_history or when running dangerous sudo commands.


# space at front omits command from history
 sudo rm -rf /some/directory
          

!<number_of_command> to re-execute a command from the output of the history command.


$ history

# output
2001  cat Pipfile
2002  gunicorn -w 4 -b 0.0.0.0:5000 wsgi:app
2003  cat wsgi.py

!2002 # re-execute command 2002
          

Moving the cursor around

CTRL + e moves the cursor to the end of the line.

CTRL + a moves the cursor to the beginning of the line.

CTRL + u deletes from the cursor to the beginning of the line.

CTRL + k deletes from the cursor to the end of the line.

CTRL + w deletes characters behind the cursor up to the space character.























Updated: 2018-09-07 - Add cat tips

Updated: 2018-09-02 - Add echo tips

Published: 2018-07-06