Thursday, January 27, 2011

Mastering Vi Editor in a simple way


Following tutorial will guide you through mastering Vi Editor in the simplest possible way -

1. Opening a file in Vi Editor -
vi <file name>
vi -r <file name> [useful if the file was not saved earlier because of sudden connection failure]

2. Quit and save a file in vi editor -
:q [wuit without saving]
:wq or :x [quit after saving the changes]
        :w <file name> [to save a file with new name]

3. Traversing a file in vi -
k or <up arrow> [to go up]
j or <down arrow> [to go down]
h or <left arrow> [to go left]
l or <right arrow> [to go right]

4. editing in vi editor -
i [replace charactor after cursor untill esc is pressed]
a [replace charactor before cursor untill esc is pressed]
r [replace the charactor where cursor is right now]
R [replace the chars starting the cursor position, untill esc is presses]
x [delete the charactor where cursor is right now]
dd [delete entire current line]
ndd [n is a number, delete n following lines in cluding current]
D [delete the line after cursor]
o [place a new line after current line to enter text untill esc is pressed]
O [place a new line before current line to enter text untill esc is pressed]
C [change the char in current line untill esc is pressed]
cc [ehcnge entire line]
yy [copy current line into buffer]
p [paste the text from buffer]
u [undo, but effective till one last change only]

5. Search and Replace in vi editor -
/String [search String forward]
?String [search String backward]
:%s/term1/term2/g [replace term1 with term2 in entire file]

Hope this will help you with vi editor.

Script to find file without a specific String


Sometimes we land in a situation where we need to find all files in the system which does not contain a specific String.
I wrote a simple script to find a file without the string.

As a demonstration I used the file type as xml. You can customize it as per your requirement.
-----------------------------------------------------------
#!/bin/bash
typeset -i mRep
for mFile in `find . -name "*.xml"`
do
  mRep=`egrep -c 'string' $mFile`
  if [ $mRep -eq 0 ]; then
        echo "File = "$mFile" does not contain 'string'"
  fi
done
-----------------------------------------------------------
Feel free to write back if you have issues in implementing the same.

Interesting Unix Commands -1


Here are a couple of interesting Unix commands. That Makes the life easier for a Unix / Linux Administrator.
I will keep posting on this series.

1. List Down all the users in the system
cat /etc/passwd | sed -e 's/:.*$//' (Prints usernames)
cat /etc/passwd | cut -d : -f1,5 (prints usernames and description of the user)

2. Command to see all the processes with the child processes.
ptree -a

3. Following command is very interesting. This command will tell the entire history of the user. If the session is connected, when the session started (Login details), mail details, further meeting plans and all.
   This command is very sensative in nature as this gives the entire data of all the users logged in, Please use this command wisely.
cat /etc/passwd | cut -d : -f1 | xargs finger {}\;


Tuesday, January 18, 2011

Command to display tree structure in Solaris

Following combination of fond and sed with a rich use of regex will display tree structure of directories in solaris machines.

find . -type d -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'