Mir Sayeed Hassan – Oracle Blog

Oracle DBA – Tips & Techniques | Learn with real-time examples

  • Translate

  • It’s Me






  • My Certificates

  • Links

    My Acclaim Certification : Credly Profile
    My Oracle ACE Pro Profile

  • Achievements

    Awarded Top 100 Oracle Blogs from Worldwide - #RANK 39
  • VISITORS COUNT

  • Verified International Academic Qualification from World Education Service (WES)

    Verified International Academic Qualification from World Education Service (WES)

  • Jobs

Linux Commands for Oracle DBA for day-to-day work environment

Posted by Mir Sayeed Hassan on February 9th, 2021

Linux Commands for every Oracle DBA for day-to-day work environment

The Linux commands discussed here are basic as well as advanced level where every Oracle DBA should known on there day-to-day work environment.

Find the help command 

[oracle@testdb ~]$ man ls 

[oracle@testdb ~]$ info ls

Note: 

Man is a help command, you can get more information about any commands in UNIX.

info – Read more information about the commands

DIERCTORY COMMANDS

Find the present working directory

[oracle@testdb ~]$ pwd

/home/oracle

Create a directory

[oracle@testdb ~]$ mkdir test
[oracle@testdb ~]$ mkdir -p /home/oracle/test

Note: 

– The above command will create a directory data under the present working directory

– In case if you want to create a directory with different location, use the 2nd example with mkdir -p 

Moving to a directory

[oracle@testdb ~]$ cd /home/oracle/test

Deleting a directory

[oracle@testdb ~]$ rmdir test

Note: 

The above command will delete  empty directory, this will not work if the directory is not empty.

Deleting a directory along with files by force

[oracle@testdb ~]$ ll /home/oracle/test

-rw-r--r--. 1 oracle oinstall 0 Feb  9 09:25 t1.sql
-rw-r--r--. 1 oracle oinstall 0 Feb  9 09:25 t2.txt

[oracle@testdb ~]$ rm -rf test

Move a contents of directory to another

[oracle@testdb ~]$ mv /home/oracle/test /home/oracle/test_copy

Rename a folder

[oracle@testdb ~]$ mv test test_copy

FILE COMMANDS

To view the contents of a file

[oracle@testdb test]$ cat sample1.txt 

This is the sample text file to review my contents of file by using the cat command

[oracle@testdb test]$ cat sample2.sql 

select status from v$instance;
select name from v$datafile;

Creating a file

[oracle@testdb test]$ vi newfile.txt
:wq - save & quit

or 

[oracle@testdb test]$ cat > newfile.txt

This blog is to shared the realtime example by mir sayeed hassan
ctl+c - exit.

Remove a file normally or force

[oracle@testdb test]$ rm newfile.txt
[oracle@testdb test]$ rm -rf newfile.txt

Rename a file

[oracle@testdb test]$ mv sample2.sql sample1.sql 

Copy the file to another location

[oracle@testdb test]$ cp sample1.sql /home/oracle

Listing a file

[oracle@testdb test]$ ls
sample1.sql

List file with creation time

[oracle@testdb test]$ ls -lt

total 4
-rw-r--r--. 1 oracle oinstall 60 Feb  9 09:34 sample1.sql

List files along with  hidden

[oracle@testdb test]$ ls -alt

total 8
drwxr-xr-x.  2 oracle oinstall   25 Feb  9 09:51 .
drwx------. 20 oracle oinstall 4096 Feb  9 09:49 ..
-rw-r--r--.  1 oracle oinstall   60 Feb  9 09:34 sample1.sql

More option use man

[oracle@testdb test]$ man ls

Copying a file from source machine to destination machine

[oracle@testdb test]$ scp sample1.sql oracle@192.168.*.*:/home/oracle/

Note: The above file will be copied as oracle user privilege & enter the oracle password when prompted.

Find the free space of system at OS Level in KB

[oracle@testdb ~]$ df

Filesystem     1K-blocks     Used Available Use% Mounted on
devtmpfs         1880388        0   1880388   0% /dev
tmpfs            1890628        0   1890628   0% /dev/shm
tmpfs            1890628   189020   1701608  10% /run
tmpfs            1890628        0   1890628   0% /sys/fs/cgroup
/dev/sda2       25678828 25136660    542168  98% /
/dev/sda1         520876   225836    295040  44% /boot
tmpfs             378128        0    378128   0% /run/user/1009
tmpfs             378128        0    378128   0% /run/user/1001
tmpfs             378128        0    378128   0% /run/user/1002

Find the free space of system at OS Level in GB

[oracle@testdb ~]$ df -h 

Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        1.8G     0  1.8G   0% /dev
tmpfs           1.9G  690M  1.2G  38% /dev/shm
tmpfs           1.9G  185M  1.7G  10% /run
tmpfs           1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda2        25G   24G  670M  98% /
/dev/sda1       509M  221M  289M  44% /boot
tmpfs           370M     0  370M   0% /run/user/1003
tmpfs           370M     0  370M   0% /run/user/1001

Find the space used by directory 

[oracle@testdb ~]$ du -h test

4.0K test

Find the space used by file 

[oracle@testdb test]$ du -h sample1.sql 
4.0K sample1.sql

View the contents of file page by page

[oracle@testdb test]$ cat sample1.sql | more

select status from v$instance;
select name from v$datafile;

Log into the remote server by SSH

[oracle@testdb ~]$ ssh oracle@192.168.*.*

Compressing the file 

[oracle@testdb test]$ gzip sample1.sql 
[oracle@testdb test]$ ls
sample1.sql.gz

Uncompressing the file

[oracle@testdb test]$ gunzip sample1.sql.gz

[oracle@testdb test]$ ls
sample1.sql

View the schedule jobs using crontab

[oracle@testdb ~]$ crontab -l

############################################################################
##Schedule the script by Mir to Delete old .trc,.trm,.xml,.aud files from DB
############################################################################

00 22 * * * /home/oracle/scripts/del_trc_trm_aud_xml_files.sh

Display the current running process 

[oracle@testdb ~]$ ps -eaf

UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 Feb02 ?        00:00:42 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root         2     0  0 Feb02 ?        00:00:00 [kthreadd]
root         3     2  0 Feb02 ?        00:00:19 [ksoftirqd/0]
root         5     2  0 Feb02 ?        00:00:00 [kworker/0:0H]
oracle    2317     1  0 Feb05 ?        00:00:58 ora_psp0_mirtdb
oracle    2322     1  1 Feb05 ?        01:32:08 ora_vktm_mirtdb
oracle    2326     1  0 Feb05 ?        00:00:11 ora_gen0_mirtdb
oracle    2328     1  0 Feb05 ?        00:00:20 ora_diag_mirtdb
—
—
—

Kill the process 

[oracle@testdb ~]$ kill -9 2377

or

[oracle@testdb ~]$ kill -F 2377

Shutdown the OS

Shutdown server immediately

[root@testdb ~]# shutdown -h now

Shutdown server after 10 minutes

[root@testdb ~]# shutdown -h +10

Reboot server immediately

[root@testdb ~]# shutdown -r now
[root@testdb ~]# init 0
[root@testdb ~]# init 6
[root@testdb ~]# reboot

Note:

init 0 – Shutdown the server with ok prompt

init 6 – Reboot the server

reboot – Reboot the server

Find out the free memory

[oracle@testdb ~]$ free -m

              total        used        free      shared  buff/cache   available
Mem:           3692         433         134         874        3125        2302
Swap:             0           0           0

Find the top memory consuming process at OS Level

[oracle@testdb ~]$ top

top - 10:20:22 up 6 days, 20:10,  1 user,  load average: 0.00, 0.03, 0.05
Tasks: 140 total,   1 running, 139 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  6.1 sy,  0.0 ni, 93.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  3781256 total,   135112 free,   445648 used,  3200496 buff/cache
KiB Swap:        0 total,        0 free,        0 used.  2355036 avail Mem 
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
 2322 oracle    -2   0 1784212  44528  42708 S   6.2  1.2  92:20.58 oracle
23823 oracle    20   0  162032   4432   3760 R   6.2  0.1   0:00.02 top
    1 root      20   0  128364   8360   5588 S   0.0  0.2   0:42.21 systemd
    2 root      20   0       0      0      0 S   0.0  0.0   0:00.15 kthreadd                                                          

Changing the permission for directory

[oracle@testdb ~]$ chmod 755 test

Changing the permission for directory

[oracle@testdb ~]$ chmod -R 755 test

Changing the permission for file

[oracle@testdb ~]$ chmod -R 777 /home/oracle/test/sample1.sql

Changing the read,write & execute permission to owner, group & other users.

[root@testdb oracle]# chmod ugo+rwx test

Changing the read, write permission to owner, read permission to group & nothing for user

[root@testdb oracle]# chmod u+rw,g+r test

Note:

You can use the below formula to allocate the permission as per your requirement.

7- READ

4 – WRITE

1 – EXECUTE

Changing the ownership of directory

[oracle@testdb ~]$ chown oracle:oinstall test
[oracle@testdb ~]$ chown oracle:dba test

In case if you want the ownership as root, then you need to login as root & execute

[root@testdb oracle]# chown root:dba test

Changing the ownership of file

[root@testdb test]# chown root:dba sample1.sql

Change exiting group 

[root@testdb test]# chgrp oinstall test

Changing the password for user

[root@testdb ~]# passwd oracle

Changing password for user oracle.
New password: *********
Retype new password: *********
passwd: all authentication tokens updated successfully.

Find the name of machine

[oracle@testdb ~]$ uname -a

Linux testdb.mirsayeedhassan.com.novalocal 4.1.12-124.34.1.el7uek.x86_64 #2 SMP Tue Dec 3 16:33:43 PST 2019 x86_64 x86_64 x86_64 GNU/Linux

How to switch one user to another 

[oracle@testdb ~]$ su - oracle

Password: 

Last login: Tue Feb  9 14:55:23 EST 2021 on pts/1

or any other user assign in /etc/sudoers

[oracle@testdb ~]$ su - mir

Password: 
Last login: Tue Feb  9 14:38:53 EST 2021 from 5.116.75.142 on pts/1

Add a new user 

[root@testdb ~]# useradd mir

Delete the existing user

[root@testdb ~]# userdel mir123

Add a new group 

[root@testdb ~]# groupadd -g dbanew
[root@testdb ~]# more /etc/group|grep dba

dbanew:x:1020:

Delete a group 

[root@testdb ~]# groupdel dbanew

Mount a new disk 

[root@testdb ~]# mount /u02

Umount a existing disk 

[root@testdb ~]# umount /u02

Alias for the command

[root@testdb ~]# alias ls='ls -al'
[root@testdb ~]# ls
total 458804
dr-xr-x---.  5 root root      4096 Jan 16 18:18 .
dr-xr-xr-x. 20 root root      4096 Oct 30 14:05 ..
-rw-------.  1 root root      1642 Oct  1  2019 anaconda-ks.cfg

Display the current date of system

[root@testdb ~]# date
Tue Feb  9 15:16:05 EST 2021

Display the calendar of current month 

[root@testdb ~]# cal

    February 2021   
Su Mo Tu We Th Fr Sa
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28

Create a file 

[root@testdb test]# touch test

Copy the output of of “ls -al” to test file

[root@testdb test]# ls sample1.sql > test
[root@testdb test]# cat test 
-rwxrwxrwx. 1 root dba 60 Feb  9 09:34 sample1.sql

Clear the display of current page

[root@testdb ~]# clear

Compare the files 

[root@testdb test]# cmp sample1.sql sample2.sql 

Difference of two files

[oracle@testdb test]$ diff sample1.sql sample2.sql 
2a3
> select * from V$log;
[root@testdb test]# cmp sample1.sql sample2.sql 
cmp: EOF on sample1.sql

Note: Here it compare both the files, if the contents are same then it return null, if not it return EOF.

Copy all the files from one directory to another 

[root@testdb ~]# cp -r /home/oracle/test/* /home/oracle/

Copy the files

[root@testdb test]# cp sample1.sql sample2.sql 

Display the Statement or parameter by using echo

[oracle@testdb ~]$ echo $ORACLE_SID
mirtdb
[oracle@testdb ~]$ echo $ORACLE_HOME
/u01/app/oracle/product/11.2.0/dbhome_1

Search a string or file

[oracle@testdb ~]$ ps -ef | grep oracle

oracle     894     1 99 15:17 ?        00:51:02 ./kswapd0
oracle    1112     1  0 15:21 ?        00:00:00 /bin/bash ./go
oracle    1156  1150 15 15:21 ?        00:03:56 /tmp/.X25-unix/.rsync/c/lib/64/tsm --library-path /tmp/.X25-unix/.rsync/c/lib/64/ /usr/sbin/httpd rsync/c/tsm64 -t 515 -f 1 -s 12 -S 10 -p 0 -d 1 p ip
root      2569 30249  1 15:46 pts/1    00:00:00 su - oracle

[oracle@testdb ~]$ ls -al | grep oracle

drwx------. 21 oracle oinstall  4096 Feb  9 15:39 .
-rw-r--r--.  1 oracle oinstall    59 Feb  6 16:29 afiedt.buf
-rw-------.  1 oracle oinstall 16538 Feb  9 15:46 .bash_history

List any jobs running in server

[oracle@testdb ~]$ jobs

View a file

[oracle@testdb ~]$ more /home/oracle/test/sample1.sql 
select status from v$instance;
select name from v$datafile;

Ping an IP Address or Hostname

[oracle@testdb ~]$ ping 5.253.*.* 
PING 5.253.25.53 (5.253.*.*) 56(84) bytes of data.
64 bytes from 5.253.*.*: icmp_seq=1 ttl=64 time=0.052 ms
64 bytes from 5.253.*.*: icmp_seq=2 ttl=64 time=0.056 ms

or

[oracle@testdb ~]$ ping testdb.mirsayeedhassan.com
PING testdb.mirsayeedhassan.com (5.253.*.*) 56(84) bytes of data.
64 bytes from testdb.mirsayeedhassan.com (5.253.*.*): icmp_seq=1 ttl=64 time=0.038 ms

Display the process status

[oracle@testdb ~]$ ps
  PID TTY          TIME CMD
 2732 pts/1    00:00:00 bash
 2897 pts/1    00:00:00 ps

Show the last lines of the files 

[oracle@testdb test]$ tail -f sample1.sql 
select status from v$instance;
select name from v$datafile;
[oracle@testdb test]$ tail -10 sample1.sql 
select status from v$instance;
select name from v$datafile;
[oracle@testdb test]$ cat -n sample1.sql 
     1 select status from v$instance;
     2 select name from v$datafile;

Note:

tail -f – This will display the current running log

tail -10 – This will display the last 10 lines of the log file

cat -n – This will display with number

Display the user currently login in

[oracle@testdb ~]$ who

rit      pts/0        2021-02-09 14:11 (129.0.205.224)
mir      pts/1        2021-02-09 14:38 (5.116.75.142)
[oracle@testdb ~]$ who am i

mir      pts/1        2021-02-09 14:38 (5.116.75.142)

Display all the current user login & there activity 

[oracle@testdb ~]$ w

 16:04:03 up 7 days,  1:54,  2 users,  load average: 6.31, 6.65, 5.59
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
ovono    pts/0    129.0.205.224    14:11    1:36m  0.71s  0.45s sshd: ovono [priv]  
mir      pts/1    5.116.75.142     14:38    0.00s  1.41s  0.43s sshd: mir [priv]

Display who am i

[oracle@testdb ~]$ who am i

mir      pts/1        2021-02-09 14:38 (5.116.75.142)

or

[oracle@testdb ~]$ id

uid=1002(oracle) gid=1002(oinstall) groups=1002(oinstall),1003(dba),1004(oper),1005(asmadmin) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

Archive files using tar & un-tar

[root@testdb oracle]# tar -cvf test_tar.tar test

test/
test/sample1.sql
test/sample2.sql
test/test.tar
[root@testdb oracle]# tar -xvf test_tar.tar 

test/
test/sample1.sql
test/sample2.sql
test/test.tar

=====Hence tested & verify in my test env=====