Home » Linux » Shell

How to Use “if… else” in Bash Scripts (with Examples)

if else bash

Bash scripting is a vital tool for developers to automate tasks in Linux. Bash scripts can be used to automate development tasks locally (like uploading files for deployment, compiling apps, or resizing images in bulk), as well as for server-side tasks (sending scheduled emails, collecting data at intervals, or sending notifications to devices). You’ll want to run some tasks dependent on the outcome of another task or variable, and that’s where if … else will help you. This decision making process in Bash scripts is called using conditionals. … Read more

Home » Linux » Shell

Linux cp Command: How to Copy Files and Directories

Linux cp command How to copy files and directories

In this tutorial, we explain how to use the cp command in Linux to copy files or directories. Examples are included below. Linux is one of the most popular Operating Systems on the planet and is the basis for all Android devices. It is open-source and has some clear advantages over other systems such as Windows or macOS. Within Linux, there are a considerable number of different commands that programmers use regularly. What is the cp command? The copy (cp) command is one of the … Read more

Home » Linux » Shell

Debian: How to Add a User to Sudoers

Debian How to Add a User to Sudoers 1

In the Filesystem Hierarchy Standard used by Linux operating systems, all files and directories appear under the root directory “/” but access to it is often restricted for security reasons. In Linux distributions like Debian, you can gain full access from the SSH by using the “sudo” command. This tutorial explains how to add a user to Sudoers so that the user is permitted to run the sudo command. Logging in as a superuser or root user enables you to make system-wide modifications. “sudo” can … Read more

Home » Linux » Shell

Linux tr Command with Examples

linux tr command 1

In Linux and Unix systems, tr is a command-line utility that translates, erases, and “squeezes” repeated characters – in fact, tr stands for “translate.” This guide explains how to use the tr command in Linux, with examples. It can be used for operations such as removing repeated characters, converting lowercase to uppercase, and basic replacing and removing of characters. It is often used in conjunction with other commands through piping. Linux is the basic kernel/operating system core behind much of the technology we use on … Read more

Home » Linux » Shell

How to Grep for Multiple Words, Strings and Patterns

How to Grep for Multiple Words Strings and Patterns 1

This guide explains how to use the grep utility for searching multiple words, strings, and patterns. You can find full examples of the commands used to accomplish this below. What Is Grep? When we refer to grep, we’re talking about the command-line function. Grep stands for Global Regular Expression Print. In essence, it will search input files looking for lines that are a match to a certain regular expression. It then returns results by writing each line for standard output. The grep utility is suitable … Read more

Home » Linux » Shell

How to Get CPU Information on Linux

how to get cpu information linux

Within your computer, the central processing unit, aka the CPU, is a critical component. Despite its essential nature in performing countless data processing functions, many of us likely know very little about the CPU, hidden away inside the computer out of sight as it works its magic. Thankfully, finding CPU information on Linux systems is fairly straightforward, so let’s take a look at how it’s done. Why You Might Need CPU Information There are a few reasons why someone may want to obtain further CPU … Read more

Home » Linux » Shell

Tiny bash scripts: check Internet connection availability

Sometimes it is necessary to check whether server you want to run some big bash script is connected to Internet. Usually it makes sense while running scripts periodically using cron.  Below is the tiny bash script for this purpose: #!/bin/bash WGET=”/usr/bin/wget” $WGET -q –tries=10 –timeout=5 http://www.google.com -O /tmp/index.google &> /dev/null if [ ! -s /tmp/index.google ];then echo “no” else echo “yes” fi As you see it tries to download google’s index page, if it’s not empty script returns “yes”, if there is not Internet connection … Read more

Home » Linux » Shell

Create Linux user with password

Sometimes it’s necessary to create Linux user accounts in batch mode (fully automatic) but often newbies ask how to set password for a new user without entering it manually. Thanks to heaven command useradd can get password as an input parameter, but it should be encrypted. In other words, to create Linux user account with password the following command will be useful: useradd -m -p encryptedPass username I know at least two ways to get password encrypted. The first one is to use perl crypt(); … Read more

Home » Linux » Shell

Find out where Unix/Linux executable binary is located

There are two commands that may help you to find where executable binary is located regardless it’s Unix or Linux system. They are whereis and type. First locates source/binary and manuals sections for specified files and second tells what exactly shell executes when you type a certain command. The next picture shows examples of these commands work.