Skip to content

SHELL COMMANDS for DBAs

##################################
## SHELL COMMANDS for DBAs v1.0 ##
## Mustafa DOGANAY - 25.03.2013 ##
## https://doganay.wordpress.com ##
##################################
Changing prompt for current user
################################
export PS1="$(whoami)@$(hostname) $ "
export PS1="\u@\h \w $ "
export PS1=$(echo "$(whoami)@$(hostname) $ " | sed 's/.server.tr.europe.intranet//g')

count of process which has been an $ORACLE_SID
##############################################
ps -ef | grep $ORACLE_SID | grep -v grep | grep -v ora_ | wc -l

display all files by sizes in current directory (sorted by size desc)
#####################################################################
du -sh * | sort -nk1

script outputs
##############
## In your shell script:
exec 1 > output.log
exec 2 > output.err

clear all files matching your wildcard
######################################
for i in your_wildcard*; do dd if=/dev/null of=$i; done
## or
find . -name "your_wildcard*" -exec basename {} \; | awk '{print "> "$1}' > a.sh
. ./a.sh

cpuinfo
#######
egrep "^processor|^cpu cores|^core id" /proc/cpuinfo

move directory with permission
##############################
tar -cf ora.tar /u01/app/oracle/product/11.2.0/dbhome_1/
cp ora.tar /target_dir
tar -xvpf ora.tar

PID of the most recent backgrounded process
###########################################
echo $!

One hour ago (1 hour ago)
#########################
$(TZ=GMT-1 date +%d_%m_%Y.%H)

Two hours ago (2 hours ago)
###########################
$(TZ=GMT+0 date +%H)

Running a VM as 32-bit
######################
add this line to the .vmx file:
monitor_control.disable_longmode = 1

Changing Permissions at specific user Level (setfacl, getfacl)
##############################################################
## You can give permission to a user:
## 4:read 2:write 1:execute
setfacl -r -m user:myuser:4 myfile

## clear permissions:
setfacl -d user:myuser:4 myfile

## set permissions of files that will be created in a specific directory:
setfacl -m d:u::rwx,d:g::r-x,d:u:myuser:r-x,d:m:r-x,d:o:---,user:myuser:r-x mydirectory
## or
setfacl -r -m default:user:myuser:r-x,default:user::rwx,default:group::r-x,default:mask:r-x,default:other:--- mydirectory

Argument list too long
######################
find . -name "*.trc" | xargs rm

NUMBER OF ARGUMENTS
###################
$#

USING ARRAYS
############
export CALENDAR=( 31 28/29 31 30 31 30 31 31 30 31 30 31 )
echo "The last days of months:"
for i in ${CALENDAR[*]}; do
echo $i
done

ARRAY LENGTH
############
${#myarray[*]}

LIST AT LEAST TWO CHARACTER LENGTH FILES
########################################
ls | grep ^...*$

32 or 64 bit?
#############
SOLARIS
-------
/usr/bin/isainfo -kv

HPUX
----
/bin/getconf KERNEL_BITS

LINUX
-----
uname -m

Remove Carriage Return Characters
#################################
tr -d "\r"

Change shell of a user
######################
Interactive:
passwd -e <username>

Non Interactive:
usermod -s /bin/bash <username>

Closing File Handles
####################
Close Input File Handles (standard input is explicitly closed)
<&-
Close Output File Handles (standard output is explicitly closed)
>&-

Cluster Sequence Number of a node
#################################
/usr/sbin/clinfo -n

Cluster Release
###############
cat /etc/cluster/release

DATA DIFFERENCE IN FILES
########################
comm -12 file1 file2 --> file1 intersect file2
comm -23 file1 file2 --> file1 minus file2
comm -13 file1 file2 --> file2 minus file1
(files must be sorted)

Directory copy
##############
cp -prf <src_dir> <dst_dir>

Number of cores
###############
linux:
egrep "^processor|^cpu cores|^core id" /proc/cpuinfo

solaris:
prtdiag -v

CPIO
####
backup:
cd your_dir
find . -print | cpio -odmv > your_dir.cpio

restore:
cpio -idmv < your_dir.cpio > cpio.log

Cron Allowing
#############
Add username into file:
/usr/lib/cron/cron.allow

Remove username from file:
/usr/lib/cron/cron.deny

CRON FIELDS
###########
# Minute Hour MonthDay Month Weekday Command
# minute (0-59),
# hour (0-23),
# day of the month (1-31),
# month of the year (1-12),
# day of the week (0-6 with 0=Sunday).

CTRL-C NOT WORKING
##################
1)
remove this line in /etc/profile:
trap "" 2
2)
Logout-Login again

CUT DELIMITER
#############
## default delimiter is "TAB"
## to use space as delimiter:
echo "a b"|cut -d" " -f1

USING DATE
##########
## yesterday
$(TZ=PST+24 date +%Y%m%d)
## today
$(TZ=PST date +%Y%m%d)
## tomorrow
$(TZ=PST-24 date +%Y%m%d)

Hour,Minute,Second------> %H%M%S

$(date +%B) --------> February
$(date +%b) --------> Feb

#### sets time
date -u MMDDHHMI
date -u 02142140 (14th Feb 21:40)

DATA DUMP
#########
dd if=/dev/zero of=/dev/null bs=512b count=1

Empty Big Files
###############
cat /dev/null > file1
cp /dev/null file1

Which command will be executed?
###############################
which zip

Remove directory and sub-directories
####################################
rm -rf <dir_to_delete>

Delete whitespace at the end of the lines
#########################################
sed 's/ $//g'

Disable Secure Linux
####################
set enforce 0

or

edit /etc/selinux/config
------------------------
SELINUX=disabled

and reboot

How do I disable a user account not using userdel
#################################################
1-Star out the password field in /etc/shadow (placing a "*" in front of the password entry)
2-Change the shell to something like /usr/local/bin/expired or /bin/false
3-any .rhosts file should be removed or renamed
4-any currently running jobs should be removed
5-any cron or at jobs owned by the user should be disabled
6-any program mail forwarding forwards to should be checked for backdoors
(e.g., open a shell on some high number port after receiving mail with
"XYZZY" in the subject.) or disabled.
7-other accounts on any hosts.equiv machines should be disabled
8-other access files (for example if you're using ssh).
9-To be on the safe side, you might consider making the home directory inaccessible (chmod 000).

UNIX/Linux Performance Commands
###############################
top
prstat
vmstat
iostat
prtdiag
prtconf
swap -l
swap -s
sar
free (linux)

How To Kill Cpu Utilizating Processes
#####################################
ps aux | awk '{if ( $3 > 50.5 ) print "kill -9 "$2}'

Change FQN to Hostname
######################
Add this line into /etc/resolv.conf :
domain your_domain.your_suffix

Password Encryption for a spesific user
#######################################
passwd -N <username>

Difference between single quote(') and double quote(")
########################################
echo "current working directory is: `pwd`"---------> output: current working directory is: /export/oracle
echo 'current working directory is: `pwd`'---------> output: current working directory is: pwd

exp or expdp ?
##############
## Mustafa DOGANAY 27.07.2011 INGBANK
if [ $# -ne 1 ]
then
echo "Usage : expordp.sh <DUMPFILE>"
exit 1
fi

FILE=$1

if [ `ls $FILE 2>/dev/null|wc -l` -eq 0 ]
then
echo "File not found."
exit 2
fi

if [ `strings $FILE|grep SYS_EXPORT_TABLE | wc -l` -ne 0 ]
then
echo "DATA PUMP."
else
echo "TRADITIONAL EXPORT."
fi

Using multiply operator within expr
###################################
Use backslashes:
expr 60 \* 60 \* 24

grep multiple patterns
######################
fgrep -f $PATTERN_FILE $SOURCE_FILE

Enable/Disable FTP access
#########################
To enable FTP, in /etc/inetd.conf file:
comment out ftp line.

# solaris 10
svcadm restart inetd

linux or solaris 9
If you use inetd:
/etc/init.d/inetd restart
If you use xinetd:
/etc/init.d/xinetd restart
If it hangs, use kill:
Restart inet service:
kill -HUP `ps -ef|grep inet|awk '{print $2}'`
Show Hidden Characters
#########################
:set list
## Do not Show Hidden Characters
################################
:set nolist

Use different delimiter in awk
#################################
Use -F:
export DELIMITER="/"
awk -F"$DELIMITER" '{.....}'

Also, you may consider using:
awk 'BEGIN{FS="/"}{...}'

Parent Process ID
#################
$PPID

PID of current shell
####################
$$
Sort find output by modify date
-------------------------------
find . -name "pattern" |xargs stat -t|sort -nk13
Last argument of previous command
---------------------------------
$_

Clear Screen
------------
ctrl-L

Comparing a remote file with a local file
-----------------------------------------
ssh user@host cat /remotepath/remotefile | diff /localpath/localfile -
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: