Wednesday, December 30, 2020

how to convert from one date format to another format in linux ?

 how to convert from one date format to another format in linux ?

echo 04-11-2021 | { IFS=- read d m y && echo "$y$m$d"; }


echo 04-11-2021 | { IFS=- read d m y && echo "$y/$m/$d"; }


 echo 04-11-2021 | { IFS=- read d m y && echo "$d$m$y"; }


 echo 04-11-2021 | awk -F- '{print $3$2$1}'


echo 04-11-2021 |gawk -F, '{split($1, a, "-"); print a[3] a[2] a[1]}' 


echo 04/11/2021 | { IFS=/ read d m y && echo "$y$m$d"; }


date -d "20211104" "+%Y-%m-%d"


date -d 04/11/2021 +%F


 %F   full date; same as %Y-%m-%d

Monday, December 28, 2020

how to add user using shell script?

 #!/bin/bash -x 

if [ $(id -u) -eq 0 ]; then

read -p "Enter username : " username

read -s -p "Enter password : " password

egrep "^$username" /etc/passwd >/dev/null

if [ $? -eq 0 ]; then

echo "$username exists!"

exit 1


if [ -f /usr/bin/mkpasswd ]; then

pass=$(mkpasswd -m sha-512 $password)

#mkpasswd will create encrypted password.

useradd -m -p "$pass" "$username"

[ $? -eq 0 ] && echo "User has been added to system!" && chage -d0 $username  || echo "Failed to add a user!"


echo "install package apt-get install whois"




echo "Only root may add a user to the system."

exit 2


Monday, November 23, 2020

how to check If the file Is Empty or not using shell script?

 how to check If the file Is Empty or not using shell script?


              FILE exists and has a size greater than zero

Example 1:

touch /$HOME/f1

echo "data" >/$HOME/f2

ls -l /$HOME/f{1,2}

[ -s /$HOME/f1 ] 

echo $?

Sample outputs:


The non-zero output indicates that the file is empty.

[ -s /$HOME/f2 ]

echo $?

Sample outputs:


$? is the return code of the last executed command.

Example 2:

$ ls

$ touch file_1

$ dd if=/dev/zero of=$HOME/file_2 bs=1 count=100

100+0 records in

100+0 records out

$ ls -l file_1 file_2 file_3

ls: file_3: No such file or directory

-rw-rw-r--  1 foo bar   0 Nov 20 20:28 file_1

-rw-rw-r--  1 foo bar 100 Nov 20 20:28 file_2

$ [[ -s file_1 ]]

$ echo $?


$ [[ -s file_2 ]]

$ echo $?


$ [[ -s file_3 ]]

$ echo $?


Thursday, October 1, 2020

Input/Output Error How to Reboot or shutdown the Linux server?

 Input/Output Error : Bad Blocks - How to Reboot or shutdown the Linux server?  

Input/output error while running the command mostly due to it could be bad blocks on the disk. 

In this situation, first suggestion would be to check /var/log/messages for any disk-related alerts (might see some sense key alerts).

tail -n 100 /var/log/messages or tail -n 100 /var/log/syslog

tail -f /var/log/messages


bash: /usr/bin/du: Input/output error

Now if try to reboot,it can also give the same output. You can try to init 6.

# reboot or init 6

bash: /sbin/reboot: Input/output error

# shutdown -r now

bash: /sbin/shutdown: Input/output error

If the above reboot commands don’t work try either forced reboot or shutdown

echo "number" >/proc/sys/kernel/sysrq

The number may be written here either as decimal or as hexadecimal with the 0x prefix. CONFIG_MAGIC_SYSRQ_DEFAULT_ENABLE must always be written in hexadecimal.

Note that the value of /proc/sys/kernel/sysrq influences only the invocation via a keyboard. Invocation of any operation via /proc/sysrq-trigger is always allowed (by a user with admin privileges).

0 - disable sysrq completely

1 - enable all functions of sysrq

Where  b Will immediately reboot the system without syncing or unmounting your disks.

              o Will shut your system off

Forced Reboot

echo 1 > /proc/sys/kernel/sysrq

echo b > /proc/sysrq-trigger

Forced Shutdown

echo 1 > /proc/sys/kernel/sysrq

echo o > /proc/sysrq-trigger

Monday, September 21, 2020

how to install R in ubuntu ?

R is an open-source programming language and free environment that specialises in statistical computing and graphical representation. It is mainly used by statisticians and data miners for developing statistical software and performing data analysis

add to /etc/apt/sources.list

deb trusty/


To obtain the latest R 4.0 packages, add an entry like

deb focal-cran40/


deb bionic-cran40/


deb xenial-cran40/

To obtain the latest R 3.6 packages, use:

deb eoan-cran35/


deb bionic-cran35/


deb xenial-cran35/


deb trusty-cran35/

To obtain the latest R 3.4 packages, use:

deb xenial/


deb trusty/

sudo add-apt-repository 'deb bionic-cran40/'

sudo apt-get update

sudo apt-get install r-base

sudo apt-get install r-base-dev xml2 libxml2-dev libssl-dev libcurl4-openssl-dev unixodbc-dev

If we want to upgrade to the latest version 

sudo apt-get install r-base-dev --reinstall

To install any package under R Example Below 

sudo R


after installation



── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──

✔ ggplot2 3.2.1     ✔ purrr   0.3.3

✔ tibble  2.1.3     ✔ dplyr   0.8.3

✔ tidyr   1.0.0     ✔ stringr 1.4.0

✔ readr   1.3.1     ✔ forcats 0.4.0

── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──

✖ dplyr::filter() masks stats::filter()

✖ dplyr::lag()    masks stats::lag()

Reference :

Monday, June 15, 2020

How to dump the database with triggers and procedures?

Stored procedures and Triggers are first introduced with MySQL 5.0. So if you are still using MySQL older version’s upgrade it to MySQL 5.0 or higher version to use these features.

What is Stored Procedure ?

A stored procedure, by definition, is a segment of declarative SQL code which is stored in the database catalog and can be invoked later by a program, a trigger or even a stored procedure.

What is Triggers ?

Triggers are event-driven specialized procedures, they are stored in and managed by the database. A trigger is a SQL procedure that initiates an action on an event (Like INSERT, DELETE or UPDATE) occurs.

mysqldump will backup by default all the triggers but NOT the stored procedures/functions. There are 2 mysqldump parameters that control this behavior:

--routines - FALSE by default

--triggers - TRUE by default

This means that if you want to include in an existing backup script also the triggers and stored procedures you only need to add the --routines command line parameter:

Backup Stored Procedures and Routines

We need to specify --routines to take backup of stored procedures with data and tables.

The following command will take backup of entire database including stored procedures. For example, your database name is “mydb”.

mysqldump -u root -p --routines mydb > mydb.sql

To take backup of only Stored Procedures and Triggers (Exclude table and data ) use the following command.

mysqldump --routines --no-create-info --no-data --no-create-db --skip-opt mydb > mydb.sql

To Take backup without triggers use following command

mysqldump -uroot -p --skip-triggers mydb> mydb.sql

Restore Procedures

To restore stored procedures in the database simply use the following command, But make sure you have taken backup properly before restoring it to avoid any data loss.

mysql -u root -p mydb < mydb.sql

Tuesday, April 28, 2020

how to find my public ip address in linux ?

Using dig

dig (domain information groper) is a simple command line utility for probing DNS name servers. To find your public IP addresses, use the resolver as in the command below:


dig +short


dig +short

Using host

host command is an easy-to-use command line utility for carrying out DNS lookups. The command below will help to display your system public IP address.

host | grep " has" | awk '{print $4}'


host | grep " has" | awk '{print $4}'

Using wget

curl is a popular command line tool for uploading or downloading files from a server using any of the supported protocols (HTTP, HTTPS, FILE, FTP, FTPS and others). The following
commands display your public IP address.


wget -qO- | xargs echo


wget -qO -


 wget -qO- | xargs echo

 wget -qO- | xargs echo

Using curl

curl is a popular command line tool for uploading or downloading files from a server using any of the supported protocols (HTTP, HTTPS, FILE, FTP, FTPS and others). The following
commands display your public IP address.




The following commands will get you the IP address list to find public IP addresses for your machine:

  • curl
  • curl
  • curl
  • curl
  • curl
  • curl
  • curl
  • curl

Monday, April 20, 2020

nc command examples in linux?

Netcat or nc is a networking utility for debugging and investigating the network.

This utility can be used for creating TCP/UDP connections and investigating them.
The biggest use of this utility is in the scripts where we need to deal with TCP/UDP sockets.

How to Install and Use Netcat in Linux
To install the netcat package on your system, use the default package manager for your
Linux distribution.

[On CentOS/RHEL]

$ yum install nc                  

 [On Fedora 22+ and RHEL 8]
$ dnf install nc                 

 [On Debian/Ubuntu]
$ sudo apt-get install Netcat    

Port Scanning

Netcat can be used for port scanning: to know which ports are open and running services on
a target machine. It can scan a single or multiple or a range of open ports.

 It may be useful to know which ports are open and running services on a target machine.
  The -z flag can be used to tell nc to report open ports, rather than initiate a connection.
Usually it's useful to turn on verbose output to stderr by use this option in conjunction with -
v    option.


    -v      Have nc give more verbose output.
    -w    timeout      Connections which cannot be established or are idle timeout after timeout
seconds.   The -w flag has no effect on the -l option, i.e. nc will listen forever for a connection, with or without the -w flag.  The default is no timeout.

    -z      Specifies that nc should just scan for listening daemons, without sending any data
to them.  It is an error to use this option in con‐junction with the -l option.

  -n      Do not do any DNS or service lookups on any specified addresses, hostnames or

  #scan a single port
$ nc -v -w 2 z 22   

 nc -v -w 2 -z 22 
 Connection to 22 port [tcp/ssh] succeeded!

#scan multiple ports

$ nc -v -w 2 z 22 80  

 #scan range of ports

$ nc -v -w 2 z 20-22 

nc -v -w 2 -z 20-22
nc: connect to port 20 (tcp) failed: Connection refused
nc: connect to port 21 (tcp) failed: Connection refused
Connection to 22 port [tcp/ssh] succeeded!

Find a Service Running on Port

You can also use Netcat to obtain port banners. In this case, it will tell you what service is
running behind a certain port. For example to know what type of service is running behind
port 22 on a specific server, run the following command

nc -v -n 22
Connection to 22 port [tcp/*] succeeded!
SSH-2.0-OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.7

Wednesday, January 15, 2020

Which directory is that bash script in?

There may be times when you need to know the actual location a BASH script is located
within the script. This can be done with a combination of the $0 value and the dirname

The $0 value

A BASH script makes the first command line argument available as $1, the second as $2
and so on. The command run, exactly as it was called but without the command line
arguments, is stored in $0

The dirname command

The dirname command returns the directory name part of a filename. Note that the directory
and/or file do not actually exists; dirname simply strips the last component of a file path.

For example, "dirname /a/b/c" will echo "/a/b", "dirname ../a/b/c" will echo "../a/b", "dirname
../" will echo "." and so on.

Putting it all together
The directory the BASH script is located can be retrieved using dirname $0 like so:

DIRECTORY=`dirname $0`

BUT note that this may be a relative path and not necessarily an absolute one, depending 
how the script is called. Take the following script, for example, which saves the directory
name to a variable for later use, and then echos it:

DIRECTORY=`dirname $0`
If the file is saved at /home/arun/bin/, the permissions are changed so it can be
executed (chmod 0700 ~/bin/ and it is run from /home/arun like so:

then it will echo this:

It it is run like so:

then it will echo this:

Because the path used to run the script in the second example is a relative path, "dirname
$0" only echo’s "bin", which may not be suitable.

Always return the absolute path

To always return the absolute path to the directory the script is located, change to the scripts
directory and get the dirname like so, again saving the value to a variable for later use and
then echoing it:

DIRECTORY=$(cd `dirname $0` && pwd)

If this script is saved as /home/arun/bin/ , then whether it is run as
/home/arun/bin/ or bin/ or by some other path, it will always echo:


Note that although the change directory command (cd) is used, the script will not change
directory and any other calls within it are still relative to the current working directory.

If we were running the above script in /home/arun, then calling "pwd" in the next line would
still echo "/home/arun"