Useful Linux commands for DBA

Linux/UNIX Commands For DBAs

Here we learn some useful Linux commands for dba.

Read: How to add SWAP Space in Linux

Useful Linux commands for an Oracle DBA

1. PWD: show the present working directory.

$pwd
/home/oracle

2. ls: list all files and directories from a given location, if the location is not given it shows the list from the current directory.

$ls
$ls /u01
$ls -l (list file details)
$ls -a (show hidden files)

3. cd: change directory or switch directory.

$cd /u02

4. touch: This is used to create an empty file with default permissions.

$touch ocp.log

5. rm: is used to remove files and directories from the location.

$rm ocp.log
or
$rm -r directory_name (remove directory with files)

6. mv: move the file from one location to another location or rename the file or directory.

$mv ocp.log /u02/ocp.log (move file in another location)
$mv ocp.log ocptech.log (rename file)

7. cp: make a copy of an existing file using the cp command.

$cp (from) (to)
$cp ocp.log /u02/ocp.log
$cp *.log /u02

8. mkdir: command used to create a new directory.

$mkdir data
or
$mkdir /u01/data

9. rmdir: used to remove directory.

$rmdir data

10. find: is used to find the location of any file.

$find / -name ocp.log
or
$find / -print |grep -i ocp.log

"/" represent the starting directory path.

11. which: find executable file path which you are using

$whcih sqlplus

Basic Linux Commands for DBAs

12. chmod: is used to change the file permissions.

$chmod 777 *.log

Permission info

7 (u+rwx) 7 (g+rwx) 7 (o+rwx) read + write + execute
6 (u+rw) 6 (g+rw) 6 (o+rw) read + write
5 (u+rx) 5 (g+rx) 5 (o+rx) read + execute
4 (u+r) 4 (g+r) 4 (o+r) read only
2 (u+w) 2 (g+w) 2 (o+w) write only
1 (u+x) 1 (g+x) 1 (o+x) execute only

u for the user, g for group & o for others.

You can use characters also instead of number values during granting the permissions.

$chmod o+rwx *.log
$chmod g+r *.log
$chmod -Rx *.log

13. chown: is used to set the ownership.

$chown -R oinstall.dba *
or
chown -R oinstall.dba /u01  (-R for recursive any sub-directory)

14. useradd: used to add a new user at OS level.

$useradd -G oinstall -g dba -d /usr/users/username -m -s /bin/ksh username
or
$useradd oracle
The “-G” flag specifies the primary group.
The “-g” flag specifies the secondary group.
The “-d” flag specifies the default directory.
The “-m” flag creates the default directory.
The “-s” flag specifies the default shell.

15. usermod: is used to modify existing user settings.

$usermod -s /bin/csh username

16. userdel: delete exiting user from OS level.

$userdel -r my_user

Here "-r" is used to remove the default directory.

17. passwd: is used to set or reset the password for OS users.

$passwd oracle

18. who: show the list of all OS users' connections.

$who
$who | head -5
$who | tail -5
$who | grep -i ora
$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.

19. ps: show the current process information

$ps
$ps -ef | grep	pmon
$ps -ef | grep -i ora
$ps -ef | grep -i ora | grep -v grep
$ps -ef | grep -i [o]ra

20. kill: used to kill the running process

$kill 231254
or
$kill -9 231254

DBA Unix Commands

21. uname & hostname: get information about the host

$Linux
$ uname -a
$ hostname
$ hostname -i

22. Compress tools in Linux:

These all are the tools available in Linux tar, star, gzip, bzip2, zip, cpio. These tools are used to compress the files, which is consumed a high amount of space on the filesystem. The gzip command compresses the original file and returns it with the .gz extension.

$gzip file_name
$gunzip file_name.gz

The compress command is also used to compress the original file with the ".Z" extension.

$compress file_name
$uncompress file_name

23. vmstat: used to check OS performance.

$ vmstat 5 3

procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0 1461956 126328 1951188    0    0    12     7   23   39  0  0 99  0  0
 0  0      0 1461956 126328 1951216    0    0     0    24   47   71  0  0 100  0  0
 0  0      0 1461848 126336 1951216    0    0     0    10   39   65  0  0 100  0  0

24. free: shows the current usage of memory

$ free
             total       used       free     shared    buffers     cached
Mem:       3925088    2465844    1459244          0     126408    1951572
-/+ buffers/cache:     387864    3537224
Swap:      4194296          0    4194296

25. iostat: Report OS i/o stats.

$ iostat
Linux 2.6.32-220.el6.x86_64 (rhel6.localdomain)         09/22/2021      _x86_64_        (2 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.12    0.00    0.20    0.18    0.00   99.50

Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn
sda               1.68        42.10        23.96    1644442     935896

26. sar: monitor the CPU, memory, disk, and network information using sar command.

sar syntax:

sar [options] [interval [count]]

Available options for sar.

CPU:
Basic CPU: sar [-u] [interval [count]]
Load Average: sar -q [interval [count]]

Memory:
Kernel Paging: sar -B [interval [count]]
Unused Memory: sar -r [interval [count]]
Swap Space: sar -S [interval [count]]

Disk:
Average Disk I/O: sar -b [interval [count]]
Disk I/O: sar -dp [interval [count]]

Network:
Network: sar -n DEV [interval [count]]
Network Errors: sar -n EDEV [interval [count]]

Example: Monitor the CPU usage

$ sar -u 1 5
Linux 2.6.32-220.el6.x86_64 (rhel6.localdomain)         09/22/2021      _x86_64_        (2 CPU)

02:00:00 PM     CPU     %user     %nice   %system   %iowait    %steal     %idle
02:00:01 PM     all      0.53      0.00      0.00      0.00      0.00     99.47
02:00:02 PM     all      0.00      0.00      1.10      0.00      0.00     98.90
02:00:03 PM     all      0.00      0.00      0.57      0.00      0.00     99.43
02:00:04 PM     all      0.00      0.00      0.00      0.00      0.00    100.00
02:00:05 PM     all      0.00      0.00      0.51      0.00      0.00     99.49
Average:        all      0.11      0.00      0.42      0.00      0.00     99.47

1 = output in every one second
5 = show CPU average after five rows.

27. mpstat: show stats of processor

$ mpstat 5 2
Linux 2.6.32-220.el6.x86_64 (rhel6.localdomain)         09/22/2021      _x86_64_        (2 CPU)

02:04:11 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest   %idle
02:04:16 PM  all    0.00    0.00    0.10    0.00    0.00    0.00    0.00    0.00   99.90
02:04:21 PM  all    0.00    0.00    0.00    0.00    0.00    0.10    0.00    0.00   99.90
Average:     all    0.00    0.00    0.05    0.00    0.00    0.05    0.00    0.00   99.90

28. top: show the top running tasks in OS

29. init: shutdown and reboot the server.

$init 6 (reboot the server)
$init 0 (shutdown the server)

30. passwd: used to change the password.

passwd oracle - The command is executed by the root user, to change the password of the oracle user.

#passwd oracle 

passwd - the command is executed by the user who is already logged in to the server. Who wants to change their own password.

$passwd

31. cat: using the cat command we can open a file on the terminal or find the ORA-ERROR line in the file.

$ cat idea_data.log

SQL*Loader: Release 11.2.0.3.0 - Production on Sat Aug 21 10:28:15 2021

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

SQL*Loader-128: unable to begin a session
ORA-01017: invalid username/password; logon denied

Or you can find the ORA - Error inside the file.

$ cat idea_data.log |grep -i ORA-
ORA-01017: invalid username/password; logon denied

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 gives a word count, but the "-l" flag alters it to give a line count.

32. Remove old files using the find command

Find 10 days old files with extension ".csv"

$find /u01/delhi/*.csv -mtime +10

or

$find /u01/delhi/ -name *.csv -mtime +10

Remove 10 days old files with extension ".csv"

$find /u01/delhi/ -name *.csv -mtime +10 -exec rm -f {} ;
$find /u01/delhi/ -name *.csv -mtime +10 -exec rm -f {} \;
$find /u01/delhi/ -name *.csv -mtime +10 -delete;

Find big-size files using the find command in the current directory.

$find . -type f -print0 | xargs -0 du -h | sort -hr | head -20

Useful Linux commands for DBA, I hope you found this article useful if yes please write your fillings in the comment box and subscribe to our YouTube channel. If you have more commands please share them with us at support@ocptechnology.com

3 thoughts on “Useful Linux commands for DBA

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top