Comparing strings in Bash

Compare string in BASH

The need to compare strings in a Bash script is relatively common and can be used to check for certain conditions before proceeding on to the next part of a script. A string can be any sequence of characters. To test if two strings are the same, both strings must contain the exact same characters and in the same order. It could be a word or a whole sentence. For example, string one is equal to string one but is not equal to string two. Get the idea?

In this guide, we’ll show you how to compare strings in the Bash shell on a Linux system. We’ll show this in the context of a simple if/else Bash script so you can see how testing for this condition would work when developing scripts, but we’ll also show how this same comparison can be done in the command line terminal.

In this tutorial you will learn:

  • How to compare strings in Bash
  • Example if/else Bash scripts that compare strings

Read more

Convert timestamp to date and vice versa

Convert timestamp to date

The date command on a Linux system is a very versatile command that can be used for many functions. Among them is the ability to calculate a file’s creation date, last modified time, etc. This can be built into a script, used for scheduling, or just used to obtain basic information about a file or directory on the system.

The date command can also handle addition and subtraction arithmetic to help calculate dates and times. It uses Unix’s epoch time as a base of reference, which is 00:00:00 UTC on January 1, 1970. From this date, it can assign a timestamp based on the number of seconds something occurred before or after it.

These timestamps are great for calculation, but hardly usable for people. It’d take a machine to know that Thu 02 Jun 2016 12:59:59 PM UTC translates to 1464872399. In this guide, we’ll explain how to convert a timestamp to a human readable date and vice versa.

In this tutorial you will learn:

  • How to convert a timestamp to date

Read more

Deleting a user from MySQL

How to delete MySQL/MariaDB user

If you have an outdated or unused account in your MySQL or MariaDB database, it’s best to get rid of it. Having even one extra user is an additional vulnerability and attack surface in the database. In this guide, we’ll show you the step by the step instructions to delete a specific user from a MySQL or MariaDB database from the command line on a Linux system.

DID YOU KNOW?
If you’ve forgotten the password to a user account and need to reset it, there’s no need to delete it and start over. We have separate guides for changing a user password in MySQL and changing the root password in MySQL.

In this tutorial you will learn:

  • How to delete a user from MySQL/MariaDB database

Read more

npm on Linux

Install npm on Linux

npm is the package manager for Node.js and the JavaScript coding language. It can be installed on a Linux system and then used on the command line to download and install JavaScript packages and their requisite dependencies.

It’s especially useful for developers working with Node.js, as npm’s online registry contains a plethora of JavaScript packages that can be browsed and downloaded with ease. It’s available for installation on any major Linux distro and operates in much the same way as a distro’s package manager, which you’re probably already familiar with.

In this guide, we’ll show you how to install npm on various Linux distributions. We’ll also show you basic usage commands for npm, such as installing and removing software packages.

In this tutorial you will learn:

  • How to install npm on major Linux distributions
  • Basic usage commands for npm

Read more

Redis on Ubuntu

How to install Redis on Ubuntu Linux

Redis is open source software used as a database and cache that sits in memory, allowing for exceptional performance. When you’re ready to give this lightning fast program a try, the developers recommend installing Redis on a Linux system, and what better candidate than Ubuntu Linux?

In this tutorial, we’ll guide you through the step by step instructions of installing Redis (both server and client) on Ubuntu. Then, we’ll verify that it’s connectable and configure the UFW firewall to allow incoming connections.

In this tutorial you will learn:

  • How to install Redis Server and Client on Ubuntu Linux
  • How to perform a connection test and configure UFW to allow Redis

Read more

pip on Linux

Install pip on Linux

pip is the package manager for the Python coding language. It can be installed on a Linux system and then used on the command line to download and install Python packages and their requisite dependencies.

This gives developers – as well as users who are just executing Python programs but not developing them – an easy way to download software packages written in Python. It’s available for installation on any major Linux distro and operates in much the same way as a distro’s package manager, which you’re probably already familiar with.

In this guide, we’ll show you how to install pip for Python 2 and Python 3 on various Linux distributions. We’ll also show you basic usage commands for pip, such as installing and removing software packages.

In this tutorial you will learn:

  • How to install pip for Python 2 and Python 3 on major Linux distros
  • Basic usage commands for pip

Read more

Redirect HTTP traffic to HTTPS in NGINX

How to use Nginx to redirect all traffic from http to https

If your website is hosted with NGINX and it has SSL enabled, it’s best practice to disable HTTP completely and force all incoming traffic over to the HTTPS version of the website. This avoids having duplicate content and ensures that all of the site’s users are only browsing the secure version of your website. You should also see an SEO boost, as search engines prefer non-redundant and secured web pages.

In this guide, we’ll assume you’re already using NGINX on a Linux system and want to redirect all HTTP traffic to HTTPS. Even if a user happens to follow an http:// link, the site should send them to the correct and secured page, which happens instantly and without the user’s intervention.

There are two ways to setup this redirection in NGINX. One method allows you to configure the redirection for individual sites. The other method can redirect HTTP to HTTPS for all NGINX sites on your server, which is handy if you have multiple sites setup and want to avoid having to apply the exact same redirection to each one. We’ll cover the step by step instructions for both methods below. Let’s get started.

NOTE
Using Apache instead of NGINX? We’ve written a separate guide for how to use Apache to redirect all HTTP traffic to HTTPS.

In this tutorial you will learn:

  • How to redirect HTTP to HTTPS for individual NGINX websites
  • How to redirect HTTP to HTTPS for all NGINX websites

Read more

Changing hostname on Linux

How to change hostname on Linux

The hostname of a Linux system is important because it’s used to identify the device on a network. The hostname is also shown in other prominent places, such as in the terminal prompt. This gives you a constant reminder of which system you’re working with. It’s a real life saver when you’re managing multiple systems through SSH and those command line terminals start to blend together in your mind.

Of course, IP addresses are used when devices need to communicate with each other, but those can change frequently. Hostnames give us a way to know which device we’re interacting with either on the network or physically, without remembering a bunch of numbers that are subject to change. Thus, it’s important that your system bears a hostname which helps you to identify it quickly. For example, “backup-server” is much more informative than “server2.”

In this guide, we’ll show you how to change the hostname on Linux. This can be done via command line or from GUI, and we’ll be showing the methods for both. If you can’t easily identify a system’s purpose from the hostname, it’s time to change it.

In this tutorial you will learn:

  • How to change hostname from command line
  • How to change hostname from GNOME GUI

Read more

Notepad++ on a Linux system

How to install Notepad++ on Linux

Notepad++ is a very popular text editor that’s only built for Windows and doesn’t have official support for Linux systems. However, it’s now pretty easy to install Notepad++ on major Linux distros thanks to Snap packages.

Old methods for installing Notepad++ relied on using Wine for a compatibility layer and installing the program through Winetricks. That method still works, but the Snap package has Wine dependencies baked in, taking the fuss out of the whole process.

In this guide, we’ll take you through the steps of installing Notepad++ on a Linux system by using Snap. Not all major distros have native access to Snaps just yet, so we will also be covering how to enable them on an assortment of popular distributions.

In this tutorial you will learn:

  • How to enable Snap support on major distros
  • How to install Notepad++ Snap package

Read more

Unzip command on Linux

How to unzip a zip file from command line and GUI

You might think that zip files belong on Windows, not Linux systems. Still, it’s a popular compression method and chances are that you’ll run across them online from time to time. Either that, or your Windows buddy will send you a zip file that you want to open.

In this guide, we’ll show you how to unzip (decompress) zip files on Linux. You’ll learn a command line method as well as a GUI method in the step by step instructions below.

In this tutorial you will learn:

  • How to unzip a zip file via command line
  • How to unzip a zip file via GUI

Read more

Redirect HTTP traffic to HTTPS in Apache

How to use Apache to redirect all traffic from http to https

If your website uses Apache and SSL, there’s not much reason to keep using HTTP with your website. Having both HTTP and HTTPS just creates duplicate content, as now any given page will be accessible through two technically different URLs.

In this guide, we’ll assume you’re already using Apache on a Linux system and want to redirect all HTTP traffic to HTTPS. This will make sure that all your visitors are only connecting through HTTPS by forcing their browser over to the secure protocol if they happen to open an HTTP link. If a user decides to preface a link with http://, your site will be smart enough to still send them to the correct page, rather than showing duplicate content or displaying a 404 error.

There are two ways to set up this redirection in Apache. The better method is to configure Virtual Host, but users with hosted websites may not have access to this configuration. The second method is by making some changes to the .htaccess file. We’ll cover the step by step instructions for both methods below. Let’s get started.

In this tutorial you will learn:

  • How to redirect HTTP to HTTPS with Virtual Host
  • How to redirect HTTP to HTTPS with .htaccess file

Read more