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


Basic Unix Commands

1) Command : man
Use : This command is used for getting help about UNIX commands.
Syntax : man <command name>
2) Command : grep
Use : This command is used to search for a particular expression in the given input.
Syntax : grep [OPTION]… PATTERN [FILE]…
e.g. : grep <pattern> <file> —> It can be used to search from lines matching pattern from file.
grep -v <pattern> <file> –> It can be used to search for lines not matching pattern from file.
3) Command : ps
Use : This command is used to see the processes running on the system.
Syntax : ps [options]
e.g. : ps -ef —> It can used to see the all processes from all users.
ps -ef | grep <process_name> | grep -v grep —> It can be used to check whether a particular process is running or not.
ps -fu <user_name> —> It can be used to see all processes started by a particular user.
4) Command : uptime
Use :This command is used check the server/system run duration and number of users currently logged on.
Syntax : uptime
e.g. : user@machine :~$ uptime
17:16:31 up 7:34, 1 user, load average: 0.83, 0.87, 1.03
5) Command : whatis
Use : This command displays manual page descriptions.
Syntax : whatis <command_name>
e.g. : user@machine :~$ whatis firefox
firefox (1) – a free and open source web browser from Mozilla
6) Command : kill
Use : This command send a signal ( default SIGTERM ) to a process.
Syntax : kill <process_id>
e.g. : kill 123 543 2341 3453 —> Send the default signal, SIGTERM, to all those processes.
7) Command : ls
Use : This command is used to list directory contents.
Syntax : ls [OPTION]… [FILE]…
e.g. : ls -l —> It shows a log list of all files in the current directory.
8) Command : rm
Use : This command is used to remove files or directories.
Syntax : rm [OPTION]… [FILE]…
e.g. : rm <file_name> —> It removes the <file_name> .
rm -r <directory_name> –> It removes <directory_name> and all the files/directory inside it.
9) Command : xargs
Use : This command build and execute command lines from standard input.
Syntax : <some_command> | xargs <another_command >
e.g. : ls <file_name_pattern> | xargs rm —> It is used to delete all files as shown by  output of ls <file_name_pattern> .
Advertisements
Leave a comment

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: