updated: 3rd of September 2019
published: 6th of July 2018
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.
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
The echo command can be used to redirect content to a file.
echo "some content" > /some/filename
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
Use the --append flag to append the content to the end of the target file.
echo "some other content" | sudo tee --append /some/file
Search a file and ignore lines that start with a # , space or tab .
egrep -v '(^#|^\s*$|^\s*\t*#)' /some/filename
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 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
}
Create and change to a directory path.
function mkcd() {
dir="$*";
mkdir -p "$dir" && cd "$dir";
}
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
! ! repeats the last command.
!! # last command was 'pwd'
# output
pwd
/home/bradmin/
! <command> repeats the last occurrence of <command> .
!rails
# output
rails console
Running via Spring preloader in process 11499
Loading development environment (Rails 5.2.2.1)
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.
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
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.