Intro

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.

cmd
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.

cmd
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.

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

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

cmd
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.

cmd
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 .

cmd
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.

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

ping

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

cmd
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>

bash
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>

bash
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

Functions

Create and change to a directory path.

bash
function mkcd() {
  dir="$*";
  mkdir -p "$dir" && cd "$dir";
}

Credit

Shell Expansion

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

cmd
(reverse-i-search):

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

cmd
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.

cmd
# 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.

cmd
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

! ! repeats the last command.

cmd
!! # last command was 'pwd'


# output

pwd
/home/bradmin/

! <command> repeats the last occurrence of <command> .

cmd
!rails

# output

rails console
Running via Spring preloader in process 11499
Loading development environment (Rails 5.2.2.1)

tee

The tee command can be used to write content to a file. This is very useful when combined with a HERE doc for files that require sudo.

file
sudo tee /etc/yum.repos.d/nginx.repo > /dev/null << "EOF"
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
EOF

Keyboard Shortcuts

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.

ALT + d deletes characters in front of the cursor up to the space character.

CTRL + f Move forward one character.

CTRL + b Move back one character.

ALT + f Move forward one word.

ALT + b Move back one word.

# linux
# shell