Saturday, March 28, 2009

UNIX COMMANDS

 Determine where a command is

The command "type" may be used to find out,
which command the shell executes.

Example:

$ type ls
/usr/bin/ls
$ type l
l is an alias for 'ls -l'
***************************************************************************************
Suspending and resuming a command

If you did i.e. a "telnet" to another host, but want to do
a "ls -l" on the current host, you do not have to close the
"telnet" session.

Just type "^Z" (control-z) to suspend the current telnet session.
The command is then waiting for you to resume. In this time
you get a shell prompt and can issue any command.

If you are ready to continue, you can resume the "telnet" session
with the command "fg %1" ("foreground: the first command waiting").

If you want to run the command in the background, you could
issue a "bg %1" ("background: first command waiting"), too.

The command "jobs" lists all commands waiting.
**************************************************************************************
Using arrow keys in kornshell

Use this tip to enable your arrow keys.
Caveat: You will have to use the emacs bindings

Enjoy!

#!/bin/ksh
# file: enable-arrow
# source in current environment with
# command:
#       . enable-arrows
#

set -o emacs

# Note: these are the actual control
# characters. In vi, type i ctrl-v
# then ctrl-P (if u want a ctrl-p)
alias _A=^P
alias _B=^N
alias _D=^B
alias _C=^F

alias __A=^P
alias __B=^N
alias __D=^B
alias __C=^F
*************************************************************************************
Print enviroment variables in an easy to read format

Replace a normal environment
variable output as in $PATH:

/usr/bin:/usr/ccs/bin:/usr/local/bin:

with:

/usr/bin
/usr/ccs/bin
/usr/local/bin

Use this SIMPLE script:

#! /usr/bin/ksh
IFS=':'
for ii in $PATH
do
echo $ii
done
# end of file
***************************************************************************************
Include the current directory within your prompt

To include the current directory within the prompt, include
the following line into your $HOME/.kshrc (KSH) or $HOME/.bashrc
(BASH):


PS1='$PWD $'
export PS1
***************************************************************************************
Running a script without changing file access permissions


Usually a user has to change the file
permissions for a shell script to
make it executable, e.g.

chmod +x script

Sometimes it's more convenient
to just invoke the shell with
the script as argument:

sh script
****************************************************************************************
Welcome the user as they login to the system

tt=`date +"%T" | cut -c1-2`
NAME=`grep "^$LOGNAME" /etc/passwd | awk -F: ' {print $5}'`
echo "\n\n\n"
tput smso
if [ $tt -gt 0 -a $tt -lt 12 ]
then
echo " $NAME !!!!!!    GOOD MORNING !!!!!!"
elif [ $tt -gt 12 -a $tt -le 16 ]
then
echo " $NAME !!!!!! GOOD AFTERNOON !!!!!!"
else
echo " $NAME !!!!!!  GOOD EVENING !!!!!!"
fi
tput rmso

**************************************************************************************
Displaying only the subdirectories from the current directory

some time U don't remember the name of the subdirectory U want to go to.
and in the jungle of files and directories it is easier if you could see
only the directories.

put the following function in your env file

dir()
{
ls -l |awk '{ if ( substr($1,1,1)== "d") {print}}'
}
**************************************************************************************
Displaying only the subdirectories from the current directory (2)



Here is an easier command to list sub-directories alone in directories

ls -l | grep ^d

or

ls -d */

************************************************************************************
Bookmark and browse directories


Enter the following two functions in your .profile or .kshrc (if your
.profile calls it).

function mark {
Usage="Usage: mark word"
case $# in
1) export "$1=cd `pwd`" ;;
*) echo "Incorrect Arguments count "
echo $Usage ;;
esac
}

function goto {
Usage="Usage: goto word"
case $# in
1)  if env | grep "^$1=cd " > /dev/null ; then
eval $"$1"
#echo "New current directory is `pwd`"
else
echo $Usage
fi ;;
*)  echo "Incorrect Argument count"
echo  $Usage  ;;
esac
}

If you are in a particular directory, you just have bookmark the directory
like "mark ", eg "mark tst", you can go to any number of directories
randomly and at any point of time if you want to return back to your test
directory just say "goto tst".

I normally have to move around many directories and find this bookmarking
functions very useful.

Try it. Happy directory browsing.

Note: It is valid only for the current shell.
************************************************************************************
Finding files recursively which match a certain pattern


"grep" can search a particular file or files in current directory for
matching a pattern.

If anybody wants to search files recursively from the current directory
which match a particular pattern
the following script can be used.

Put the following script in a file.


if [ $# -ne 2 ]
then
echo "Usage : $0  "
echo
exit
fi

PWD=`pwd`
for file in `find $PWD -name "$1"`
do
grep -l "$2" $file
done


For Example ,if you want to find a string called "execption" in all *.java
files recursively under the directory.

scriptfilename *.java exception

or

The following command can be used to
execute "grep" not only in the current,
but in all subdirectories:

# rgrep - recursive "grep"
[ $# -lt 1 ] && exec echo "usage: grep search [file ...]"

search=$1; shift
[ $# -lt 1 ] && set -- .

find "$@" -type f -print |
xargs grep "$search"


or

find . -name *.java -exec grep -Hn "SEARCH_STRING" {} \;

**************************************************************************************










Print this post

No comments: