Unix Commands For DBA
Unix Commands For DBA
Unix Commands For DBA
vi EDITOR
******************************************************************************
General rules when using vi :
• You can precede most commands by a number to indicate the number of times to execute a
command. For example, 5dd erases five lines.
When you’re typing text, you are in Insert mode. Before you can issue any commands or move
the cursor, you must enter Command mode by pressing the ESC key. To leave the Command
mode and begin inserting text, you must either press i for Insert or a for Append. Insert allows
you to enter text before your cursor location, whereas
Append allows you to enter text after your cursor location.
vi filename
Cursor-Movement Commands
To move… Press…
Left h
Right l
Up k
Down j
To the end of file G
To line n nG
Entering Text
To perform this… Press…
Insert i
Append a
Editing Text
To perform this… Press…
Miscellaneous Commands
To do this… Type…
Display current line number CTRL-G
Refresh the screen CTRL-L
Read an outside file into the document :r /path_to_file/filename <ENTER>
Search /search_string <ENTER>
Global search and replace :1,$s/search_string/replacement_string/g
(The above reads, start at line 1 (:1), search for search_string, replace it with replacement_string, and do so
globally(g).)
******************************************************************************
last
last username
"pwd" command
root> pwd
/u01/app/oracle/product/9.2.0.1.0
"ls" command
lists all files and directories in the specified directory. If no location is defined it acts on the
current directory:
root> ls
root> ls /u01
root> ls –al
The "-a" flag lists hidden "." files. The "-l" flag lists file details.
head
head –n filename
tail
tail –n filename
tail –n filename displays the last n lines of a file. To continually see the end of a logfile that’s
being added to, use tail –f filename; this will keep displaying the end of the file until you press
CTRL-C.
The –f option is commonly used to monitor export or import logs, whereas the –n option is often
used for alert logs.
CAT COMMAND
The output of the file will scroll past the screen if the file is large. Pipe this file with more to
display the contents one
screen at a time
(for example, cat long_file.txt | more).
Also use with /dev/null to erase the contents of log files (for example, cat /dev/null >
alertdemo.log).
grep
grep –i string filename
searches the file for occurrences of the string, with the –I making the search case-insensitive. For
example,
searches the alert log for any ORA-600 errors and displays them to the screen. This can also be
used with ps -ef to find Oracle background processes
The "grep -i ORA-" command limits the output to lines containing "ORA-". The "-i" flag
makes the comparison case insensitive. A count of the error lines can be returned using
the "wc" command. This normally give a word count, but the "-l" flag alteres it to give a
line count:
"cd" command
root> cd /u01/app/oracle
"touch" command
"rm" command
root> rm my.log
root> rm -R /archive
"find" command
searches for the file in every directory starting at / and working through each subdirectory. Any
location can be given instead of /, with the current location (.) being a common option.
When the command is executed and the search attempts to view a directory you don’t have
permissions for, you will receive an error but the search will continue.
To suppress these error messages add 2>/dev/null to the end of the command asfollows:
The "/" flag represents the staring directory for the search. Wildcards such as "dbms*" can be
used for the filename.
"which" command
can be used to find the location of an executable you are using:
The "which" command searches your PATH setting for occurences of the specified executable.
"chmod" command
is used to alter file permissions after the file has been created:
The "chown" command is used to reset the ownership of files after creation:
The "-R" flag causes the command ro recurse through any subdirectories.
CHGRP
clear
clear clears the screen of all previous output and moves the prompt to the top of the screen.
"useradd" command
"usermod" command
is used to modify the user settings after a user has been created:
root> usermod -s /bin/csh my_user
"userdel" command
"passwd" command
"who" command
root> who
root> who | head -5
root> who | tail -5
root> who | grep -i ora
root> who | wc -l
The "head -5" command restricts the output to the first 5 lines of the who command.
The "tail -5" command restricts the output to the last 5 lines of the who command.
The "grep -i ora" command restricts the output to lines containing "ora".
The "wc -l" command returns the number of lines from "who", and hence the number of
connected users.
The "uname" and "hostname" commands can be used to get information about the host:
root> uname -a
OSF1 oradb01.lynx.co.uk V5.1 2650 alpha
root> uname -a | awk '{ print $2 }'
oradb01.lynx.co.uk
root> hostname
oradb01.lynx.co.uk
The Korn shell allows you to check for the presence of a file using the "test -s" command.
In the following script a backup log is renamed and moved if it is present:
#!/bin/ksh
if test -s /backup/daily_backup.log
then
DATE_SUFFIX=`date +"%y""%m""%d""%H""%M"`
mv /backup/daily_backup.log /backup/archive/daily_backup$DATE_SUFFIX.log
fi
The find command can be used to supply a list of files to the rm command:
The following scripts shows how a number of commands can be run as the "oracle" user the
"root" user:
#!/bin/ksh
su - oracle <<EOF
ORACLE_SID=LIN1; export ORACLE_SID
rman catalog=rman/rman@w2k1 target=/ cmdfile=my_cmdfile log=my_logfile append
EOF
This is often necessary where CRON jobs are run from the root user rather than the oracle user
Compress Files
In order to save space on the filesystem you may wish to compress files such as archived redo
logs. This can be using either the gzip or the compress commands. The gzip command results in
a compressed copy of the original file with a ".gz" extension. The gunzip command reverses this
process:
gzip myfile
gunzip myfile.gz
The compress command results in a compressed copy of the original file with a ".Z" extension.
The uncompress command reverses this process:
compress myfile
uncompress myfile
CRON
There are two methods of editing the crontab file. First you can use the "crontab -l > filename"
option to list the contents and pipe this to a file. Once you've editied the file you can then apply it
using the "crontab filename":
Login as root
crontab -l > newcron
Alternatively you can use the "crontab -e" option to edit the crontab file directly.
Use cronjob
crontab -l ( list current jobs in cron)
crontab -e ( edit current jobs in cron )
_1_ _2_ _3_ _4_ _5_ $Job_Name
1 - Minutes (0-59)
2 - Hours ( 0-24)
3 - day of month ( 1- 31 )
4 - Month ( 1-12)
5 - A day of week ( 0- 6 ) 0 -> sunday 1-> monday
e.g. 0 0 1 * 5 Means run job at Midnight on 1st of month & every friday
Useful Files
Path Contents
/etc/passwd User settings
/etc/group Group settings for users.
/etc/hosts Hostname lookup information.
/etc/system Kernel parameters for Solaris.
/etc/sysconfigtab Kernel parameters for Tru64.
symbolic links
How to find the symbolic links that point to the old path in your oracle_home and
appl_top.
This command is useful in cloning after restore from source to target that symbolic link are not
pointing to source.
ls -al `find . -type l` | grep $OLD_PATH
SORT
PORT
bdf
bdf displays all the filesystems and the amount of disk space on an HP-UX server. Use this
command to determine what filesystems you have and how much disk space is available on each.
Df
df
displays your filesystems and the amount of space available on each. It is common to
use this with the –k option to display results in kilobytes
du
du -s *
shows disk usage for every subdirectory below your current location. This is useful for finding
directories with large files to be purged.
Echo
or
echo $ENV_VARIABLE
echoes the contents of the text string or the value of a variable to the screen. Echoing strings is
common in shell scripts. Echoing variables is useful for verifying your environment.
fuser
fuser filename
Glance
glance -m
invokes the HP-UX system-monitoring tool. Use this to check system performance on HP
servers.
groups
groups
diff
compares two filenames and displays the differences between the two.
dmesg
dmesg
shows all the messages generated from the operating system boot. This is useful when you’re
looking for problems with the server or when you’re trying to find out more about the system. It
is common to pipe this command through more (for example, dmesg | more).
ln (linking)
ln –s /location_of_actual_file/filename /where_file_should_appear
creates asoft link for a file. This makes it seem as if the file appears in one location even though
it really
exists in another location. For example,
ln –s $ORACLE_BASE/admin/demo/pfile/ initdemo.ora
$ORACLE_HOME/dbs/initdemo.ora
creates a link so the initdemo.ora file appears to exist in the $ORACLE_HOME/dbs directory.
The –s makes this a soft link so that a ls –l will denote the file is a link and provide its real
location. Removing the link will notdelete the physical file. Removing the actual file will leave
the link intact, but it will be invalid.
mailx
mailx [email protected]
prepares to send an email to the user at the email address specified (for example,
joe@acme_test.com). Next, you will be prompted for a subject and then need to press Enter.
Next you write your message. To send the message, press CTRLD.
pine—pine invokes a Unix-based email system.
page
page filename
sar
sar
is System Activity Report and can show a variety of operating system statistics. This is
frequently used for monitoring the server and it has several options.
script
script filename.txt
creates a log of everything that is displayed to the screen until you press CTRL-D.
This is similar to spooling output in SQL*Plus. This is good to run when applying patches.
su
su – username
prompts you for a password to log in as the specified user. The hyphen indicates that the
user’s .profile will be executed and new environment variables will be established when you log
in. If the hyphen is not specified, the user will inherit the environment of the previous user. There
is normally not a good reason to log in as a user without setting up the proper environment, so
generally you should use the hyphen.
talk
requests an online chat session with the user with a specific tty. You can obtain this information
using the who command. This is a handy means of communicating with another user logged in
to the same server. The communication will continue until you quit with a CTRL-C. To refresh
the screen display, use CTRL-L.
tar
compresses a directory, its subdirectories, and all the files into one file called filename.tar. This
TAR file can be compressed and transported via cp or ftp to a different location. Once it has been
moved to its location, the
directory, its subdirectories, and all the files can be recreated by issuing
in its new location. This is a common way to move directory trees from one server to another.
Oracle patches also are often in TAR files.
top
top
invokes a Unix server monitoring utility. This is a handy utility to have as it gives a good
snapshot of system performance and load.
truss
truss -p PID
traces all the system calls for a given process ID. This command is handy when you have to see
exactly what a process is doing at the Unix level.
uptime
uptime
displays the current time, how long the server has been running since the last reboot, the number
of users on the server, and the load average for the present time, five minutes ago, and 15
minutes ago.
wc (word count)
wc –l filename
gives a word count of the number of lines in a file. Use it in conjunction with grep to count the
number of lines found. For example, to see the number of Oracle errors in your alert log, issue
this command
grep -i ORA- | wc –l
This will show you the number of lines with ORA- in them.
which
which command
determines which executable you are going to use when you issuecommand. For example,
which sqlplus
/u01/app/oracle/product/8.1.6/bin/sqlplus.
whereis