Tuesday, July 16, 2013

Splitting string using cut command in Linux

cut command can be used to split the string with specific split character

command:

echo "18:30:00"|cut -d ':' -f1
18

echo "18:30:00"|cut -d ':' -f2

30

echo "18:30:00"|cut -d ':' -f3

00

Sunday, July 14, 2013

split string in bash

here is example using sed command.

echo "my name is;chotelal" | sed -e 's/;/\n/g'
myname is
chotelal


File test option in shell

These are the options to be used in conditional statement to check file attributes.

-e
file exists

example:
      
      if [ -b "$filename" ]
      then
        echo "$filename exist."
      fi

-a
file exists
This is same in effect to –e, but now it is discouraged to use.
-f
file is a regular file (not a directory or device file)
-s
file is not zero size
-d
file is a directory
-b
file is a “block device”
-c
file is a “character device”
-p
file is a “pipe”
-h
file is a “symbolic link”

-L
file is a symbolic link
-S
file is a “socket”
-r
file has read permission (for the user running the test)
-w
file has write permission (for the user running the test)
-x
file has execute permission (for the user running the test)

-k
sticky bit set
Commonly known as the sticky bit, the save-text-mode flag is a special type of file permission. If a file has this flag set, that file will be kept in cache memory, for quicker access.  
-O
you are owner of file
-G
group-id of file same as yours
-N
file modified since it was last read
f1 -nt f2
file f1 is newer than f2
f1 -ot f2
file f1 is older than f2
f1 -ef f2
files f1 and f2 are hard links to the same file
!
"not" -- reverses the sense of the tests above (returns true if condition absent).


how to find files with specific date range

There seems to be no direct option in find command to specify the date and get the files with specified modified date, but find command provide -newer option which will find files newer than file specified. so, here is small tweak which worked for me.

Variable:
Sdate=07/02/2013
Stime=19:00:00
Edate=07/05/2013
Etime=18:00:00
WRKDIR=/data/log
TEMPDIR= /home/dba/temp

1. create a file with modified date as start date.

> touch -d "$Sdate $Stime" $TEMPDIR/start

2. create a file with modified date as end date

> touch -d "$Edate $Etime" $TEMPDIR/end

3. find command

> find $JIRDIR -newer $TEMPDIR/start -and -not -newer  $TEMPDIR/end >$TEMPDIR/tmp_jir.txt

"tmp_jir.txt" file contains file names between the specified date range.


Saturday, June 29, 2013

Script command in linux

Script command is very useful way to record logs of any command fired in shell, it logs anything printed on screen.

Example:
--------

script myfile.txt
Will log all results into the file myfile.txt. This will open a sub-shell and records all information through this session. The script ends when the forked shell exits (user types exit) or when CTRL-D is typed.

Killing child process in Linux

I had requirement to stop one of your stats collection process at 7 AM, the stats collection was running from one .sh file which was running a .sql file to collect stats.so, to kill entire process i had to find child process (here child process is .sql file).

I searched through the internet and found various tricks to do it, but most of them seems to be not supporting in my bash version. it tried it myself ,here is the code which is working for me.


export xpid2=`ps -ef|grep -v grep|grep collect_daily_stats2|awk '{print $2}'`
echo "parent:"$xpid2
export cpid2=`ps -ef|grep -v grep|grep "$xpid2"|awk '{print $2}'|sed -s '1d'`
echo "child:"$cpid2
kill -9 "$cpid2"
echo "daily stats2 stopped"
 mailx -s"Daily stats2 stopped successfully" teradatamanager@cvscaremark.com  < /dev/null
else


any suggestions are welcome.

Thursday, May 23, 2013

find string in file using find command


Here is the syntax to find a word in a file.

find . -type f -exec grep -l "word" {} +