Unix Command Reference

AWK | FIND | LS | SED | CAT | TOUCH | GREP | TAIL | HEAD | CHMOD | CHOWN | TAR | GZIP | GUNZIP | TEE | XARGS | TOP | CP | MV | ECHO | SORT | SAR | KILL |
Common regular expression operators
===========================================================================
OperatorPurpose
. (period)Match any single character.
^ (caret)Match the empty string that occurs at the beginning of a line or string.
$ (dollar sign)Match the empty string that occurs at the end of a line.
AMatch an uppercase letter A.
aMatch a lowercase a.
\dMatch any single digit.
\DMatch any single non-digit character.
\wMatch any single alphanumeric character; a synonym is [:alnum:].
[A-E]Match any of uppercase A, B, C, D, or E.
[^A-E]Match any character except uppercase A, B, C, D, or E.
X?Match no or one occurrence of the capital letter X.
X*Match zero or more capital Xs.
X+Match one or more capital Xs.
X{n}Match exactly n capital Xs.
X{n,m}Match at least n and no more than m capital Xs. If you omit m, the expression tries to match at least n Xs.
(abc|def)+Match a sequence of at least one abc and def; abc and def would match.
===========================================================================
#!/bin/sh
INPUT_STRING=hello
while [ "$INPUT_STRING" != "q" ]
do
echo "Please type something in (q to quit)"
read INPUT_STRING
echo "You typed: $INPUT_STRING"
done

for file in $*
do
# do something on $file
[ -f "$file" ] && cat "$file"
done
Regular Expression
For loop
While loop
Case statement
Finding OS Version Finding RAM Size
Finding No of CPU's/No of Core's
Finding orphaned processes

Install flash plugin firefox - RHEL/Fedora

posted May 22, 2013, 9:07 AM by Sachchida Ojha   [ updated May 22, 2013, 9:11 AM ]


32bits su

rpm -ivh http://linuxdownload.adobe.com/adobe-release/adobe-release-i386-1.0-1.noarch.rpm

rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-adobe-linux

yum -y install flash-plugin


64bits su

rpm -ivh http://linuxdownload.adobe.com/adobe-release/adobe-release-x86_64-1.0-1.noarch.rpm

rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-adobe-linux

yum -y install flash-plugin

Installing Open Office Suite in RHEL 6

posted Apr 22, 2013, 1:55 PM by Sachchida Ojha

Download open office using link

http://www.openoffice.org/download/


After your download completes, Open Terminal and Execute the following…

# tar xzf OOo_3.3.0_Linux_x86_install-rpm-wJRE_en-US.tar.gz

Now Change your directory into OOo_3.3.0_Linux_x86_install-rpm-wJRE_en-US (the folder which you extracted)
On Same Directory You will find Folder call RPMs, now move to that folder using follwoing command.

#cd RPMs

here We go,need to Install All RPMS to Install OpenOffice 3.2. Execute Following Command

# rpm -ivh *.rpm

If everything goes fine, it means OpenOffice gets installed. But By performing above steps OpenOffice got installed, but you’ll not able to see OpenOffice Selection in Applications Menu. For this we have to Perform another few Steps.

In RPMs directory there is one more directory called “desktop-integrates“. Now Lets Switch to that Directory and install Red Hat Related Desktop Integration RPM.

# rpm -ivh openoffice-desktop-redhat-xxx.rpm

Once Desktop Integration RPM get installed You’ll able to See Open Office Selection in Application Menu.

Pattern Matching Operators

posted May 2, 2012, 7:05 PM by Sachchida Ojha   [ updated May 22, 2013, 9:08 AM ]



PATTERN-MATCHING OPERATORS

Operator Function
${var#pattern} Deletes the shortest match of pattern from the front of var and returns the rest.

${var##pattern} Deletes the longest match of pattern from the front of var and returns the rest.

${var%pattern} Deletes the shortest match of pattern from the end of var and returns the rest.

${var%%pattern} Deletes the longest match of pattern from the end of var and returns the rest.

${var/pattern/string} Replaces the longest match of pattern in var with string. Replaces only the first match. This operator is only available in bash 2.0 or greater.

${var//pattern/string} Replaces the longest match of pattern in var with string. Replaces all matches. This operator is only available in bash 2.0 or greater.


The canonical usage of bash’s pattern-matching operators is manipulating file and path names. For example, suppose you have a shell variable named myfile that has the value /usr/src/linux/Documentation/ide.txt (which is the documentation for the kernel’s IDE disk driver). Using “/*” and “*/” as the pattern, you can emulate the behaviour of the dirname and basename commands.


1 #!/bin/bash
2 # Listing 34.3
3 # pattern.sh - Demonstrate pattern matching operators
4 #####################################################
5
6 myfile=/usr/src/linux/Documentation/ide.txt
7
8 echo ‘${myfile##*/}=’ ${myfile##*/}
9 echo ‘basename $myfile =’ $(basename $myfile)
10
11 echo ‘${myfile%/*}=’ ${myfile%/*}
12 echo ‘dirname $myfile =’ $(dirname $myfile)


Line 8 deletes the longest string matching “*/” in the filename, starting from the beginning of the variable, which deletes everything through the final “/”, returning just the filename. Line 11 matches anything after “/”, starting from the end of the variable, which strips off just the filename and returns the path to the file. The output of this script is:

$ ./pattern.sh
${myfile##*/} = ide.txt
basename $myfile = ide.txt
${myfile%/*} = /usr/src/linux/Documentation
dirname $myfile = /usr/src/linux/Documentation

To illustrate the pattern-matching and replacement operators, the following command replaces each colon in the $PATH environment variable with a new line, resulting in a very easy to read path display (this example will fail if you do not have bash 2.0 or newer):

$ echo -e ${PATH//:/\\n}
/usr/local/bin
/bin
/usr/bin
/usr/X11R6/bin
/home/kwall/bin
/home/wall/wp/wpbin

Of course, your path statement will look somewhat different. The -e argument to echo tells it to interpret the \n as a new line rather than a literal string. Perversely, however, you have to escape the escape (\\n) to get the new lines into the variable in order for echo to interpret them.


How to print specific lines from large text files.

posted May 2, 2012, 4:15 PM by Sachchida Ojha   [ updated May 22, 2013, 9:10 AM ]

I have been asked to dump lines 50001 to 50100 from a text file size 20GB having 50 million lines. What are the various option?

1. sed -e '1,N d; M q' ---> This command will print the line from N+1 to M.

sed -e '1,50000 d; 50100 q'

2. sed -n '50000,50100p' file_name

3. awk 'FNR>50000 && FNR<=50100' file





How to debug a shell script in UNIX

posted Apr 29, 2012, 8:07 AM by Sachchida Ojha   [ updated May 22, 2013, 9:11 AM ]



Using -x option to debug a shell script

Run a shell script with -x option.
$ bash -x script-name
$ bash -x dbaref.sh

Using SET
set -x : Display commands and their arguments as they are executed.
set -v : Display shell input lines as they are read.


Display RAM size in UNIX

posted Apr 29, 2012, 7:54 AM by Sachchida Ojha   [ updated May 22, 2013, 9:12 AM ]



DEC Unix

$uerf -r 300 |grep -i mem

HP-UX

Use sar  utility

Solaris

$lsdev -C|grep mem

$lsattr -E1 mem0

AIX

$svmon




Display no of CPU's in your UNIX machine

posted Apr 29, 2012, 7:43 AM by Sachchida Ojha   [ updated May 22, 2013, 9:13 AM ]



AIX
$lsdev -C|grep Process|wc -l

Solaris

$psrinfo -v|grep "Status of "|wc -l



Useful find commands

posted Apr 29, 2012, 7:29 AM by Sachchida Ojha   [ updated May 22, 2013, 9:14 AM ]


1. Locate files that contains certain strings

$ find . -print|xargs grep -i v\$session (find files containing v$session)

2. Find recently created files

$find . -mtime -1 -print ( shows files created during the past day)

3. Find large files on server

$find . -size  +10000 -print (file size greater than equal to 10,000 bytes)

4. Delete file in bulks


$find . -mtime +7 -exec rm {} \; (delete all files older than 7 days).

5. allocate an empty files

$touch new.txt

6. Find all files owned by a specific user

$find /  -user sachi

7. Find all directories named 'conf'

$find / -type d -name conf

8. Find all files larger than 500k


$find -name '*' -size +500k


Mapping a resource-intensive process to a database process

posted Apr 24, 2012, 6:55 AM by Sachchida Ojha   [ updated May 22, 2013, 9:13 AM ]

This is most common in DBA's troubleshooting task to identify an OS intensive process in the BOX and then MAP OS process back to a database process. If the database process is a SQL process then display user of the SQL statement and also the SQL.


1. Find the top consuming queries with the ps command.


$ps -e -o pcpu,pid,user,tty,args|grep -i oracle|sort -n -k 1 -r|head

2. you can now determine what type of Oracle process.

SELECT
'USERNAME :'||s.username ||chr(10) ||
'SCHEMA :'||s.schemaname ||chr(10) ||
'OSUSER :'||s.osuser ||chr(10) ||
'PROGRAM :'||s.program ||chr(10) ||
'SPID :'||p.spid ||chr(10) ||
'SID :'||s.sid ||chr(10) ||
'SERIAL# :'||s.serial# ||chr(10) ||
'KILL STRING: '||'''' ||s.sid||','||s.serial#||''''||chr(10) ||
'MACHINE :'||s.machine ||chr(10) ||
'TYPE :'||s.type ||chr(10) ||
'TERMINAL :'||s.terminal ||chr(10) ||
'SQL ID :'||q.sql_id ||chr(10) ||
'SQL TEXT :'||q.sql_text ||chr(10)
from v$session s, v$process p,v$sql q
where s.paddr=p.addr
and p.spid='&&PID_FROM_OS'
and s.sql_id = q.sql_id(+);


3. If you want to investigate further about the SQL statement consuming OS resources then

SQL> SELECT * from table(DBMS_XPLAN.DISPLAY_CURSOR(('&&sql_id')));

You can also use ORADEBUG utility to display top consuming SQL Statement if you know the OS ID.



Monitor disk space for a given set of mount points

posted Apr 23, 2012, 5:10 PM by Sachchida Ojha



#!/bin/bash
mntlist="/home /tmp /"
for ml in $mntlist
do
echo $ml
usedSpc=$(df -h $ml |awk '{print $5}' | grep -v capacity | cut -d "%" -f1 -)
BOX=$(uname -a |awk '{print $2}')
#
case $usedSpc in
[0-9])
arcStat="Relax, lots of diskspace: $usedSpc"
;;
[1-7][0-9])
arcStat="Disk space OK: $usedSpc"
;;
[8][0-9])
arcStat="space getting low: $usedSpc"
;;
[9][0-9])
arcStat="warning, running out of space: $usedSpc"
echo $arcStat $ml | mailx -s "space on: $BOX" snojha@gmail.com
;;
[1][0][0])
arcStat="update resume, no space left: $usedSpc"
echo $arcStat $ml | mailx -s "space on: $BOX" snojha@gmail.com
;;
*)
arcStat="huh?: $usedSpc"
esac
#
BOX=$(uname -a | awk '{print $2}')
echo $arcStat
#
done
#
exit 0



1-10 of 11