Check if internet connection is available in bash script

This bash function uses netcat to test for internet connection (by pinging so it should work on most Linux computers. We use it on our Raspberry Pies because some times if the internet is disconnected, it won’t establish a new connection once the network is back up. To bypass the problem we trigger a reboot which re-initialises network connection.


# Description:  Reboot machine if internet is down

function check_internet_connection() {
    echo -e "GET HTTP/1.0\n\n" | nc 80 > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        echo "Online"
        echo "Offline"
        sudo reboot


Getting reboot history with correct time on Raspberry Pi

If you run the last reboot command to list reboot history, you might’ve noticed that the dates are wrong. That’s because the Raspberry Pi doesn’t have a RTC (Real Time Clock). But, the last command actually saves two dates (login and logout). The login date will be wrong because at this point of the boot-sequence the Raspberry hasn’t synchronized it’s clock with NTP (Network Time Protocol) yet, but the logout date will actually be stored correctly.

So, instead of doing last reboot, you have to run last reboot -F to view both dates in full format.

Example output:

last reboot
last reboot -F

CRON script to auto-renew SSL certificate and restart apache webserver using Certbot

Setting up a SSL certificate on a brand new server is so easy thanks to Certbot. But remember that every LetsEncrypt certificate expires after 3 months, so you better remember to renew it. Also, just renewing the certificate isn’t enough. Your apache webserver doesn’t know that it has been renewed, so you have to restart the webserver as well. Thankfully, certbot has a hook if the certificate has been renewed where we can add extra commands, such as restarting apache.

I like to put this in my crontab so I never have to think about it:

* 04 * * * sudo certbot renew --renew-hook "service apache2 restart" >> /home/user/logs/certbot.txt

Replace service apache2 restart with systemctl restart apache2 if you prefer to use systemctl syntax.

This will check the certificate every night at 0400 and renew it if necessary. If the certificate has been renewed, it will also restart apache.

Simple trick for remembering tar parameters

You have probably looked up the parameters for tar more than you’ve changed socks. And what if the scenario from xkcd ever became real? Could you really run a valid tar command without googling first?

XKCD: “I don’t know what’s worse–the fact that after 15 years of using tar I still can’t keep the flags straight, or that after 15 years of technological advancement I’m still mucking with tar flags that were 15 years old when I started.”

Here’s a few tricks to help you remember how to use tar if you want to extract files from an archive, either .tar , .gz or tar.bz2


That’s it! All you need is tar -xf <filename>

Many guides will use -xvfz (or -xfzv or -xfvz or whatever), but -v and -z are not actually necessary.

-z is only used to tell tar it’s a .gz file, but for the last 15 years or so tar have been able to automatically detect this itself.

The other parameter, -v, is not necessary either, but it can be quite useful as it simply means verbose (extended/detailed) output in the terminal window.

So you if you want to see the progress of the file operation, include -v as well, so the final command becomes:

tar -xvf <filename>

Remember it by saying out loud: tar eXtract Verbose File <filename>

Or, as mentioned, simply use: tar -xf <filename> (tar eXtract File <filename>)

How to restart KDE Plasma shell if it freezes

If your KDE Plasma environment is hanging or freezing you don’t necessarily have to logout and login. Open the Search/Run prompt by pressing ALT+F2 and enter konsole to open your terminal.

Then simply issue the necessary command to restart plasma shell.

For KDE4:

killall plasma-desktop
kstart plasma-desktop


killall plasmashell
kstart plasmashell

KDE5.10 and above:

kquitapp5 plasmashell
kstart5 plasmashell

Credits goes out to Korcia at this AskUbuntu thread.

Add text color to Linux bash scripts

Bash supports coloring text, but I never remember the syntax, so here’s a little cheat sheet. During my little research it also seems you can enter the color codes in two ways as illustrated below:

echo -e "\033[0;32m Hello world!"
echo -e "\e[32m Hello world!"

Some more examples:

Difficult syntax:
\033[0;xxm (where xx is color code)
Easier syntax:
\e[xxm (where xx is color code)

I’m not sure if there’s any reason to choose one or the other, but the latter example is arguably easier to remember.

All color codes:

Reset color   0
Black 0;30
Blue 0;34
Green 0;32
Cyan 0;36
Red 0;31
Purple 0;35
Brown/Orange 0;33
Light Gray 0;37
Dark Gray 1;30
Light Blue 1;34
Light Green 1;32
Light Cyan 1;36
Light Red 1;31
Light Purple 1;35
Yellow 1;33
White 1;37

You can also do lots of other things in bash, such as blinking text, bold, etc. Take a look at this site for more examples.

If you like to create CLI tools, adding some colors will make it a lot more immersive and easier to read output, like this example:

Linux terminal command equivalents in Windows

sudo <cmd>runas /user:admin <cmd>
This will run the command with admin-privileges on Linux, while on Windows it’ll simply run as the user provided (so you need to run as a user who has local administrator rights)
cat src1.js src2.js > compiled.jstype src1.js src2.js > compiled.js
Combine two or more files into one
ifconfigipconfigShows network information
traceroutetracertTrace an address and show routes
uptimenet stats server or net stats workstationWindows don’t have a builtin command to show uptime in days, but this command will show you when the machine was last booted (do the math)
lsdirShow directory contents
freememShow available RAM

I started on this article a couple years ago but was quickly forgotten in the drafts bin. Now I actually don’t have any Windows computers available anymore and can’t be arsed to look up more commands so this’ll have to work for now.

Some simple commands such as cd, mkdir, rm, rmdir has been ignored for now as they’re equal in both operating systems.

Please post a comment below if you know of any useful commands available on both operating systems.

List disks with fdisk without showing loopback disks created by snap apps

You might’ve noticed that Snap apps creates a loopback disk used for mounting the image file for the app. This clutters the UI of fdisk, df and other disk related tools.

A typical fdisk -l could show you this:

Since most regular drives starts as /dev/sd* you could simply grep the output:

sudo fdisk -l | grep "Disk /dev/sd"

Or even better, use sudo fdisk -l | grep -v "loop". The -v parameter means grep reverts the keyword so anything containing “loop” will not be shown.

Another example: df -h:

Now: df -h | grep -v loop

Note regarding grep: You only need to add quotation marks around the keyword if it’s a string which contains spaces. Single words don’t need quotes.

You can also pipe it several times through grep, like this:

sudo df -h | grep -v loop | grep -v tmp

Or take a look at this Stackoverflow thread for more grep variants

Missing the new community theme Yaru after upgrading from Ubuntu 18.04 to 18.10

I just upgraded from 18.04 to 18.10 yesterday, but to my dismay, the new fancy theme, Yaru, was nowhere to be found. Looked around in Gnome Tweaks to no avail. Turns out some of the theme packages were not properly installed, more specifically the yaru-theme-gtk package. 

Enter the command apt search yaru-* to see installation status of every package containing “yaru-*”. If they are installed they will state [Installed] inside the square brackets, like so:

If they’re not installed, you can simply fix the issue by running sudo apt install yaru-*. Once complete, log out and back in, and the theme files will be available in Gnome Tweaks

Voila! Remember to also set CursorIcons and Sound theme to Yaru in Gnome Tweaks if desired.