Pages

Showing posts with label SCRIPTING. Show all posts
Showing posts with label SCRIPTING. Show all posts

Saturday, January 11, 2014

Customizing Your SHELL Prompt in Unix with TIME



How to display time in the bash promt ?



# declare -x PS1=" \@ Bash $ "


 05:05 PM Bash $

Setting Permission while creating directories

How to set permission while creating creating directory in linux ?

# mkdir --parents --mode=550 vasanth/backup/january

--mode=m ( -m )—Sets the permission mode (as in chmod )
--parents ( -p )—Makes all necessary directories even if they don’t currently exist



How to classify files and directories in "ls" command ?


How to classify in ls command ?


ls command can classify directories,files,programs,symbolic links and named pipes.



# ls --color=never --classify


The –classify symbols are directories ( / ), programs ( * ), symbolic links ( @ ), pipes ( | ),and Unix domain socket files ( = ).






OR & AND conditions in shell scripts

OR Statement in shell scripts

|| is known as OR statement in shell script.

Eg:


# id user1 || useradd user1

In the above example if the user1 exist in the system the second command will not be executed. If the user1 does not exist second command will be executed.

If first command returns true second will not exist run. if the command is false second will be executed.

AND Statement in shell scripts


 Eg:

&& is known as AND in shell script


#  [ -d /tmp/test ] && rm -rf /tmp/test

 If the first command is true the second command will be executed. If the first
false second command second will not executed.


Saturday, December 21, 2013

ldapsearch using shell script

Shell script to serach data in LDAP

Below shell script can be used to search data in ldap. Modify the script accordingly for your needs.
 Have fun !



#!/bin/bash
case "$1" in
# Searching LDAP name from full name 
ln)
LN=
# Group search
gs)
GID=`ldapsearch -x -b "dc=test,dc=com" "(cn=$2)" | sed -n -e '/^gid/p' | cut -d: -f2`;
if [  "$GID" = "" ]; then
echo " The "$2" LDAP group does not  exist"
else
echo "GID OF the $2 group is $GID"
MEM=`ldapsearch -x -b "dc=test,dc=com" "(cn=$2)" | sed -n -e '/^member/p' | cut -d: -f2`;
if [ "$MEM"  = "" ]; then
echo " No users belong to this group "
else
echo "Following are the members of the $2 LDAP GROUP"
echo "$MEM"
fi
fi
;;
# User Search
us)
USER1=`ldapsearch -x -b "dc=test,dc=com" "(uid=$2)" | sed -n -e '/^dn/p' | cut -d: -f2`;
if [ "$USER1" = "" ]; then
echo "User does not exist"
else
echo $USER1
MEM1=`ldapsearch -x -b "dc=test,dc=com" "(memberUid=$2)" | sed -n -e '/^dn/p' | cut -d: -f2`;
echo "$MEM1"
fi
;;
*)
echo "lquery.sh  "
echo "lquery.sh  "
;;
esac

Thursday, July 18, 2013

Using find in Linux


1. To find out all the Mp3 files in the system and to move /home/songs

# find / -name '*.mp3' -print -exec mv '{}' ~/songs \;

2. To print out all the Mp3 file names with odd charecters ( Doesn't matter whether upper or lower case)

# find . -name '*.mp3' -print0 | xargs -i -0 cp '{}' /back

3

Wednesday, August 31, 2011

Find useful useful unix command

1. To find all the .conf file in / and copy it to /backup

# find . -name '*.conf' -print -exec cp '{}' ~/backup \;

No need to explain the options above except -print and exec.

-print - It is allways true and has a side effect of printing.

{} - This will replace the name of the file found.

\; - Means end of the line

---------------------------------------------------------------------------------------------------------

2. To find all the .txt files with odd characters( Contain upper and lowe case and numbers)

# find . -name '*.txt' -print0 | xargs -i -0 mv '{}' ~/backup

-print tell find to use null character insted of white space.

----------------------------------------------------------------------------------------------------------
3. To find all the files across the symbolic links

# find . -follow -name '*.txt' -print0 | xargs -i -0 mv '{}' ~/backup

-follow - This option help to find out the orgination of the symbolic links.

-----------------------------------------------------------------------------------------------------------

4. To find out all the.txt files case insensitively

# find . -follow -iname '*.txt' -print0 | xargs -i -0 mv '{}' ~/backup


------------------------------------------------------------------------------------------------------------

5. To find out file modified more than +90 days

# find . -name '*.txt' -mtime +90 -print


-mtime - Takes argument to specify the time frame.

--------------------------------------------------------------------------------------------------------------
6. To print out the files modified more than 7 days and less than 14 days


# find /home -mtime +7 -a -mtime -14 -print

---------------------------------------------------------------------------------------------------------------
7. To find the files with java extension

# find . -name '*java*' -print

----------------------------------------------------------------------------------------------------------------
8. To find the java files in all the directories in /

# find / -type d -name '*java*' -print

----------------------------------------------------------------------------------------------------------------
9. To find out all the block device files in /dev

# find /dev -type b -name '*' -print

----------------------------------------------------------------------------------------------------------------
10. To find out charecter special file in /dev

# find /dev -type c -name '*' -print

----------------------------------------------------------------------------------------------------------------
11. To find out all the directories in /

# find / -tyde d -name 'dev' -print

----------------------------------------------------------------------------------------------------------------
12. To find out the all the named pipes in dev directory

# find /dev -type p -name '*' -print

----------------------------------------------------------------------------------------------------------------
13. to find out all the symbolc link in /

# find / -type l -name '*' -print

----------------------------------------------------------------------------------------------------------------
14. To find all the files above 3MB

# find / +3000K -print

----------------------------------------------------------------------------------------------------------------
15. Finding Files By content

# grep -i vasanth /etc/passwd
(This can be used only when we have the vicinity of the file.)

----------------------------------------------------------------------------------------------------------------
16. To find the word vasanth from files inside /etc/

# find /etc -name '*' -exec grep -Hi vasanth '{}' \;

( Use of exec command:- When predicates are true upto that point it will execute the grep command for all the files.

'{}' is where the filename is put when executing the command
The \; indicates the end of the command
-H print if grep command find soomething

----------------------------------------------------------------------------------------------------------------



----------------------------------------------------------------------------------------------------------------
17. To find


Sunday, August 28, 2011

SIMPLE SHELL SCRIPT TO BACKUP WHOLE MYSQL DATABASES AND KEEP ONLY TWO LATEST COPIES

#! /bin/bash
# Written by Vasanth.T.M, L2-Systems Engineer(*nix), Perfomix, Inc.


CKUPDATE=$(date +%d-%m-%Y)
BACKUPDIR=/mysqlbackup
DATABASES=$(mysql -u root -h localhost -pmysql -Bse 'show databases')


delete_old ()
{
echo Deleting old backup of backup of "$name"
name="$1"
find "$BACKUPDIR" -name "$name-*.sql.bz2" | sort | head -n -2 | xargs --no-run-if-empty rm -f
}

back ()
{
for GH in $DATABASES; do
echo "Creating mysql backup of $GH"
mysqldump -u root -pmysql $GH | bzip2 --compress --stdout > $BACKUPDIR/$GH-$CKUPDATE.sql.bz2
name=`basename $GH`
delete_old "$name"
done
}
back

Sunday, June 26, 2011

Pring Number of Files Inside a Directory

1 .for i in `find -maxdepth 1 -type d`; do echo -n $i " ";find $i|wc -l; done


2. To list the Files inside /home directory.

# for i in `find /home/ -maxdepth 1 -type d`; do echo -n $i " ";find $i|wc -l; done

3.

#!/bin/bash
for i in `find $1 -maxdepth 1 -type d`; do
echo -n $i " ";
find $i|wc -l;
done

4. ls -lR | grep -B 1 -e “^total “

Wednesday, September 22, 2010

Shell script to start Authdeamon

#! /bin/sh

case "$1" in
start)
echo "Initializing authdaemon. (Courier)"
/usr/local/sbin/authdaemond start
;;
stop)
echo "Shutting down authdaemon:"
/usr/local/sbin/authdaemond stop
;;
restart|reload)
$0 stop && $0 start
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0


~

Wednesday, September 15, 2010

SQUID monitoring

tail -f /var/log/squid/access.log | awk '{print$3 " " $8 " " $7}' // Use this script to monitor the We usage in ur home network

Thursday, August 26, 2010

Find Usefull Commands

# find /path/to/base/dir -type f -exec chmod 755 {} \;

# find public_html/ -type f -exec chmod 644 {} \;

Find Usefull Commands

# find /path/to/base/dir -type f -exec chmod 755 {} \;

# find public_html/ -type f -exec chmod 644 {} \;