Showing posts with label Unix. Show all posts
Showing posts with label Unix. Show all posts

Thursday, November 20, 2008

Helpful Unix commands - Part 2

- When shell script complains of ^M character which is a windows character
sh: /opt/tmp/testPerson.sh: /usr/bin/ksh^M: bad interpreter: No such file or directory

Reason is there may be DOS linefeeds in your file. Convert the file to Unix linefeeds, either by copying the file using ASCII mode FTP, or one of:

tr -d '\r' < script > script.new

perl -pi -e 's/\r$//;' script

dos2unix < script > script.new



-To Change permissions
chmod 775 <>
chmod -R 775 <> [for recursive]


-To Move Files from One server to another
scp username@servername:/opt/tmp/filename filename


To connect to B server from A server
From A server type
ssh username@serverB


-To check if a directory exist

if [ -d mydir ]
then
echo "Directory found"
else
echo "Directory not found"
fi


-To check if a file exists

#(-f === if file exists)
if test -f $TMP;
then
if test ! -w $TMP ;
then
printf the temp file $TMP exists and cannot be overwritten aborting >&2
exit 192
fi
fi


- To copy a file
cp -p <> <>
- To copy a directory
cp -p -R <> <>

- To remove a file
rm <>
- To remove a directory
rm -d -R <,filename>>


- To find out if perl or bash or sh file exists
which perl
which bash
which sh


- To find out the version of linux
cat /etc/redhat-release

- To change group name of a file
chgrp <> <>

Monday, June 16, 2008

Some helpful Unix commands

VI COMMANDS

> Move cursor
> h (left) j (down) k (up) l (right)
> w Move forward one WORD.
> b Move BACK one word.
> e Move to END of word.
> ^ u Scroll UP a partial screen.
> ^ d Scroll DOWN a partial screen.
> ^ f Scroll FORWARD a full screen (page down).
> ^ b Scroll BACKWARD a full screen (page up).
> - Move up one line.
> [ret] Return, move down one line.
> 0 (zero) Move to BEGINNING of line.
> $ Move to END of line.
> G Go to END of file.
> n G Go to line number n.
>
> Search
> /text [RET] SEARCH for text.
> / [RET] CONTINUE search for previous text.
> ?text Search backwards for text.
>
> vi CHANGES
> s SUBSTITUTE for character.
> S SUBSTITUTE for line.
> i text [Esc] INSERT text before cursor.
> cw text [Esc] CHANGE word to text.
> c3w text [Esc] Change 3 words to text.
> C text [Esc] Change rest of line to text.
> cc text [Esc] Change line to text.
> x DELETE single character.
> r p REPLACE single character with p.
> R text [esc] Replace (type over) characters with text.
> dw DELETE word (4dw deletes 4 words).
> dd DELETE line (3dd deletes 3 lines).
> D DELETE from cursor to end of line.
> u UNDO last change (remember this one).
> U UNDO all changes to line (don't forget this one).
> s SUBSTITUTE character.
> S SUBSTITUTE lines.
> J JOIN lines.
> a text [Esc] ADD text after cursor.
> A text [Esc] ADD text to end of line.
> I text [Esc] INSERT text at start of line.
> o text [Esc] OPEN new line below with text.
> O text [Esc]: OPEN new line above with text.
> esc END an insert, replace command.
>
> vi SUBCOMMANDS
> :w fname WRITE (save) fname (default: to present file).
> ZZ EXIT the editor, saving all changes (same as :wq).
> :q! QUIT emphatically, do not save changes (! is your tool to insist;
useful after messing up (also :w temp).
> :e! Edit again previous file, i.e., revert to last saved version.
> :n Edit NEXT file (after vi file1 file2 ...).
> :f Show current FILE name and cursor line number.
>
>
> vi COPY and PASTE
> Y [RET] YANK (copy) current line.
> yy [RET] YANK (copy) current line.
> yw YANK (copy) next word.
> p PUT last thing yanked before cursor.
> P PUT last thing yanked after cursor.
> nb: you can have many buffers with stuff in them
>


i enter input mode before the cursor
I enter input mode at the start of the current line
a enter input mode after the cursor
A enter input mode at the end of the current line
o create a new line below the cursor and enter input mode on it
O create a new line above the cursor and enter input mode on it


/ pattern search forward through the document for the next occurrence of the pattern (or string of text)
? pattern search backward through the document for the next occurrence of the pattern (or string of text)
n repeat search in the same direction
N repeat search in opposite direction
f char search forward from the cursor in the current line for a single character (char)
F char search backward from the cursor in the current line for a single character (char)
; repeat single character search in either direction (f or F)




LESS COMMANDS
>less <>
>g first line
>shift g -> G bottom
>Ctrl u page up
>Ctrl d page down
>/ search
>q quit

....more Useful commands while viewing a file with 'more' or 'less':
scroll forward one page
^b (control-b) scroll backward one page
/ search for string
n find next occurence of string
N find previous occurence of string (search for string in opposite direction)*
g go to the beginning of the file*
G go to the end of the file*
v edit the current file with 'vi'*
:n go to the next file*
:p go to the previous file*
q quit


To enable bash
bash

To clear screen
CLEAR

To find a process
ps -ef | grep SUNW
ps -ef | grep SUNW* | grep app

To kill a process
kill -9 << process>>


FIND / GREP commands
-This command will avoid the printing of files without permission
find . -name *mylog.log* 2>/dev/null
grep "Exception:"
grep -i "vxcv" (ignores case)


-unzip tar
gunzip docs-Orion.tar.gz
tar -xvf docs-Orion.tar

-unjar
jar xf jar-file

-read the timestamp of a file
ls -al ./applications/j2ee-modules/WEB-INF/classes/config/runtime.properties


- compare two directories (pg - page by page)
diff -r ./firstwar ./secondwar | pg

- to get system date and time
date

- whoami
- id


- remove dir
rmdir -f * (forces remove all direc in that location)
rmdir docs-Orion

To go back to the prevoius directory
cd -


Go to home directory
cd ~ (or)
cd


Shows the latest files in order
- ls -alrt

- To sort a app process
ps -ef | grep app |sort


To remove files recursively from a dir
cd work
rm -rf *

If file is full, to check
du -sh *


To gzip files
gzip *2008*



Cronjobs
To check cronjobs
>crontab -l

We may read the result in reverse order
5 0 * * * /opt/home/server/bin/run_nfm.sh
(time is in reverse order - min hours day month year)

To check whether a user is available or not
cat /etc/passwd