= Filter unique values from third column, separated by space  =
{{{#!highlight bash
cat datax.log |  awk -F ' ' '/IMEI/{print $3}' | sort | uniq | wc -l
}}}

= Filter unique values from third column, separated by :  =
{{{#!highlight bash
cat datax.log |  awk -F ':' '/IMEI/{print $3}' | sort | uniq | wc -l
}}}


= Search for text in files recursively  =
{{{#!highlight bash
cd startFolder
grep -H "textToSearch" * -R
}}}

= Copy files to other destination (Win32) =
{{{
@echo off
REM Mount drive
echo Mounting remote drive
net use z: \\192.168.1.1\backs password /USER:userx

REM copy current day files
echo Copying current day files
forfiles -p "I:" -s -m *FULL*.sqb -d +0 -c "cmd /c copy @path Z:\BACKUPS\@file"

echo Deleting files older than 7 days
REM delete files older than 7 days
forfiles -p "Z:\BACKUPS" -m *.sqb -d 7 -c "cmd /c del @path"
}}}

= Gzip files that where last modified in 30 or more days (Win32) =
{{{
forfiles /p "c:\logs" /d -30 /m *.log /c "cmd /c gzip @path "
}}}

= Run task every day at 01:00 (Win32) =
{{{
at 01:00 /every:M,T,W,Th,F,S,Su c:\task.bat
}}}

= Block responses to pings =
{{{#!highlight bash
#block responses to pings
iptables -A INPUT -p icmp -m icmp --icmp-type echo-request -j DROP
iptables -L
#restore responses to pings
iptables -F
iptables -L
}}}

= Zip SVN modified Java files =
{{{#!highlight bash
#!/bin/sh
# chmod 755 /usr/local/bin/modifiedJavaSvn.sh # on CygWin
svn status | awk '/.java/{ print( gensub( /\\/,"/","g",$2) ); }' | xargs -i zip modifiedJavaFiles.zip {}
unzip -v modifiedJavaFiles.zip
}}}

== Deploy EAR script ==
{{{#!highlight bash
#!/usr/bin/bash
# deploy EAR in remote JBoss . 
# Must have pub SSH keys (authorized_keys) deployed on remote server
# Can be used with cygwin
FILEX=sample.ear
DEST_DIR=/opt/jboss/server/default/deploy/
ECLIPSE_PROJECT=/home/userx/workspace/sample-ear/

deploy()
{
  SERVER=$1
  USER=$2
  echo "Deploying in $SERVER ..."

  cd $PROJECT
  scp target/$FILEX $USER@$SERVER:/tmp
  ssh $USER@$SERVER -C "mv /tmp/$FILEX $DEST_DIR/$FILEX"
  ssh $USER@$SERVER -C "ls -lah $DEST_DIR"
  ssh $USER@$SERVER -C "chmod 666  $DEST_DIR/$FILEX"
  ssh $USER@$SERVER -C "/sbin/service jboss abort"
  ssh $USER@$SERVER -C "sudo /sbin/service jboss start"
}

deploy "example-server" "userx"
}}}

== List JAR contents to file ==
 * cd ~/.m2/repository
 * find . -name "*.jar" | xargs -i jar -tf {} > /tmp/explodedJars.txt
 * find . -name "*.jar" | xargs -i sh -c "echo {} ; jar -tf {}" > /tmp/explodedJars.txt

== Size of each directory inside current directory ==
 * find . -maxdepth 1 -type d | xargs -i du {} -hs

== Backup folders in HOME ==
{{{#!highlight bash
#!/bin/bash
# cd ~
# ln -s scripts/backup.sh backup.sh
# sh backup.sh
DATE=`date '+%Y%m%d%H%M%S'`
FILENAME=~/backups/backup_$DATE.tar.gz
cd ~
mkdir -p ~/backups
rm $FILENAME
tar cvzf $FILENAME Documents GNUstep ThunderbirdMail Desktop .Skype scripts moin-1.9.7
# tar tvzf backup_20150108103242.tar.gz
}}}

== Get installed updates on Windows ==
{{{
wmic qfe list brief /format:texttablewsys > updates.txt
}}}

== Find files modified less than an hour ago ==
{{{
find . -mtime -1h -name "*.cfg"
}}}

== Lock file to allow only one instance of a script to run ==
{{{#!highlight bash
#!/bin/sh
LOCK_FILE=$(echo "/var/tmp/lockTest.lck")
PID=$$
LOCKED=0
COUNT=0
echo "Trying to get lock to $PID in $LOCK_FILE"

while [ $LOCKED -eq "0" ] && [ $COUNT -le "3" ]; do
    if ( set -o noclobber; echo "$PID" > "$LOCK_FILE" ) 2> /dev/null; then
        LOCKED=1
    else        
        sleep 1
        COUNT=$(($COUNT+1))
        echo "Trying to get lock to $PID in $LOCK_FILE, retry $COUNT"
    fi    
done

if [ $LOCKED -eq "0" ]; then
    echo "Unable to get lock to $PID with $COUNT retries"
    exit 1
else
    echo "Lock acquired to $PID"    
fi   
#####################################
sleep 10
#####################################
rm -f "$LOCK_FILE"
LOCKED=0
echo "Released lock to $PID"
exit 0
}}}

== awk , load file into array ==
 * https://magvar.wordpress.com/2011/05/18/awking-it-how-to-load-a-file-into-an-array-in-awk/

{{{#!highlight sh
#! /bin/bash
INPUTFILE="inputfile.csv"
DATAFILE="data.csv"
OUTFILE="output.csv"

awk 'BEGIN {
while (getline < "'"$INPUTFILE"'")
{
split($0,ft,",");
id=ft[1];
name=ft[2];

key=id;
data=name;
nameArr[key]=data;
}
close("'"$INPUTFILE"'");

while (getline < "'"$DATAFILE"'")
{
split($0,ft,",");
id=ft[1]; # Id is the first column
phone=ft[2]; # Phonenumber is the second
name=nameArr[id]; # Get the name from the nameArr, using "id" as key
print id","name","phone > "'"$OUTFILE"'"; # Print directly to the outputfile
}
}'

}}}

== Shell script sample ==
{{{#!highlight sh
#!/bin/sh
function replaceString {
  RES=$(echo $1 | sed "s/$2/$3/g")
  echo $RES
}

function add {
  echo $( echo "$1 + $2" | bc )    
}

function inc {
  echo $( expr $1 + 1 )  
}
####################################
echo $(add "1.5" "3.2" )

TEXT1="AAA BBB CCC"

for WORD in $TEXT1
do
  echo ${WORD}
done

WITHOUTSPACES=$(replaceString "AAA BBB XXX" " " "" )
echo $WITHOUTSPACES

if [ $WITHOUTSPACES == "AAABBBXXX" ]
then
  echo "Equal"
fi

if [ $WITHOUTSPACES != "AAA BBB XXX" ]
then
  echo "Diff"
fi

A=$(ls / | wc -l)
A=$(expr $A + 1)
if [ $A -eq 22 ]
then
  echo $A
fi

B="222"
B=$(expr $B)
if [ $B -eq 222 ]
then
  echo $B
fi

COUNT="0"
while [ $COUNT -lt 10 ]
do
  COUNT=$( inc $COUNT )
done

$(test 0 -ge 0)
RES=$(echo $?)
echo $RES
}}}

== Change TIME_WAIT (time wait) tcp/ip ==
Linux
{{{
echo 1 > /proc/sys/net/ipv4/tcp_fin_timeout
echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle
echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse
}}}

Freebsd
 * sysctl net.inet.tcp.msl=1500

== Show root folders occupied space ==
{{{#!highlight bash
cd /tmp
ls --color=never | xargs -i du {} -hs
find . -type d -maxdepth 1 | xargs -i du {} -hs

#~/.bashrc
alias folderUsedSpace='find . -type d -maxdepth 1 | xargs -i du {} -hs'
}}}

== Add swapfile 512MB ==
{{{#!highlight sh
df -h
dd if=/dev/zero of=/swapfile1 bs=1024 count=524288
chown root:root /swapfile1 
chmod 0600 /swapfile1 
mkswap /swapfile1 
swapon /swapfile1
free -m
vi /etc/fstab
# /swapfile1 none swap sw 0 0
free -m
htop
}}}

== Alias current date ==
{{{#!highlight sh
alias currdate="date '+%Y%m%dT%H%M%S'"
}}}