Skip to content

Useful UNIX/LINUX commands for DBAs

Below are some of the useful UNIX/LINUX commands which we use frequently:

–LIST YOUR FILES

ls — lists your files
ls -l — lists your files in ‘long format’, which contains lots of useful information, e.g. the  exact size of the file, who owns the file and who has the right to look at it, and when it was last modified.
ls -a — lists all files, including the ones whose filenames begin in a dot, which you do  not always want to see.

 

–COMPRESSING THE FILE

gzip filename — compresses files, so that they take up much less space. There are other tools for this  purpose, too (e.g. compress), but gzip usually gives the highest compression rate. Gzip produces files with the ending ‘.gz’ appended to the original filename.
gunzip filename — uncompresses files compressed by gzip.

 

 

–PRINTING RELATED COMMANDS 

lpr filename — print to default printer
lpr -P<printer_name> filename — Use the -P option to specify the printer name if you want to  use a printer other than your default printer.
lpq — check out the default printer queue.
lpq <printer_name>— check out specific printer queue.
lprm jobnumber — remove specific job queue from the printer queue. Y

 

 

–DIRECTORY RELATED COMMANDS

mkdir dirname — make a new directory
cd dirname — change directory.  ‘cd ..‘ will get you one level  up from your current position.
pwd — tells you where you currently are.

 

–MOVING, RENAMING, AND COPYING FILES

cp file1 file2 —copy a file
mv file1 newname — move or rename a file
rm file1 [file2 ...] —remove or delete a file
rm -r dir1 [dir2...] —recursivly remove a directory and its contents

 

 

–VIEWING AND EDITING FILES

cat filename —Dump a file to the screen in ascii.
more filename— Progressively dump a file to the screen: ENTER = one line  down SPACEBAR = page down q=quit
less filename —Like more, but you can use Page-Up too. Not available on all systems.
vi filename —Edit a file using the vi editor. All UNIX systems will have vi in some form.
head filename —Show the first few lines of a file.
head -n filename —Show the first n lines of a file.tail filename —Show the last few lines of a file.
tail -n filename —Show the last n lines of a file

 

 

–FIND COMMAND

find search_path -name filename — Find command format
find . -name aaa.txt —Finds all the files named aaa.txt in the current  directory or  any subdirectory tree.
find / -name vimrc —Find all the files named ‘vimrc’ anywhere on the  system.
find /usr/local/games -name “*xpilot*” —Find all files whose names contain the string  ‘xpilot’ which  exist within the ‘/usr/local/games’ directory tree.

 

–VI EDITOR – VERY IMPORTANT TO LEARN

Start with vi <filename>

Edit modes: These keys enter editing modes and type in the text of your document.

i —Insert before current cursor position

I —Insert at beginning of current line

a —Insert (append) after current cursor position

A —Append to end of line

r —Replace 1 character

R —Replace mode

<ESC> —Terminate insertion or overwrite mode

 

Deletion of text

x —Delete single character

dd —Delete current line and put in buffer

ndd —Delete n lines (n is a number) and put them in buffer

J —Attaches the next line to the end of the current line (deletes carriage return).

 

Undo

u —Undo last command

 

Cut and Paste

yy —Yank current line into buffer

nyy —Yank n lines into buffer

p —Put the contents of the buffer after the current line

P —Put the contents of the buffer before the current line

 

Cursor Positioning

ctrl-d —Page down

ctrl-u —Page up

:n —Position cursor at line n

:$ —Position cursor at end of file

^g —Display current line number

h,j,k,l —Left,Down,Up, and Right respectivly. Your arrow keys should also work

 

String Substitution

:n1,n2:s/string1/string2/[g] —Substitute string2 for string1 on lines n1 to n2. If g is included (meaning global),
all instances of string1 on each line are substituted. If g is not included, only the first instance per matching line is substituted.

^ —matches start of line

. —matches any single character

$ —matches end of line

These and other “special characters” (like the forward slash) can be “escaped” with \

i.e to match the string “/usr/STRIM100/SOFT” say “\/usr\/STRIM100\/SOFT”

Examples:

:1,$:s/dog/cat/g —Substitute ‘cat’ for ‘dog’, every instance

for the entire file – lines 1 to $ (end of file)
:23,25:/frog/bird/ —Substitute ‘bird’ for ‘frog’ on lines 23 through 25. Only the first instance on each line is substituted.

 

Saving and quitting commands
These commands are all prefixed by pressing colon (:) and then entered in the lower left corner of the window.

Press <ESC> to exit from an editing mode.

:w —Write the current file.

:w new.file —Write the file to the name ‘new.file’.

:w! existing.file —Overwrite an existing file with the file currently being edited

:wq —Write the file and quit.

:q —Quit.

:q! —Quit with no changes.

:e filename —Open the file ‘filename’ for editing.

:set number —Turns on line numbering

:set nonumber —Turns off line numbering

 

 

–KILLING ALL LOCAL=NO PROCESS BY SINGLE COMMAND  

kill ps -ef | grep <INSTNAME> |grep LOCAL=NO| egrep -v grep | awk '{print $2}'

or

ps -ef| grep -i <INSTNAME> | grep LOCAL=NO| awk {‘print $2′}| xargs kill -9

 

 

-CUSTOMIZE UNIX COMMAND PROMPT

Put below value in .profile of user

HOSTNAME=$(uname -n)
PS1='(${?})${LOGNAME}@${HOSTNAME}:${ORACLE_SID:-“NO_SID”}: ${PWD}
$ ‘
PS2=’> ‘

 

 

–MOUNT COMMAND

mount -t nfs -o <options> server-name-or-ip-address:/<file_system_to_mount> /<file_system_to_mount>

 

–To find the number of physical CPUs:

$ cat /proc/cpuinfo | grep "^physical id" | sort | uniq | wc -l
 2

 

–To find the number of cores per CPU:

$ cat /proc/cpuinfo | grep "^cpu cores" | uniq
cpu cores : 4

The total number of processors available is the number of physical CPUs multiplied by the number of cores per CPU.

 

–To find the total number of processors:

$ cat /proc/cpuinfo | grep "^processor" | wc -l
 16

 

Brijesh Gogia

One Comment

  1. Srinivasulu Bommineni Srinivasulu Bommineni

    Excellent ..Good work. Keep it up

Leave a Reply