MySQL: Run Query from Bash Script or Linux Command Line

Sometimes it is needed to run some MySQL queries from the Linux command-line interface without accessing the interactive MySQL prompt.

For example, when it is required to schedule a backup of MySQL database or to automate execution of some SQL queries with a Bash script.

In this article i will show the most useful, from my point of view, options of the MySQL command-line client and show how to run multiple SQL queries to a database from a Bash script. (more…)

HowTo: Generate and Print a Sequences of Numbers with BASH

You don’t have to type a range of numbers by hands!

BASH already has a built-in utility, named seq, for generating and printing a sequence of numbers.

Generate a sequence of numbers

Syntax: seq [OPTION]… FIRST
Syntax: seq [OPTION]… FIRST LAST

Print a sequence of numbers from 1 to 10:

$ seq 1 5
1
2
3
4
5

Generate a sequence of numbers with increment

Syntax: seq [OPTION]… FIRST INCREMENT LAST

Print a sequence of numbers from 0 to 20 with increment 5:

$ seq 0 5 20
0
5
10
15
20

Equalize width by padding with leading zeroes

Syntax: seq -w…

Equalize width by padding with leading zeroes:

$ seq -w 1 10
01
02
03
04
05
06
07
08
09
10

Use another separator

Syntax: seq -s SEPARATOR… – use SEPARATOR to separate numbers.

Default separator is a new line – "\n".

Use space to separate numbers:

$ seq -s " " 1 10
1 2 3 4 5 6 7 8 9 10

Check a Website Response Time from the Linux Command Line

Test you website response time from the Linux command line with CURL.

Total website response time

Use the following command to get a total response time, in seconds.

$ curl -s -w %{time_total}\\n -o /dev/null http://www.shellhacks.com

Sample output:

0,117

Brief options description:

Option Description
-s Quiet mode. Don’t show progress meter or error messages
-w Defines what to display on stdout after a completed and successful operation
-o Write output to ‘/dev/null’
time_total The total time, in seconds, that the full operation lasted

Detailed timing of a website response

The following command returns lookup, connect, pretransfer, starttransfer time in seconds and the total time that the full operation lasted.

$ curl -s -w '\nLookup time:\t%{time_namelookup}\nConnect time:\t%{time_connect}\nPreXfer time:\t%{time_pretransfer}\nStartXfer time:\t%{time_starttransfer}\n\nTotal time:\t%{time_total}\n' -o /dev/null http://www.shellhacks.com

Sample output:

Lookup time:    0,004
Connect time:   0,022
PreXfer time:   0,022
StartXfer time: 0,068

Total time:     0,125

Brief options description:

Option Description
Lookup time (time_namelookup) The time, in seconds, it took from the start until the name resolving was completed
Connect time (time_connect) The time, in seconds, it took from the start until the TCP connect to the remote host was completed
PreXfer time (time_pretransfer) The time, in seconds, it took from the start until the file transfer was just about to begin. This includes all ‘pre-transfer’ commands and negotiations that are specific to the particular protocol(s) involved
StartXfer time (time_starttransfer) The time, in seconds, it took from the start until the first byte was just about to be transferred. This includes ‘time_pretransfer’ and also the time the server needed to calculate the result

More detailed timing of a website response

The following command adds appconnect and redirect time in seconds, to the previous report. These options are available in a latest versions of CURL.

$ curl -s -w '\nLookup time:\t%{time_namelookup}\nConnect time:\t%{time_connect}\nAppCon time:\t%{time_appconnect}\nRedirect time:\t%{time_redirect}\nPreXfer time:\t%{time_pretransfer}\nStartXfer time:\t%{time_starttransfer}\n\nTotal time:\t%{time_total}\n' -o /dev/null http://www.shellhacks.com

Sample output:

Lookup time:    0,003
Connect time:   0,020
AppCon time:    0,000
Redirect time:  0,000
PreXfer time:   0,020
StartXfer time: 0,963

Total time:     1,001

Brief options description:

Option Description
AppCon time (time_appconnect) The time, in seconds, it took from the start until the SSL/SSH/etc connect/handshake to the remote host was completed (Added in 7.19.0)
Redirect time (time_redirect) The time, in seconds, it took for all redirection steps include name lookup, connect, pretransfer and transfer before the final transaction was started. ‘time_redirect’ shows the complete execution time for multiple redirections. (Added in 7.12.3)

Use --version to see if your CURL supports these options.

$ curl --version

Response Times: The 3 Important Limits

Short note for your information.

  • 0.1 second – is about the limit for having the user feel that the system is reacting instantaneously, meaning that no special feedback is necessary except to display the result;
  • 1.0 second – is about the limit for the user’s flow of thought to stay uninterrupted, even though the user will notice the delay. Normally, no special feedback is necessary during delays of more than 0.1 but less than 1.0 second, but the user does lose the feeling of operating directly on the data;
  • 10 seconds – is about the limit for keeping the user’s attention focused on the dialogue. For longer delays, users will want to perform other tasks while waiting for the computer to finish, so they should be given feedback indicating when the computer expects to be done. Feedback during the delay is especially important if the response time is likely to be highly variable, since users will then not know what to expect.

Check a Website Availability from the Linux Command Line

You can easily test a a website availability from the Linux command line and get the status codes from the web-server using commands like TELNET or CURL.

Check a website availability with CURL

Execute the following command to check whether a web site is up, and what status message the web server is showing:

$ curl -Is http://www.shellhacks.com | head -1
HTTP/1.1 200 OK

Status code ‘200 OK’ means that the request has succeeded and a website is reachable.

Here is an another example that shows you how curl displays different status codes.

$ curl -Is http://shellhacks.com | head -n 1
HTTP/1.1 301 Moved Permanently

You’ll notice that if you visit: http://shellhacks.com you are redirected to http://www.shellhacks.com, because I prefer to have www in my site’s URL. I do this by implementing a 301 Redirect (Permanently moved) for any visitor who goes to http://shellhacks.com.

You can also check the availability of a particular page on the site:

$ curl -Is http://www.shellhacks.com/Bash-Colors | head -n 1
HTTP/1.1 200 OK

Read more: Status Code Definitions

Check a website availability with TELNET

You can also test website availability and get the response code using telnet command:

$ telnet www.shellhacks.com 80
Trying 91.206.200.119...
Connected to www.shellhacks.com.
Escape character is '^]'.
HEAD / HTTP/1.0
HOST: www.shellhacks.com
<PRESS ENTER>
<PRESS ENTER>

You will get the output as follows:

HTTP/1.1 200 OK
Server: nginx/1.1.10
Date: Sun, 26 May 2013 19:29:46 GMT
***

That also means that the website is OK.

HowTo: Clear BASH History

Sometimes you don’t want to leave Bash history, because it may contain some sensitive data (e.g. passwords, tokens, etc.).

This article will help you to take a full control on your .bash_history file.

I will show how to clear the commands history for the current Bash session, how to prevent particular commands from being recorded to the .bash_history file and how to completely erase the Bash history. (more…)

HowTo: Count Number of Files in a Directory

Here are the best ways of finding and counting the number of files or folders in a particular directory.

Count the number of files in a current directory

Using “ls” command:

This method is the best, if you need to count a large number of files.

$ ls -f . | wc -l

This command also enables -a option, so . , .. and other files starting with . , will be counted.

Using “find” command:

$ find . -type f  -maxdepth 1 | wc -l

Count the number of files recursively

The following command recursively counts the number of files in a current directory and all its sub-directories:

$ find . -type f | wc -l

Count the number of folders recursively

The following command recursively counts the number of folders in a current directory and all its sub-directories:

$ find . -type d | wc -l

Count the number of files by “file type”

The following command recursively counts the number of files, with “.txt” extension, in a current directory and all its sub-directories:

$ find . -type f -name "*.txt" | wc -l

Apache Web-Server Installation on CentOS/RHEL

This guide explains how to install Apache Web Server on CentOS/RHEL based systems.

Apache Server Installation

Execute the following command to install the latest Apache Server from basic CentOS/RHEL repositories:

# yum install httpd

Set the Apache Server daemon to start at boot.

# chkconfig httpd on

Basic Apache Configuration

Backup the Apache configuration file httpd.conf.

# cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.backup

Open the Apache configuration file and un-comment the line, containing the text “NameVirtualHost *:80”.

# vi /etc/httpd/conf/httpd.conf

Save and close the file.

Firewall opening for Apache Server

Add the rules to IPTABLES.

# vi /etc/sysconfig/iptables

Append the following lines before the REJECT line, to open http and https ports 80 and 443:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT

Save and close the file. Restart the firewall.

# service iptables restart

Confirming the Apache Server Installation

Start the Apache HTTP Server daemon.

# service httpd start

Visit http://localhost/ in your web browser, if you’ve installed server on your local machine, or enter the server’s IP address. You should see an Apache Test Page.

MySQL: Create Database – Command Line

From this small tutorial you will learn how to create a MySQL database from the command-line in Linux.

I will show the general MySQL CREATE DATABASE syntax for creating a database with a default character set.

Additionally i will show how to create a user in MySQL, set him a password, grant all privileges on this newly created database and allow him to access it locally. (more…)

Sharing a Directory Content over HTTP with Python

Sometimes, you need to share some files or directories from your Linux machine in a fast and easy way.

There is no simpler way than …

Using Simple HTTP Server – a module that comes with Python (>= 2.4). In most cases you don’t need to install or configure anything!

It creates a Simple HTTP Server, that turns the current directory into your web server directory, that gives you an opportunity to share files from it over HTTP.

# For Python >=2.4
python -m SimpleHTTPServer
# For Python 3.x
python -m http.server

Now you can access the shared folder by visiting http://your_ip_address:8000.

By default Simple HTTP Server will listen to 0.0.0.0:8000, but port number can be changed:

# For Python >=2.4
python -m SimpleHTTPServer 8888
# For Python 3.x
python -m http.server 8888

0.0.0.0 – means that the server is not bound to a specific address, and will listen on all configured network interfaces.

Backup Site Recursively from FTP with Wget

Backing up your WebSite is a necessary step for all users.

This article describes how to recursively download your WebSite with all files, directories and sub-directories from FTP server, using Wget utility.

First of all create a folder in which you are going to download a site. For example, let’s create the folder backups in a home directory.

# mkdir ~/backups
# cd ~/backups

Download Entire Site from FTP

The following command recursively downloads your site with all its files and folders from FTP server and saves them to the current directory.

# wget -r -l 0 -nH ftp://user:pass@ftp.server.com
Option Description
user FTP username
pass FTP password
ftp.server.com IP address or domain name of an FTP server
-r, –recursive Recursive retrieving
-l, –level Maximum recursion depth (0 = unlimit)
-nH, –no-host-directories Disable generation of host-prefixed directories

Your site has been downloaded:

# ls -l
drwxr-xr-x 4 user group 4096 2013-05-09 18:20 yoursite.com

Backup Downloaded Site

Now you can compress the folder with your site as follows:

# tar -czf site-backup-$(date +%Y%m%d-%H%M%S).tar.gz yoursite.com

The previous command creates an archive, named something like site-backup-20130509-190638.tar.gz.

To extract the archive, type:

# tar -zxvf site-backup-20130509-190638.tar.gz

Download a Particular Folder from FTP

Let’s say we have the following structure in FTP home directory:

/yoursite.com/www/images

The following command recursively downloads ‘images’ folder, with all its content from FTP server and saves it to the current directory.

# wget -r -l 0 -nH -np --cut-dirs=2 ftp://user:pass@ftp.server.com/yoursite.com/www/images
Option Description
-np, –no-parent Don’t ascend to the parent directory
–cut-dirs Ignore ‘number’ parent directories

All these tasks have to be automated and added to Cron. The HowTo is coming …