UNIX & Linux For Oracle Developers & DBAs

General Information
Command Syntax command [option] [source file(s)] [target file]
Whatis whatis <command>
whatis grep
Manual man
man ls

man -k ls

man -k ls | grep list
 
Directory Structure
Basic directory structure of a UNIX or Linux file system
/ Root
/bin Commands that can be accessed by all users
/boot Static boot loader files
/dev Device files
/etc Host-specific configuration files
/home User home directories
/lib Shared libraries and kernel modules
/mnt Mount point for temporary file systems
/opt Add on application software
/root Home directory for root user
/sbin System binaries
/tmp Temporary files
/usr Shareable, read-only
/var Variable data files
 
User
Change User su <user_name>
su root

su oracle

exit

exit
Become root su -
su -

exit
Current user account whoami
whoami

su -

whoami

exit

whoami 
Change password passwd
passwd

-- you will be asked to enter your current password then twice enter the new password, identical in both character and case
Change user su
su
Change user sudo
sudo
Change Group ID groupmod -g <NEWGID> <GROUP>
usermod -u 100 oracle
groupmod -g 101 dba
find / -user 56 -exec chown -h 100 {} \;
find / -group 34 -exec chgrp -h 101 {} \;
usermod -g 101 oracle
Change User ID usermod -u <NEWUID> <LOGIN>
See Change GROUP ID demo above
 
Navigation
Present Working Directory pwd
pwd
Change Directory: Navigating from root cd <directory_name>
pwd

cd /etc

pwd
Change Directory: Navigating down one level pwd

cd pam.d

pwd
Change Directory ... up one level pwd

cd ..

pwd
Change To Root Directory cd /home/oracle

pwd

cd /

pwd
 
Navigation Demo
Navigation Exercise -- navigate to root directory
cd /

-- verify location
pwd

-- navigate to home directory
cd home

pwd

-- make a directory
mkdir  morgan

cd morgan

pwd

mkdir dest
mkdir src

ls -l

cd src

pwd

cd ../dest

pwd

cd ..

pwd
 
File Handling
Make Directory mkdir <directory_name>
ls -al

mkdir morgan

ls -al
Remove Directory rmdir <-options> <directory_name>
ls -al

rmdir morgan

ls -al
Remove Directory & Subdirectories rm -i <directory_name>
cd $HOME

pwd

ls -al

mkdir morgan

ls -al

cd morgan

pwd

mkdir subdir

ls -al

cd ..

pwd

rmdir morgan

rm -ir morgan

-- answer "y" (yes) to questions for removal to proceed
Create File touch <file_name>
cd $HOME

touch mlib.ora

ls -l
Copy cp <old_name> <new_name>
cp mlib.ora copiedfile.ora

ls -l *.ora
Recursive Copy (include directories and their contained files) cp -ir <old_name> <new_name>
cd /home/oracle

mkdir arch

cd arch

touch mlib.ora

cd ..

pwd

ls -l

cp -r arch /tmp

cd /tmp

ls -l
Secure CoPy scp <user_name>@<server_name>:<source_path_and_file_name> <target_path_and_file_name>
scp oracle@bigdog:/home/oracle/*.gz .
Delete (Remove) a file rm <file_spec>
touch delfile

ls -l

rm delfile
List ls [<-options>] <file_spec>
ls

ls *ora

ls -l *ora

ls -larn

ls -larn *ora

ls -lt
Move mv <starting_file> <resulting_file>
touch movefile

ls -l

mv movefile /home/movefile

ls -l 

cd /home

ls -l
Rename mv <starting_file> <resulting_file>
touch rename.txt

ls -l

mv rename.txt rename.log

ls -l
Wildcards: Multiple Characters *
touch test1
touch test2
mkdir arch

ls -l

mv test* arch

cd /arch

ls -l

rm t*

ls -l
Wildcard: Single Character ?
touch test1
touch test2
touch test99
mkdir arch

ls -l

mv test? arch

ls -l

cd /arch

ls -l

rm test?

ls -l
Change Owner chown <owner_name> <directory_or_file_name>
touch chngdemo

ls -al chngdemo

chown oracle chngdemo

ls -al chngdemo
Change Group chgrp <group_name> <directory_or_file_name>
touch chngdemo

ls -al chngdemo

chgrp dba chngdemo

ls -al chngdemo
Change Mode Triplets ROOT - GROUP - USER
drwxrwxrwx
Change Mode chmod <code> <file_name>
touch chngdemo

ls -al chngdemo

chmod 755 chngdemo

ls -al chngdemo
CHMOD and UMASK Codes and Results

First bit is '-' if a regular file
First triplet is owner
Second triplet is group owner
Third triplet is others
Binary Octal Permissions
000 0 ---
001 1 --x
010 2 -w-
011 3 -wx
100 4 r--
101 5 r-x
110 6 rw-
111 7 rwx
 
Code UMASK File Permissions Directory Permissions
111 666 ---------- ---x--x--x
222 555 --w--w--w- --w--w--w-
333 444 --w--w--w- --wx-wx-wx
444 333 -r--r--r-- -r--r--r--
555 222 -r--r--r-- -r-xr-xr-x
666 111 -rw-rw-rw- -rw-rw-rw-
777 000 -rw-rw-rw- -rwxrwxrwx
124 653 -----w-r-- ---x-w-r--
644 133 -rw-r--r-- -rw-r--r--
660 017 -rw-rw---- -rwxrw----
750 027 -rw-r----- -rwxr-x---
755 022 -rw-r--r-- -rwxr-xr-x
Setting the setuid bit chmod u<+ | ->s <file_name>
touch chngdemo

ls -al chngdemo

chmod u+s chngdemo

ls -al chngdemo
Setting the setgid bit chmod g<+ | ->s <file_name>
touch chngdemo

ls -la chngdemo

chmod g+s chngdemo

ls -al chngdemo
Setting the setoid bit chmod o<+ | ->s <file_name>
touch chngdemo

ls -la chngdemo

chmod o+s chngdemo

ls -al chngdemo
Symbolic changes

u = user
g = group
o = other
a = all
chmod o<+ | ->x <file_name>
touch chngdemo

ls -la chngdemo

chmod o+x chngdemo

ls -al chngdemo
 
Environment & Environment Manipulation
server uptime uptime
uptime
 15:29:26 up 236 days, 14:39, 1 user, load average 0.96, 1.04, 1.09
set the date and time date <-options>
date

date -s "20130703 23:59:00"
rename host server Rename a Linux server's name
su

cd /etc/sysconfig/network

-- change the HOSTNAME entry and reboot
List hardware/operating system environment uname <-options>
Switch Description
a all
i hardware platform
m hardware platform
n machine name
o operating system
p processor
r kernel release
s kernel name
v kernel version
uname -a

uname -i

uname -m

uname -o

uname -p

uname -r

uname -s

uname -v
View the full environment: All variables set
set

export ORACLE_SID=orabase

echo $ORACLE_SID

set
List the environment of the current session: This is a more limited collection than is displayed by "set" env
env

export CURUSER

echo $CURUSER

env
Remove an environment variable unset <name>
set

unset CURUSER

set
profile Set the environment and run the .profile commands
cd /home/oracle

-- use an editor to add the following to the .bash_profile file
export ZZYZX=$PATH
-- and save the file

more .bash_profile

echo $ZZYZX

source ~/.bash_profile

echo $ZZYZX
Command history $HISTSIZE = <integer>
echo $HISTSIZE

!10

!-4

export $HISTSIZE=100

echo $HISTSIZE
Command prompt -- bash
PS1=whoami@hostname\current working directory\time\date

-- korn
PS1='$PWD>'
Home environment variable $HOME
cd /

pwd

echo $HOME

cd $HOME

pwd

cd ~
Rebooting init <integer>
Level Description
0 In Linux the init 6 command gracefully reboots the system running all the K* shutdown scripts before performing the reboot.
1 Restarts the system in single user mode
6 In Linux the init 6 command gracefully reboots the system running all the K* shutdown scripts before performing the reboot.
# reboot
# reboot -f

# init 0

# init 6
Shutdown shutdown -<switch>
Level Description
h Equivalent to init 0
r Equivalent to init 6
   
# shutdown -r

# shutdown -h

-- what, specfically, does this do?
# last -x | grep -e shutdown -e reboot
 
File Backup & Restore
Tape Archive (tar) tar <options> <file_spec> <tarball_name>
tar -cvf * beta1RAC.tar
Compressed TAR tar <options> <file_spec> <tarball_name>
tar -cxvf * beta1RAC.tar.gz
UNTAR tar <options> <file_spec> <tarball_name>
tar -xvf beta1RAC.tar
UNTAR Compressed tar <options> <file_spec> <tarball_name>
tar -zxvf beta1RAC.tar.gz
UNJAR A Java File jar <options> <file_spec> <jar_name>
jar -xf patch.jar
CPIO cpio -ivf <file_name>
cpio -idv < as_linux_x86_101300_disk1.cpio
 
Cron Commands
Switch Description
-e Edit the current crontab using the editor specified by the VISUAL or EDITOR environment variables. After you exit from the editor, the modified crontab will be installed automatically.
-l List - display the current crontab entries
-r Remove the current crontab
Edit your crontab file, or create one if it doesn't already exist crontab -e
crontab -e
Display the crontab file's contents crontab -l
crontab -l
Remove the crontab file crontab -r
crontab -r
Specify the crontab user crontab -u <username>
crontab -u username -l
Display the last time the crontab file was edited (not on all systems) crontab -v
crontab -v
 
Applications
Concatenate (CAT)

An obscure word meaning "to connect in a series"
cat [options] <file_name>
Switch Description
A show all
b number non-blank lines
n number all lines
s squeeze blank: never more than one blank line
cd $ORACLE_HOME/network/admin

cat tnsnames.ora


cat -n tnsnames.ora
Disk Free Space df <switches>
df
df -k
df -m
df -h (Linux only)
Disk Usage du <switches>

Switch Description
a display an entry for each file contained in the current directory
c display a grand total of the disk usage found by the other arguments
d # the depth at which summing should occur. -d 0 = and the subdirectory level
H calculate disk usage for link references specified at the command line
k show sizes as multiples of 1024 bytes, not 512 bytes
L calculate disk usage for link references anywhere
s report only the sum of the usage in the current directory, not for each file
x only traverse files and directories on the device on which the pathname argument is specified
du -sH
du -sk
View environment variable's value echo <environment variable>
echo $ORACLE_SID
Find Files find . -name "<file_name>" -print
find * -name "crontab*" -print
Find archive logs older than 3 days and delete them find
find . -name *.arc -type f -ctime +3 -exec rm {} \;
Delete files over 14 days old find $WORKDIR -name "<filemask>" -mtime+<days> -exec rm {}\;
find $WORKDIR -name "*.dat.*" -mtime + 14 -exec rm {} \;
Find and delete trace files more than 7 days old find . -name "<file_name>" -print
find $ORACLE_BASE/admin/$ORACLE_SID -name "*.trc" -mtime +7 -exec rm -f {} \;
Global Regular Expression Print (GREP). Prints all lines matching a certain pattern

Submitted to the library by JP Vijaykumar. Thank you.
grep [-options] pattern [filename]
-- I am having the following files in a directory

[oracle@localhost jp]$ ls -l
total 48
-rwxrwxrwx 1 oracle oinstall 258 Aug 10 22:56 jp1.dat
-rwxrwxrwx 1 oracle oinstall 258 Aug 10 22:56 jp2.dat
-rwxrwxrwx 1 oracle oinstall 258 Aug 10 22:56 jp3.dat
-rwxrwxrwx 1 oracle oinstall 258 Aug 10 22:56 jp4.dat
-rwxrwxrwx 1 oracle oinstall 258 Aug 10 22:57 jp5.dat


/* I have to include the following message "THIS SCRIPT BELONGS TO ROAD FARMS COMPANY" in the third line of each of these files.
   The redirection option ">>" can append the line to the end of a file. Let us see how to insert the message in the middle of a file.
   I checked whether any of my files contain the message. */



[oracle@localhost jp]$ grep "THIS SCRIPT BELONGS TO ROAD FARMS COMPANY" *dat
[oracle@localhost jp]$

/* I created a while loop to implement the task of copying the message in the middle of the files *.dat. */

[oracle@localhost jp]$ cat while_loop.sh
#!/bin/sh -x
#Author JP Vijaykumar Oracle DBA
#Date 08 - 08 - 08
#THIS SCRIPT INSERTS THE FOLLOWING MESSAGE
#IN THE THIRD LINE OF FILES *.DAT
#THIS SCRIPT BELONGS TO ROAD FARMS COMPANY

ls -1 |grep -v while_loop.sh|while read FILE
do
head -2 $FILE >> TEMP
echo "THIS SCRIPT BELONGS TO ROAD FARMS COMPANY" >> TEMP
tail -`cat $FILE|wc -l|awk '{print $0 -2}'` $FILE >> TEMP
mv TEMP $FILE
done

exit

I executed the script.

[oracle@localhost jp]$./while_loop.sh

I checked whether any of my files contain this message.

[oracle@localhost jp]$ grep "THIS SCRIPT BELONGS TO ROAD FARMS COMPANY" *dat
jp1.dat:THIS SCRIPT BELONGS TO ROAD FARMS COMPANY
jp2.dat:THIS SCRIPT BELONGS TO ROAD FARMS COMPANY
jp3.dat:THIS SCRIPT BELONGS TO ROAD FARMS COMPANY
jp4.dat:THIS SCRIPT BELONGS TO ROAD FARMS COMPANY
jp5.dat:THIS SCRIPT BELONGS TO ROAD FARMS COMPANY
Find a file containing a string $ grep "dbms_heat" /app/oracle/product/12.1.0/db_1/rdbms/admin/*
Disk i/o Memory Statistics iostat
iostat
More more <file_name>
cd /etc

more protocols
Processor related statistics mpstat -P ALL 12 5
-P is either calling out a specific processor all. the above is 12 second intervals and 5 reports in total
mpstat
Processes ps <options> grep <filter>
ps -ef

ps -ef | grep ora
System Activity Statistics (Paging) sar <options>
sar -B
Tee

Splits the output of another command, sending it to a file and to the terminal.
tee <file_name>
cd $HOME

ls -larn

ls -larn | tee zzyzx

ls -larn

more zzyzx
Top CPU processes top
top

q
Top Swapping ordered by swap usage top
top
-- let to start ... then
Op<enter>
Free memory information free -m
free -m
Virtual Memory Statistics vmstat
vmstat
Swapping meminfo
/proc/meminfo
View the full path of shell commands which <shell command name>
which grep

which id

which which
 
System Management
Network Configuration ifconfig <options>
ifconfig
Wireless Configuration iwconfig <options>
iwconfig wifi0 power off
Shared Library Dependencies ldd <program_name>
ldd ?
Display Loaded Kernel Modules lsmod
lsmod
Display Open Files lsof
lsof
Display Formatted Process Tree pstree <options>
pstree -ca
 
User Management
Change Password and Expiration Information chage
chage ?
Force Password Change At Next Logon chage
chage -d0
Create Group groupadd <switch> <group_number> <group_name>
groupadd -g 500 oinstall
Display All Resource Limits for the Current User ulimit <switches>
ulimit -a
Display data about one or more users finger <switches> users
finger -l oracle
Remap key stty <keyword> <key>
stty ERASE <backspace_key>
-- displays stty ERASE ^?
 
vi
create a new file or open a file for editing vi
vi initSID.ora
quit without saving :q!
:q!
save and quit :wq
:wq
insert Esc i
i
append Esc A
A
open a new line Esc o
o
replace a single character Esc r
r
replace a multiple characters Esc R
R
delete a single character Esc x
x
delete line Esc dd
dd
delete word Esc dw
dw
undo Esc u
u
 
SQL*Plus Shell Script Demo
bash demo -- log onto Linux as the user oracle

-- verify the UNIX user

[oracle@gamma2 home]$ whoami
oracle

-- log into Oracle as scott/tiger
[oracle@gamma2 ~]$ sqlplus scott/tiger@orcl

SQL*Plus: Release 10.2.0.1.0 - Production on Mon Nov 20 17:18:37 2006

Copyright (c) 1982, 2005, Oracle.  All rights reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, Real Application Clusters, OLAP and Data Mining options

-- create a test table
SCOTT@orcl > create table test (
  2  testcol VARCHAR2(30));

Table created.

-- exit SQL*Plus
SCOTT@orcl >exit;

[oracle@gamma2 ~]$Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 Production With the Partitioning Option, Real Application Clusters, OLAP and Data Mining options


-- verify location is Oracle's home directory
[oracle@gamma2 ~]$ pwd
/home/oracle

-- create a file named "morgan"
[oracle@gamma2 ~]$ touch morgan

-- look at the file's permissions
[oracle@gamma2 ~]$ ls -al morgan
-rw-r--r--  1 oracle oinstall 0 Nov 20 15:56 morgan

-- make it executable
[oracle@gamma2 ~]$ chmod 755 morgan

-- verify it is executable
[oracle@gamma2 ~]$ ls -al morgan
-rwxr-xr-x  1 oracle oinstall 0 Nov 20 15:56 morgan

-- open the file using vi
[oracle@gamma2 ~]$ vi morgan
-- insert the following w/o quotes: "touch swartz"

-- verify the file's contents
[oracle@gamma2 ~]$ more morgan
touch swartz

-- execute it
[oracle@gamma2 ~]$ ./morgan

-- verify the swartz file was created
[oracle@gamma2 ~]$ ls -al swartz
-rw-r--r--  1 oracle oinstall 0 Nov 20 16:04 swartz

-- delete the "swartz" file
[oracle@gamma2 ~]$ rm swartz

-- open morgan with vi, remove the TOUCH command and put in what you see after "more morgan" below
[oracle@gamma2 ~]$ vi morgan

[oracle@gamma2 ~]$ more morgan
sqlplus scott/tiger@orcl <<EOF
INSERT INTO test (testcol) VALUES ('Swartz');
COMMIT;
EXIT
EOF


-- execute it ... everything else is what scrolls on screen
[oracle@gamma2 ~]$ ./morgan

SQL*Plus: Release 10.2.0.1.0 - Production on Mon Nov 20 17:20:16 2006

Copyright (c) 1982, 2005, Oracle.  All rights reserved.

Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, Real Application Clusters, OLAP and Data Mining options

SCOTT@orcl >
1 row created.

SCOTT@orcl >
Commit complete.

SCOTT@orcl > exit;
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, Real Application Clusters, OLAP and Data Mining options
[oracle@gamma2 ~]$
 
Rescue
In the event of failure boot from the Linux CD or DVD boot: linux rescue

# chroot /mnt/sysimage

# cd /boot/grub

use vi to edit/configure files
 
Example Configuration Files
Export Display export DISPLAY=localhost:0:0

echo $DISPLAY
.bash_profile alias ob='cd $ORACLE_BASE'
alias oh='cd $ORACLE_HOME'
alias cm='cd $ORACLE_HOME/oracm/log'
alias sql='sqlplus "/ as sysdba"'
hosts.equiv
# Comment/Uncomment those entries for your cluster below.
 
alpha1 oracle
alpha2 oracle
#beta1 oracle
#beta2 oracle
#gamma1 oracle
#gamma2 oracle
delta1 oracle
delta2 oracle
 
alpha-node1 oracle
alpha-node2 oracle
#beta-node1 oracle
#beta-node2 oracle
#gamma-node1 oracle
#gamma-node2 oracle
#delta-node1 oracle
#delta-node2 oracle
 
# DO NOT Comment out this entry!
topdog oracle
 
Related Topics
Cron Jobs
Linux Install for Oracle 11.2.0.3
  Morgan's Library Page Footer
This site is maintained by Dan Morgan. Last Updated: This site is protected by copyright and trademark laws under U.S. and International law. © 1998-2014 Daniel A. Morgan All Rights Reserved
 
* Thank you Maxim Demenko for corrections to his page