Thursday, July 16, 2015

quick file copy (without ftp/scp)

This is for quick copy only, where you don't want to setup any ftp or scp server
and have the machines in same network

## say we have two hosts: alpha & beta
##  need to copy: file-big.tgz
## linux util: nc

on host: alpha
$ cat file-big.tgz | nc -v -l 3334


now get the file on host:beta
$ nc  -v  alpha 3334   > file-from-alpha-file-big.tgz


voila, we have the file on host beta...


Tuesday, June 2, 2015

milliseconds to date

Most of the java apps use milliseconds, here is bash date for getting the date for the same


## Current system date

$ date
Tue Jun  2 08:07:57 UTC 2015

## Epoch system date
$ date +"%s"
1433232485

## Milliseconds for the date
$ date +"%s%3N"
1433232489392

## Last 3 digits are for millisecs, so put decimal point and you get the get
$ date -d @"1433232489.392"

Tue Jun  2 08:08:09 UTC 2015


## or strip of Last 3 digits

$ date -d @"1433232489"

Tue Jun  2 08:08:09 UTC 2015


Thursday, March 5, 2015

script for reading file or stream

I often wanted to pass the file to a pipeline some command, and also if the file is parameter to command it should accept


Voila!!

here is 'stream.sh'

#!/bin/bash

file=${1:--}

cat $file 


Usage and output

$ ./stream.sh underSTANDING.txt 

If you understand, say "understand".
If you don't understand, say "don't understand".
But if you understand and say "don't understand".
How do I understand that you understand? Understand!



$ cat underSTANDING.txt | ./stream.sh 

If you understand, say "understand".
If you don't understand, say "don't understand".
But if you understand and say "don't understand".
How do I understand that you understand? Understand!



$ ./stream.sh < underSTANDING.txt 

If you understand, say "understand".
If you don't understand, say "don't understand".
But if you understand and say "don't understand".
How do I understand that you understand? Understand!

bytes to gb/tb conversion

Often had to go to websites to do the bytes to KB.. TB.. GB conversions
here is the quick script for handy use

#!/bin/bash

# Give numeric value, which is in bytes
# will show all possible conversions 

call_bc() {
n1=$1
n2=$2
echo "scale=4; $n1/($n2)" |bc
}

nm=$1
pw=$2
pn=""

k_ilo=1024;
m_ega=$k_ilo*$k_ilo;
g_iga=$m_ega*$k_ilo;
t_era=$g_iga*$k_ilo;
p_eta=$t_era*$k_ilo;


[ -z "$nm" ] && exit

echo  "B: "$nm
for i in K M G T P; do
case $i in
K)pn=$k_ilo;;
M) let pn=$m_ega;;
G) let pn=$g_iga;;
T) let pn=$t_era;;
P) let pn=$p_eta;;
esac
echo -n "$i: "; call_bc $nm $pn
done

Usage and output
$ ./convert.sh 74274576
B: 74274576
K: 72533.7656
M: 70.8337
G: .0691
T: 0
P: 0