“test” operators in Bash

Bash Conditional Expressions

Here I am trying to list almost all available bash “tests” (file tests, string tests, arithmetic tests)

file operators description
-e <FILENAME> True, if exists
-f <FILENAME> True, if exists and is a regular file
-d <FILENAME> True, if exists and is a directory
-c <FILENAME> True, if exists and is a character special file
-b <FILENAME> True, if <FILENAME> exists and is a block special file
-p <FILENAME> True, if <FILENAME> exists and is a named pipe (FIFO)
-S <FILENAME> True, if <FILENAME> exists and is a socket file
-L <FILENAME> True, if <FILENAME> exists and is a symbolic link
-h <FILENAME> True, if <FILENAME> exists and is a symbolic link
-g <FILENAME> True, if <FILENAME> exists and has sgid bit set
-u <FILENAME> True, if <FILENAME> exists and has suid bit set
-r <FILENAME> True, if <FILENAME> exists and is readable
-w <FILENAME> True, if <FILENAME> exists and is writable
-x <FILENAME> True, if <FILENAME> exists and is executable
-s <FILENAME> True, if <FILENAME> exists and has size bigger than 0
-t <fd> True, if file descriptor <fd> is open and refers to a terminal
<FILENAME1> -nt <FILENAME2> True, if <FILENAME1> is newer than <FILENAME2> (mtime)
<FILENAME1> -ot <FILENAME2> True, if <FILENAME1> is older than <FILENAME2> (mtime)
<FILENAME1> -ef <FILENAME2> True, if <FILENAME1> is a hardlink to <FILENAME2>

 

string operators description
-z <STRING> True, if <STRING> is empty
-n <STRING> True, if <STRING> is not empty (this is the default operation)
<STRING1> = <STRING2> True, if the strings are equal
<STRING1> != <STRING2> True, if the strings are not equal
<STRING1> < <STRING2> True, if <STRING1> sorts before <STRING2>
<STRING1> > <STRING2> True, if <STRING1> sorts after <STRING2>

 

arithmetic operators description
<INTEGER1> -eq <INTEGER2> True, if the integers are equal
<INTEGER1> -ne <INTEGER2> True, if the integers are NOT equal
<INTEGER1> -le <INTEGER2> True, if the first integer is less than or equal second one
<INTEGER1> -ge <INTEGER2> True, if the first integer is greater than or equal second one
<INTEGER1> -lt <INTEGER2> True, if the first integer is less than second one
<INTEGER1> -gt <INTEGER2> True, if the first integer is greater than second one

Differences between innodb and myisam mysql storage engines

Data in MySQL is stored in files (or memory) using a variety of different techniques. Each of these techniques employ different storage mechanisms, indexing facilities, locking levels and ultimately provide a range of different functions and capabilities. By choosing a different technique you can gain additional speed or functionality benefits that will improve the overall functionality of your application.

Comparison between the MyISAM and InnoDB storage engines of MySQL.

innodb myisam

1

row-level locking table-level locking

2

supports transaction does not support transactions

3

foreign key constraints no foreign key constraints

4

row count is not stored internally and so slow COUNT(*)s row count is stored internally and so fast COUNT(*)s

5

automatic crash recovery no automatic crash recovery, but it does offer repair table functionality

6

stores both data and indexes in one file stores indexes in one file and data in another

7

uses a buffer pool (innodb_buffer_pool_size) to cache both data and indexes uses key buffers (key_buffer) for caching indexes and leaves the data caching management to the operating system

8

ACID(Atomicity, Consistency, Isolation and Durability) compliant not ACID compliant

mount: could not find any free loop device

One day I got 35 iso images and I created a script for mounting it all. But after 8 loop device mounting I can’t do any more. I was keep getting the error mount: could not find any free loop device
So I decided to increase the number of loop devices in the system.
Below are the steps which I did for increasing loop devices on my SUSE_11.4-x86_64 box

One day I got 35 iso images and I created a script for mounting it all. But after 8 loop device mounting I can’t do any more. I was keep getting the error mount: could not find any free loop device

So I decided to increase the number of loop devices in the system.

Below are the steps which I did for increasing loop devices on my SUSE_11.4-x86_64 box

1. Created a file named loop.conf in /etc/modprobe.d/

2. Added following entry in loop.conf

3. Reloaded the loop module

Import csv file to a mysql table

Using the LOAD DATA INFILE SQL statement we can import data to a mysql table. Suppose I have a table named from_csv in the database test_csv.

Using the LOAD DATA INFILE SQL statement we can import data to a mysql table.

Suppose I have a table named from_csv in the database test_csv.

And I have csv file with following content

The following query will import this csv file to mysql table from_csv

After the execution of the query the contents of the table: is:

Compile apache with suphp on Ubuntu

One day my client asked me to setup a webserver on a control panel less Ubuntu server. He needs as php as suphp. Following are the steps which I followed during the web server setup.

One day my client asked me to setup a webserver on a control panel less Ubuntu server. He needs as php as suphp.

Following are the steps which I followed during the web server setup.

Downloaded all neccessary sources:

1. First compile apache from the source with following configure options
Continue reading “Compile apache with suphp on Ubuntu”

Match a pattern and delete n lines after it

Suppose you have a file named file.txt with following content. Delete all 2 lines [including pattern itself]after the pattern match “Class”.

I got this trick from SERVERFAULT.COM 🙂

Situation:

Suppose you have a file named file.txt with following content
Continue reading “Match a pattern and delete n lines after it”

bash: /bin/rm: Argument list too long

bash: /bin/rm: Argument list too long. If you’re trying to delete files inside a directory and the following command is not working:

If you’re trying to delete files inside a directory and the following command is not working:

In this case you can delete all files using find with appropriate switches:

If you want to delete files in verbose mode:

rsync – a fast, versatile, remote (and local) file-copying tool

Suppose you have a load of files and folders which don’t want to backup. In this case rsync have option, define all the files and folders which want to exclude in a single file and make rsync to read from it.
Copying a file from one location to another(under same file system hierarchy)
Copying a file from one machine another remote machine
Copying all files except one
Delete files from the destination folder that are no longer required (i.e. they have been deleted from the folder being backed up)

Rsync is a fast and extraordinarily versatile file copying tool. It can copy locally, to/from another host over any remote shell, or to/from a remote rsync daemon. It offers a large number of options that control every aspect of its behavior and permit very flexible specification of the set of files to be copied. It is famous for its delta-transfer algorithm, which reduces the amount of data sent over the network by sending only the differences between the source files and the existing files in the destination. Rsync is widely used for backups and mirroring and as an improved copy command for everyday use.

Some of the additional features of rsync are:

  • support for copying links, devices, owners, groups, and permissions.
  • exclude and exclude-from options.
  • can use any transparent remote shell, including ssh or rsh.
  • does not require super-user privileges.
  • support for anonymous or authenticated rsync daemons (ideal for mirroring).

I think it is better to study rsync command through examples.
Continue reading “rsync – a fast, versatile, remote (and local) file-copying tool”

Generating and installing SSL certificate

Once the private key is generated a Certificate Signing Request can be generated. The CSR is then used in one of two ways. Ideally, the CSR will be sent to a Certificate Authority, such as Thawte or Verisign who will verify the identity of the requester and issue a signed certificate. The second option is to self sign the CSR.

Before proceeding with installation, make sure that Apache is compiled with mod_ssl. You can check the same by executing the following command in the server.

Continue reading “Generating and installing SSL certificate”

Checking SSL cert’s validity and other details

OpenSSL is a cryptography toolkit implementing the Secure Sockets Layer (SSL v2/v3) and Transport Layer Security (TLS v1) network protocols and related cryptography standards required by them. The openssl program is a command line tool for using the various cryptography functions of OpenSSL’s crypto library from the shell

1. Get complete available details of an SSL certificate

2. Who issued the certificate?
Continue reading “Checking SSL cert’s validity and other details”