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

-xextract
-ffile

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


KDE5:

killall plasmashell
kstart plasmashell


KDE5.10 and above:

kquitapp5 plasmashell
kstart5 plasmashell

Credits goes out to Korcia at this AskUbuntu thread.

Get the current weather (with graphics!) in your terminal

Developer @igor_chubin has created a new awesome tool for displaying the current weather in beautiful graphs right inside your terminal.

All you have to do is curl the website, wttr.in

If your distro don’t include cURL already, install it (for Debian/Ubuntu-based distros) by running

sudo apt install curl

Then you can simply run

curl wttr.in

to see the weather come to life!

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:

SEO friendly URLs with htaccess and PHP include script

Creating fancy SEO friendly URLs like yoursite.com/contact is actually pretty easy and looks much better than yoursite.com/index.php?page=contact. In addition to looking more professionally, it might give you a small boost for your search engine visibility. This article is meant for PHP novices and will be pretty basic.

What you’ll need:

Before we begin, we will utilize PHP include() so we end up with 1 file as the template (index.php). All other sub pages will simply store the content and not the surrounding html/head/footer code.

So for the purpose of keeping things simple, I’ll paste all the code every step of the way so you can copy & paste it.

Step 1: Creating the template

Assuming your index.php looks like this:

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>SEO Friendly URLs by IT-DB</title>
<link rel="icon" type="image/x-icon" href="res/favicon.ico">
</head>

<body>

<header>
<nav>
<ul>
<li><a href="index.php?page=home">Home</a></li>
<li><a href="index.php?page=portfolio">Portfolio</a></li>
<li><a href="index.php?page=about">About</a></li>
<li><a href="index.php?page=contact">Contact</a></li>
</ul>
</nav>
</header>

<main>
Welcome to our website!
</main>

<footer>
Copyright © it-db.com <?php echo date('Y'); ?>
</footer>

</body>

</html>

We have to implement our PHP include function. Replace Welcome to our website! between the <main></main> tags with the following PHP script:

<?php
$page = !empty($_GET['page']) ? $_GET['page'] : '';
$page = substr(strtolower(preg_replace('([^a-zA-Z0-9-/])', '', $page)), 0, 20);
$def = "home"; // change to your default page
$dir = "inc"; // directory name containing all sub pages
$ext = "php"; // file extension if using anything else than php
if(!empty($page)) {
if(file_exists("$dir/$page.$ext") && is_readable("$dir/$page.$ext") && $page != "index") {
include("$dir/$page.$ext");
}
else {
header("HTTP/1.0 404 Not Found");
echo "Error 404 - File not found";
}
}
else {
include("$dir/$def.$ext");
}
?>

It might look intimidating but it’s very simple. It checks if the URL contains ?page=x, and if so, it tries to show that page (file). It also does some input sanitizing as you can see in the second line (substr(strtolower(preg_replace('([^a-zA-Z0-9-/])', '', $page)), 0, 20);) to avoid hacking exploits. If the page (file) is not found, it will send error message 404.

Keep in mind that index should be saved as .php extension. If you save it as index.html the PHP code will simply not run.

Step 2: Setup URL rewrite using htaccess and mod_rewrite

Create a new file and save it as .htaccess. That’s right, no filename, only the extension. If using Notepad in Windows it might throw a fit because of a lacking filename. In that case, use another editor such as VSCode.

To keep things simple we’ll only use two lines to setup our URL rewrite. Copy and paste the following into your .htaccess file:

RewriteEngine On
RewriteRule ^([^/.]+)/?$ /index.php?page=$1

What this means is that any argument after / will be redirected to index.php?page=. Example: yoursite.com/hi will redirect to yoursite.com/index.php?page=hi (but your browser address bar will ofcourse only show yoursite.com/hi)

Step 3: Create the sub pages

Remember from last step, our PHP script defined “inc” as the name of our directory containing all sub pages. So, go ahead and create a directory called “inc” and then create a new file for every sub page by calling them subpage.php (i.e: Home -> home.php, About -> about.php).

Your file structure should look something like this

Keep in mind that these files should only contain the content related to that sub page. But they’re not limited to just text, you can use HTML and even PHP scripts inside them. Point is, you don’t have to declare the whole <html><head><meta> etc in every file, because that’s already saved in the template file and reused for every sub page.

Step 4: Change the URLs in index.php

While creating our template in the first step, we made a navigation menu like this:

<li><a href="index.php?page=home">Home</a></li>
<li><a href="index.php?page=portfolio">Portfolio</a></li>
<li><a href="index.php?page=about">About</a></li>
<li><a href="index.php?page=contact">Contact</a></li>

Although this will still work, we should change the URLs to use our new SEO friendly URLs. Change them to:

<li><a href="home">Home</a></li>
<li><a href="portfolio">Portfolio</a></li>
<li><a href="about">About</a></li>
<li><a href="contact">Contact</a></li>

That’s it, we’re done! Please let me know in the comments below if you encounter any issues or consult the troubleshooting tips.

Troubleshooting tips

Error “Object not found” or 404 error

Make sure you have mod_rewrite activated. If you recently installed mod_rewrite you may get away by just restarting the apache service. If not, check the guide I mentioned earlier:
https://www.digitalocean.com/community/tutorials/how-to-set-up-mod_rewrite

I’m being redirected to the wrong page

This can happen if your project directory is in another subfolder. Please remove the / from the route in .htaccess:
Change
RewriteRule ^([^/.]+)/?$ /index.php?page=$1
to:
RewriteRule ^([^/.]+)/?$ index.php?page=$1

Thanks for reading!

How to brutally kill a process in Windows environment

Today we’ll go over the various methods to kill a process. Can be useful especially if the process is stuck somehow and you’re having trouble closing/killing it.

Kill process with WMI queries

Open cmd.exe and run command:

wmic process where name='myprocessname.exe' delete

Batch kill in command prompt

Open cmd.exe and run command:

taskkill /IM "myprocessname" /T /F

/IM is required to pick process, but the extra parameters /T and /F are optional. They’re quite useful though:

/T = also kills child processes
/F = force termination of your process

Kill it the powerful way

Indeed. You can also kill it using Powershell, but you need to find the process ID first. Open Powershell prompt and simply type

Get-Process myprocessname | Select -expand id
# use the id returned in the next command:
kill -id id

Kill process using graphical user interface

You’re probably familiar with Task Manager. Open it by right-clicking on the taskbar and click on “Task Manager”. If that fails, or if you need to run it as another user, you can browse to %windir%\system32\ and run the executable file, taskmgr.exe.

If you’re lucky you can just kill the process and get on with your life. But, if you want to figure out what’s wrong, then a great tip is to open the Resource Monitor and Filter by process. This will show you all disk I/O file handles related to your process so you might actually see what exactly is hanging.

Linux terminal command equivalents in Windows

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

Quick explanation of PHP property types

If you have little object-oriented programming experience in PHP you might be a little confused by all the new terms in OOP programming compared to regular functional programming. Before we begin, remember the difference between the terms initialize and instantiate. This is common for all programming languages, not just PHP.

Initialize = assign a variable to a value (all variables/properties must have an initial value when created, even if the compiler does this automatically)
Instantiate = create a new instance of an object (which could be assigned to a variable).

In other words, you could initialize a variable and assign it to a class instantiation. Now, take a look at the example class below with some methods to describe how they work in practice:

<?php
class ITDB
{
private function __construct()
{
// Constructor runs when class is instantiated,
// like this: $test = new ITDB();
}

private function hello1()
{
// Only accessible in current class
}
protected function hello2()
{
// Only accessible in current class AND
// child classes which inherits this one
}
public function hello3()
{
// Accessible to all classes
}

// By declaring the method as "static" we can call the
// methods without having to instantiate the class first
static public function hello4()
{
// Accessible without having to instantiate a
// new ITDB() instance first
}
}

ITDB::hello1(); // Will not work because method is private

$sayhi = new ITDB(); // Instantiate class, assign to $sayhi
$sayhi->hello1(); // Will not work, hello1() is still private
$sayhi->hello3() // This is valid, hello3() is a public method

ITDB::hello4(); // Also valid, hello4() is a public static method

Let me know in the comments if you have any questions regarding this 🙂

Custom button in WordPress navigation for opening prompt() and mailto event

This is a neat little trick you could to do spice up the administration experience for your customers if you sell websites using WordPress as CMS.

First of all you’re gonna have to add a new entry/link in your admin sidebar. To do this, you could use a plugin such as Admin Menu Editor or code it manually through your theme functions.php file by following this instruction.

Now for the exciting part. In the URL for your menu/sidebar link, you’re going to use an inline javascript function which fires a prompt() dialog which shows some general contact information, in addition to actually firing a mailto() event if the user clicks OK.

Paste the following code as the target URL for your link. Remember to edit the text/mail/phone etc. \n just means a newline to make it look a little better. After all, this is just an ugly javascript prompt dialog, not some sexy bootstrap dialog. 

javascript:(function(){
    let subj = prompt("Hi! Contact us at\n\nEmail: support@company.com\nTelephone: 123 45 678.\n\nEnter subject below and press OK to send us an email.\n","Issue regarding WordPress");
    if(subj) { window.location.href = "mailto:support@company.com?Subject="+subj+""; }
})();

Once that’s done, refresh your admin panel and it could look something like this:

For more inline javascript bullshittery you can take a look at this browser bookmark which appends stupid emojis all over the website.

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