Linux Most Used Commands

I am in the process of putting my notes into web pages, please keep checking in later for more contents
random password awk cut find sed sort
top   prev   next  

screen

screen -S xxx    --> Create new screen window
screen -ls       --> list screen names
screen -x -r xxx --> attach
CTRL-a CTRL-d    --> detach
CTRL-a : fit ---> fit window
CTRL-a : quit ---> kill all windows and exit
screen ref1  screen ref2 
top
top   prev   next  

putty prompt

echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}\007"

PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME} ${HOST} ${ORACLE_SID} : ${PWD} \007"'
echo $PROMPT_COMMAND

PS1="\[\u@\h: \w \s-\v\] \\$ "
PS1="\[\u@\h: \w \${ORACLE_SID} \s-\v\] \\$ "

top   prev   next  

du -h order by size

find . -maxdepth 1 -type f -exec du -h {} + | sort --human-numeric-sort --reverse
find . -maxdepth 1 -type f -exec du -h {} + | sort --human-numeric-sort

find /some/path -type f -printf '%s %p\n' | sort -rn
find . -type f -printf '%s %p\n' | sort -rn

top   prev   next  

df -h order by percent

df -h /u01 /u03 /u11 /u12 /u13 /u14 /u98 /u99

df -h|perl -pe "s/[ \t]+/ /g"|sort -n -k 5
df -h|perl -pe "s/[ \t][ \t]*/ /g"|sort -n -k 5

df -h|grep -i u03|perl -pe "s/[ \t][ \t]*/ /g"|sort -n -k 5

top   prev   next  

Merge Two Files Line By Line

paste -d'\0' a1.txt a2.txt 
paste -d'<' a1.txt a2.txt
paste -d'<' a1.txt a2.txt|sed 's/</ --< /g'
paste -d' ' a1.txt a2.txt
paste a1.txt a2.txt   --> default delimiter is tab

top   prev   next  

Shared Memory and Semaphore Segments

ipcs -b
ipcs -m
ipcs -s

# ipcrm -m 
# ipcrm -s 

top   prev   next  

Crontab output file with Date in File Name

# using the following command failed
* * * * * myprog > /dbbackups/rman_backup_`date '+%Y%M%d'`.log 2<&1

# using the following command will work 
* * * * * myprog > /u01/app/oracle/log/rman_backup_$(date +\%Y\%m\%d_\%H:\%M:\%S\%z).log 2<&1
* * * * * myprog > /u01/app/oracle/log/rman_backup_$(date +\%Y\%m\%d).log 2<&1

top   prev   next  

Environmental Variables Used By Oracle

ORACLE_BASE
ORACLE_HOME
ORACLE_SID
LD_LIBRARY_PATH
PATH
TMP=/tmp
TEMP=/tmp
TMPDIR=/tmp
ORACLE_PATH
SQLPATH
ORACLE_TERM
NLS_DATE_FORMAT="YYYY-MM-DD HH24:MI:SS"
TNS_ADMIN=$ORACLE_HOME/network/admin
ORA_NLS11=$ORACLE_HOME/nls/data
ORA_TZFILE=$ORACLE_HOME/oracore/zoneinfo/timezlrg.dat
ORAENV_ASK
ORACLE_TRACE=T  # If it is set to T, many Oracle shell scripts (eg: oraenv.sh) use the set -x command
export TWO_TASK=sid; sqlplus xxx/yyy
THREADS_FLAG=native
UMASK  # umask 022,  default file creation permission 644

CLASSPATH=$ORACLE_HOME/JRE
CLASSPATH=${CLASSPATH}:$ORACLE_HOME/jlib
CLASSPATH=${CLASSPATH}:$ORACLE_HOME/rdbms/jlib
CLASSPATH=${CLASSPATH}:$ORACLE_HOME/network/jlib


top   prev   next  

Interact between sqlplus and shell

-- Passing sqlplus out to shell variable
VALUE=`sqlplus -silent "/ as sysdba" <<END
set pagesize 0 feedback off verify off heading off echo off
select max(sequence#) from v\\\$log_history;
exit;
END`

if [ -z "$VALUE" ]; then
    echo "No rows returned from database"
    exit 0
else
    echo "Max Sequence Number: $VALUE"
fi
-- Passing sql or sqlplus error code back to shell
sqlplus -s > junk1 "/ as sysdba" <<EOF
column num_tables new_value num_tables format 9999
select count(*) num_tables from dba_tables;
exit num_tables
EOF
echo "Number of tables: $?"

top   prev   next  
top

My Unix Environment

whoami  id   id -a
hostname
pwd
ls -lart

env|grep -i $HOME
env|grep -i oracle
alias

history|tail -n 10   -->  my last 10 commands

echo $SHELL          --> what is my shell
set -o|egrep "vi\W|emacs" --> what is editor for my shell

most used directories:
/root ---> root user's home directory
/db/q/sprn/v1/admin --> dbqsprn user's home directory

. --> current
.. --> parent

cd --> go to home directory
/  -> root directory

/etc
/root
/bin
/usr/bin
/usr/local/bin
$ORACLE_HOME/bin
/db/q/sprn/v1/product/11.2.0/bin
/db/q/sprn/v1/admin

userful commands:
find $ORACLE_BASE -name "alert*.log" -print
history|tail -n 10  --> my last 10 commands

sar

sar -ALL

cd /var/log/sa

-- previous day sar data
sar -f sa19
sar -f /var/log/sa/sa19


top   prev   next  

Environment Variables

env
env|sort
env|sort|more

env|grep -i oracle
env|grep -i oracle_sid
env|grep -i oracle_home
env|grep -i tns_admin
env|grep -i home
env|grep -i path
env|grep -i sqlpath


cd $HOME
cd ~
cd
cd ..
cd -
cd $ORACLE_HOME
cd $ORACLE_HOME/bin --> tnsping, sqlplus, lsnrctl
cd $ORACLE_HOME/dbs --> initSID.ora

cd $ORACLE_HOME/network/admin --> tnsnames.ora sqlnet.ora listener.ora
cd $TNS_ADMIN

cd $ORACLE_HOME/rdbms/admin   --> addmrpt.sql awrrpt.sql

pwd


top   prev   next  

very basic


set -o vi
set -o emacs
set -o|egrep "vi|emacs"
set -o|egrep "vi\W|emacs"

df -h
df -m
df -k

df -h /db/c/ntmk/v*

du -sh * 2>/dev/null

whoami
who am i
hostname
pwd

ps -ef|grep -i smon
ps -ef|grep -i $ORACLE_SID

top

man df
info df

-- --------------

cd /db/c/ntmk/v1/admin/NTMKC1/logs
pwd


ls -lart
ls -lart alert*.log

head -100  alert_NTMKC1.log
tail -100  alert_NTMKC1.log
more  alert_NTMKC1.log
vi    alert_NTMKC1.log

history

alias
alias alert

stty erase ^h

dbtest -sid inkd1
dbinfo -sid inkd1

in sqlplus, make sure you are in the right place:
select * from v$instance;
select * from v$database;
select user from dual;
!whoami
!hostname
!pwd
!ls -lart

top   prev   next  

More Complex

env|grep -i SQLPATH

export PATH=$PATH:$SQLPATH:.

1): using which,
short coming if the .sql file don't have the -x execute permissiojn, you can not find it

which as.sql
which ts_used.sql

2): using locate
locate ts_used.sql
locate as.sql

or
locate ts_used.sql|xargs ls -l
locate as.sql|xargs ls -l

top   prev   next  

sudo su - oracle

sudo su - oracle --> don't forget the - which will bring additional information

top   prev   next  

ls command

Turn off color Terminal
unalias ls

list files in different directories
ls -la /vol*/oradata/stage/.*orcl_idx_004.dbf

top   prev   next  

sed command

echo "This is a test"|sed 's/test/practice/'

echo "This is a test"|perl -p -i -e "s/test/practice/g"

For making the replacement in all files in the current directory, the following would be all that is needed:

perl -p -i -e "s/SHARK/DOLPHIN/g"  *


And then to search and make changes in files in the current and all sub directories:

find . -type f -print | xargs perl -p -i -e "s/SHARK/DOLPHIN/g"

top   prev   next  

awk command

#cutout the domain part of hostname
hostname|awk -F. '{ print $1 }'
hostname=`hostname|awk -F. '{ print $1 }'`

top   prev   next  

xargs command

echo /home/oraprd/auto_arch.9992|xargs rm -fr
find /home/oraprd/auto_arch.* type d -name "auto_arch.[0123456789]*" -mtime +1 -print 2>/dev/null|xargs rm -fr

top   prev   next  

find command

find . -name "*DBMS_AQ*" -print
find . -iname "*DBMS_AQ*" -print

10 days ago
find . -name "*.trc" -mtime +10 -exec ls -l {} \;
find . -name "*.trc" -mtime +10 -exec rm {} \;

10 minutes ago
find . -name "*.trc" -mmin +10 -exec ls -l {} \;
find . -name "*.trc" -mmin +10 -exec rm {} \;

-type and -print
find . -type f -name "*dmp.gz" -mtime +1 -print -exec ls -l {} \;
find . -type f -name "*dmp.gz" -mtime +1 -print -exec  rm -f {} \;

find . -name "*${ORACLE_SID}*arch*.arc" -mtime +80 -exec ls -l {} \;

find . -name "*${ORACLE_SID}*arch*.arc" -mtime +30 -exec \rm -f {} \;

top   prev   next  

cut

grep -i port|cut -d: -f2|cut -d' ' -f2`

top   prev   next  

file comparison

md5sum filename1
diff filename1 filename2

top   prev   next  

sort

sort -t: -n -k 1,1|tail -20
-t: seperator
-n: numeric
-r: reversal
-k: sort field
-u: sort unique    combine with diff command can find and/or eliminate duplicates: diff original sort_uniqued

top   prev   next  

ps long format

ps uxww
ps auxww|grep -i $USER
ps -Ao pid,args -ww

top   prev   next  

swap space Information

swapon -s
/sbin/swapon -s

cat /proc/meminfo
cat /proc/cpuinfo


top   prev   next  

Send Out Email

1): mailx -s "subject line" aaa.bbb@ccc.com,ddd.eee@fff.com < file1.txt

2): send as an attachment
uuencode file1.txt file1.txt|mailx -s "subject line" aaa.bbb@ccc.com,ddd.eee@fff.com

top   prev   next  

sshd ssh

SSHOpts="-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ConnectTimeout=10 -t"

ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ConnectTimeout=10 -t

chkconfig sshd off
chkconfig sshd on

/sbin/chkconfig --list

service sshd stop
service sshd start

top   prev   next  

scp and rsync

# to local
rsync -avI --safe-links --del myusername@myhostname:/mydirectorypath/* .
rsync -avI                    myusername@myhostname:/mydirectorypath/* .

# to remote
rsync -auvzpogl -e ssh /u01/oracle/product/11.203 linux4:/u01/oracle/product/11.203

top   prev   next  

nslookup ifconfig ping

nslookup proddb002

/sbin/ifconfig

gui:
/usr/bin/system-config-network &


uhshen: --> . /u02/oracle/dba/hshen/users/my_alias.sh

-- ---------------
more my_alias.sh
cd /u02/oracle/dba/hshen/users
export HISTFILE=/u02/oracle/dba/hshen/users/.k
exec ksh
-- ---------------

alias fhh='alias|grep $1'
COLUMNS=350
set -o vi;

alias ls='ls -F'
alias lsd='ls -l|grep ^d'
alias rm='rm -i'
alias cp='cp -i'
alias sp='sqlplus "/as sysdba"'


OSTYPE=`uname -a|cut -d' ' -f1`
# echo $OSTYPE

if [[ "$OSTYPE" == "SunOS" ]];
then
         export HOSTNAME=`hostname`;
         stty sane
else if [[ "$OSTYPE" == "AIX" ]];
then
         export HOSTNAME=`hostname -s`;
else if [[ "$OSTYPE" == "Linux" ]];
then
         export HOSTNAME=`hostname -s`;
else
         export HOSTNAME=`hostname -s`;
fi
fi
fi

-- ------------------------------
function find_dot_file {
find /u02/oracle/shortcuts /u02/oracle/dbacommon /u02/oracle/menuscripts  -name $1 -print 2>/dev/null
find `echo $PATH|sed 's/:/ /g'` -name useap -print 2>/dev/null
}
-- ------------------------------



alias my_bdump='echo "column name format a21\ncolumn value format A80\nset line 180 trimspool on\nselect name,value from v\$parameter;"|sqlplus "/ as sysdba"|grep -i background_dump_dest|sed "s/background\_dump\_dest/cd/g"'
alias my_bdump2=`my_bdump`


'echo "column name format a21\ncolumn value format A80\nset line 180 trimspool on\nselect name,value from v\$parameter;"|sqlplus "/ as sysdba"|grep -i background_dump_dest|sed "s/background\_dump\_dest//g"|xargs cd'


top   prev   next  

vi takes out extra ^M

:%s/[ctrlkey+v and ctrl-key+M]//g
:%s/^V^M//g

# check free space in aix
# lsvg
rootvg
asdbvg

# lsvg asdbvg
# lsvg asdbvg|grep -i free

# df -h /backups
/dev/hd4           1.00      0.91    9%     4957     3% /

# lslv 
# lslv hd4
# lsattr -El hd4

# chfs -a size=+10000M /backups # Increase by 10GB


# lsattr -El hdisk2|grep queue_depth

# lsdev -Cc adapter
# lsdev -Cc adapter -spci | grep fcs

df -h
df -m
df -k

df -h /db/c/ntmk/v*


-- --------------

cd /db/c/ntmk/v1/admin/NTMKC1/logs
pwd


ls -lart
ls -lart alert*.log

head -100  alert_NTMKC1.log
tail -100  alert_NTMKC1.log
more  alert_NTMKC1.log
vi    alert_NTMKC1.log

history

alias
alias alert

stty erase ^h

-- --------------

dbtest -sid inkd1

dbinfo -sid inkd1

Turn off color Terminal
unalias ls


top   prev   next  

screen

screen -S xxx    --> Create new screen window
screen -ls       --> list screen names
screen -x -r xxx --> attach
CTRL-a CTRL-d    --> detach
CTRL-a : fit ---> fit window
CTRL-a : quit ---> kill all windows and exit
screen ref1 
screen ref2 

top
top   prev   next  

putty prompt

echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}\007"

PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME} ${HOST} ${ORACLE_SID} : ${PWD} \007"'
echo $PROMPT_COMMAND

PS1="\[\u@\h: \w \s-\v\] \\$ "
PS1="\[\u@\h: \w \${ORACLE_SID} \s-\v\] \\$ "
top
top   prev   next  

My Unix Environment

#kill -9 pid, strip space for pid
ps -ef|grep -i ${ORACLE_SID}|grep -i escalate|sed 's/  / /g'|sed 's/  / /g'|sed 's/  / /g'|awk '{print "kill -9 " $2}'


grep -i CORRUPT alert_seap.log|cut -d' ' -f8|sort -u|wc -l

grep -i CORRUPT alert_seap.log|cut -d' ' -f8|sort -u|awk '{print mv $1 ../rat_bat}'

grep -i CORRUPT alert_seap.log|cut -d' ' -f8|sort -u|awk '{print "mv " $1 " ../rat_bad"}'

grep -i CORRUPT alert_seap.log|cut -d' ' -f8|sort -u|awk '{print "mv /u03/oracle/data/rat/" $1 " /u03/oracle/data/rat_bad"}'

whoami  id   id -a
hostname
pwd
ls -lart

topas --> top

env|grep -i $HOME
env|grep -i oracle
alias

history|tail -n 10   -->  my last 10 commands

echo $SHELL          --> what is my shell
set -o|egrep "vi\W|emacs" --> what is editor for my shell

most used directories:
/root ---> root user's home directory
/db/q/sprn/v1/admin --> dbqsprn user's home directory

. --> current 
.. --> parent
~  --> home directory
cd --> go to home directory
/  -> root directory

/etc
/root
/bin
/usr/bin
/usr/local/bin
$ORACLE_HOME/bin
/db/q/sprn/v1/product/11.2.0/bin
/db/q/sprn/v1/admin

userful commands:
find $ORACLE_BASE -name "alert*.log" -print
history|tail -n 10  --> my last 10 commands

sar 

sar -ALL

cd /var/log/sa

-- previous day sar data
sar -f sa19
sar -f /var/log/sa/sa19


top   prev   next  

Environment Variables

env
env|sort
env|sort|more

env|grep -i oracle
env|grep -i oracle_sid
env|grep -i oracle_home
env|grep -i tns_admin 
env|grep -i home
env|grep -i path
env|grep -i sqlpath


cd $HOME
cd ~
cd
cd ..
cd $ORACLE_HOME
cd $ORACLE_HOME/bin --> tnsping, sqlplus, lsnrctl 
cd $ORACLE_HOME/dbs --> initSID.ora

cd $ORACLE_HOME/network/admin --> tnsnames.ora sqlnet.ora listener.ora
cd $TNS_ADMIN

cd $ORACLE_HOME/rdbms/admin   --> addmrpt.sql awrrpt.sql

pwd


top   prev   next  

very basic


set -o vi
set -o emacs
set -o|egrep "vi|emacs"
set -o|egrep "vi\W|emacs"

df -h
df -m
df -k

df -h /db/c/ntmk/v*

du -sh * 2>/dev/null

whoami
who am i
hostname
pwd

ps -ef|grep -i smon
ps -ef|grep -i $ORACLE_SID

top

man df
info df

-- --------------

cd /db/c/ntmk/v1/admin/NTMKC1/logs
pwd


ls -lart
ls -lart alert*.log

head -100  alert_NTMKC1.log
tail -100  alert_NTMKC1.log
more  alert_NTMKC1.log
vi    alert_NTMKC1.log

history

alias
alias alert

stty erase ^h

-- --------------

dbtest -sid inkd1
dbinfo -sid inkd1

in sqlplus, make sure you are in the right place:
select * from v$instance;
select * from v$database;
select user from dual;
!whoami
!hostname
!pwd
!ls -lart

top   prev   next  

More Complex

env|grep -i SQLPATH

export PATH=$PATH:$SQLPATH:.

1): using which, 
short coming if the .sql file don't have the -x execute permissiojn, you can not find it

which as.sql
which ts_used.sql

2): using locate
locate ts_used.sql
locate as.sql

or
locate ts_used.sql|xargs ls -l
locate as.sql|xargs ls -l

top   prev   next  

sudo su - oracle

sudo su - oracle --> don't forget the - which will bring additional information

top   prev   next  

ls command

list files in different directories
ls -la /vol*/oradata/stage/.*orcl_idx_004.dbf

top   prev   next  

awk command

#cutout the domain part of hostname
hostname|awk -F. '{ print $1 }'
hostname=`hostname|awk -F. '{ print $1 }'`

top   prev   next  

xargs command

echo /home/oraprd/auto_arch.9992|xargs rm -fr
find /home/oraprd/auto_arch.* type d -name "auto_arch.[0123456789]*" -mtime +1 -print 2>/dev/null|xargs rm -fr 

top   prev   next  

find command

10 days ago
find . -name "*.trc" -mtime +10 -exec ls -l {} \;
find . -name "*.trc" -mtime +10 -exec rm {} \;

10 minutes ago
find . -name "*.trc" -mmin +10 -exec ls -l {} \;
find . -name "*.trc" -mmin +10 -exec rm {} \;

-type and -print
find . -type f -name "*dmp.gz" -mtime +1 -print -exec ls -l {} \;
find . -type f -name "*dmp.gz" -mtime +1 -print -exec  rm -f {} \;

top   prev   next  

cut

grep -i port|cut -d: -f2|cut -d' ' -f2`

file comparison

md5sum filename1 
diff filename1 filename2

top   prev   next  

sort

sort -t: -n -k 1,1|tail -20
-t: seperator 
-n: numeric
-r: reversal
-k: sort field
-u: sort unique    combine with diff command can find and/or eliminate duplicates: diff original sort_uniqued

top   prev   next  

ps long format

ps uxww 
ps auxww|grep -i $USER 
ps -Ao pid,args -ww

top   prev   next  

swap space Information

swapon -s
/sbin/swapon -s

cat /proc/meminfo
cat /proc/cpuinfo


top   prev   next  

Send Out Email

1): mailx -s "subject line" aaa.bbb@ccc.com,ddd.eee@fff.com < file1.txt

2): send as an attachment
uuencode file1.txt file1.txt|mailx -s "subject line" aaa.bbb@ccc.com,ddd.eee@fff.com

top   prev   next  

sshd ssh

chkconfig sshd off
chkconfig sshd on

/sbin/chkconfig --list

service sshd stop
service sshd start

top   prev   next  

scp and rsync

rsync -avI --safe-links --del myusername@myhostname:/mydirectorypath/* .
rsync -avI                    myusername@myhostname:/mydirectorypath/* .

top   prev   next  

nslookup ifconfig ping

nslookup proddb002

/sbin/ifconfig

gui:
/usr/bin/system-config-network &


top   prev   next  

radom password

1. 
date +%s | sha256sum | base64 | head -c 32 ; echo

2.
openssl rand -base64 6 
openssl rand -base64 8
openssl rand -base64 12 
openssl rand -base64 16 
openssl rand -base64 32

3. 
date | md5sum

4. using a website
random password
grc password