0% found this document useful (0 votes)
3 views68 pages

Unix Note Book

The document outlines the Software Development Life Cycle (SDLC) stages, production support team levels (L1, L2, L3), and essential skills/tools needed for production support. It also provides an overview of Unix architecture, basic commands, and file management operations in Unix. Additionally, it explains the differences between commands like 'touch' and 'cat', and how to create, remove, and manage files and directories.

Uploaded by

mohammed.shohel9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views68 pages

Unix Note Book

The document outlines the Software Development Life Cycle (SDLC) stages, production support team levels (L1, L2, L3), and essential skills/tools needed for production support. It also provides an overview of Unix architecture, basic commands, and file management operations in Unix. Additionally, it explains the differences between commands like 'touch' and 'cat', and how to create, remove, and manage files and directories.

Uploaded by

mohammed.shohel9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 68

1

UNIX

SDLC (software development life cycle)

1. Requirement analysis
2. Design
3. Development
4. Testing
5. Maintains /support

Production support / Application support

 Production support is the team works on production issues.


 The team is responsible for monitoring production activities acknowledge issues, working on
trouble shooting/ investigation and resolution process.
 The team should escalate the issues product key to other teams such as develop at admins etc.
whenever required.
 Production support team is also responsible for code development in the productions.

Levels in production support

They are three different teams in the production support

L1 team

 L1 team is the team works on the basic level of investigation they work on performing check
outs, monitoring generating reports etc.
 They basically contain only read accesses to the code.
 They work as per the documentation process [wacky pages].
 L1 team is the team who receives the issue [acknowledge] and perform the basic level of check
out and escalate the issue to L2 team if they count resolve it.

L2 team

 L2 team is the team works on trouble shooting, investigation and performing root cause analysis
[RCA].
 If any issue comes in the production L2 team should take the responsibility by performing all the
required steps.
 If an issue needed code level changes are code level investigation then L2 team can escalate the
issue to L3 team.
 L2 team contains all level of access to code such as read, write and execute [may not contains
write access

L3 team

 L3 team is nothing but development team.


 L3 team works on code level fixes and database changes as well as working on issues which
required code level investigation them must contain read, write and execute permission to the
code.
2

Skills / tools required for production support

 A production support guy should have the knowledge on any data base such as oracle (or)
Sybase (or) sql server (or) DB etc.
 Support guy should have knowledge on any operating system such as windows (or) Unix.

Scheduling tools

Autosy’s , control +m, tidal.

Ticketing tools

Bmc remedy, Service now, Jira.

Monitoring tools

ITRS, Gomej monitoring

Database converting tools

Putty, Tectia, QP tool.


3

Unix architecture

Unix is an operating system that contains set of programs and acts as a link between user and
computer.

They are different types/ flavors are available in the market

Example : redhat linux, sun solaries, hp unix , ibm aix, Ubuntu and so on

The most frequently use version of unix is redhat linux

Hardware


Kernel

Shell

User

User :

User is nothing but who execute the commands and contains set of application programs.

Shell :

Shell is command interpreter it translates the commands enter by user to the kernel in a language
understandable by kernel.

They are different types of shells in unix

Bourne shell (sh)

Bourne again shell (bash)

Csh – c shell

Korn shell – ksh

Tc sh (tc shell) –texim c shell

Note:

All the shells has the almost same features

But the way of execution differences from shell to shell

 The default login shell waries from one flavor to another flavor
 Is an example in red hat linux the different shell is bash and in sun Solaris the difficult shell is csh.

Q. what is the shell that your currently working on ?


4

Ans: bash

Q. how to change (or) switch from one shell to another shell ?

Ans: by typing the shell name

Q. how to find out which is the default shell ?

Ans: echo $SHELL

Q. how to find out current working shell ?

Ans : echo $0 (zero)

kernel

kernel is the core of unix operating system through the kernel the program gets executes through the
kernel

Hard ware

Hard ware is nothing but system resources such as cpu , ram, hard disk etc

UNIX BASIC COMMANDS

$logname : it displays current user name

$pwd : It displays present working directory path

$date : it displays system date & time

$clear : to clear the screen

$cal : It displays current month calendar

$cal2000 : it displays 2000 year calendar

$cal 06 1995 : it displays June month 1995 year

$uname : it displays operating system name

$uname -r : it displays kernal version

$hostname : it displays server name

$hostname -i : it displays server IP address

$who : it displays list of users who are connected to the server

$who am i : it displays current user information

$tty : it displays current terminal name


5

$uptime : it displays how long server is up and running, no.of users connected, and average load on the
server

$su : it is used to switch from one user account to another user account

syntax : $su - username

$whoami : it displays switched user name or child user name

$exit : to logout from current user account

$man commandname : it displays help pages of given command

Basic unix commands

who am I :

it gives the current user information such as username, terminal name, date and time, since user logged
in date.

Output :

Username terminal name date time

Prakashpts/0 2018/02/27 10:40 (:00)

Who:

It gives the information about all users who are currently logged in to the machine / server.

Output :

Username terminal name date time

Prakash tty 2018/02/27 03:20 (:0)

Prakash pts/0 2018/02/27 03:21 (:00)

logname or whoami :

It gives the user name who is currently logged in.

Out put : prakash

tty :

It gives the terminal type of the current user who is currently logged in.

Out put : terminal type of the current user

/dev/pts/0
6

Last :

It gives the login and logout information of all users

Out put : User terminal name date time

Prakashpts/0 :00 tue feb 27 03:21 still logged in

Uname -a :

It gives the information about operating system such as type of operating system release date, kernel
version, type of unix etc.

Out put :

Linux local host local domain 2.6.32-71.e16.i686 #/ smp wed sep |01:26:34 EDT 201

How to find out or how to check operating system version?

In unix there is the directory called etc which is located under home directory.

In ETC directory there a should be a file called release. The release file contains the information about
version of unix operating system.

cat /etc/*realease*

Out put :

Red hat enterprise linux server release 6.0 (stantiago)

Red hat enterprise linux server release 6.0 (Santiago)

Cpe: /0: red hat : enterprise_linux : 6 server :go.server

date :

It is used to find out the current date and time and time zone of the server.

Out put :

Wed jan 24 03:44:44 pst 2018

date +%d =24

it is used to display only date

date +%m = 01

it display month of the year

date +%y =18


7

to display the year

date +%H =03

to display the hours

date +%M =46

to display the minutes

date +%S =49

to display the seconds

Touch :

Touch is used to create zero (0) byte files as well as ‘n’ number of files at a time.

Syntax: touch file1 file2 file3 -------filen

NOTE :

When we use touch command while created any file if the file is already existed the time stamp of the
file will be changed to current time.

Q. How to change the time stamp of the file using touch command

Time stamp is nothing but the access, modified , change time of the file.

By using ‘stat’ command we can see the timestamp of the file.

Example : stat filename

NOTE :

If we change the content of the file or data of the file that is called as modified time

If we change the external file attributes such as permissions are owner, group etc that is called as
change time (only time will be change)

Touch –a file name it will be change the access time

Touch –m file name it will be used to change modify time or access time

Touch –c file name  it will be used to change access time or modify or change time

By using above command we can change the accessed, modified and changed time to the current time
(time stamp)

Q. How to change the timestamp of a file with another file time stamp?

Touch required filename -r referenced filename

Touch –r referenced filename required filename


8

CAT COMMAND :

Cat command is used to create new file as well as inserting the data while creation as well as to view the
file content

Syntax : cat >filename

------
-------
Ctrl +d to come out or exit

By using above command we can create the file as well as we can insert the data in to the file

Cat command to append the data

Syntax: cat >> file1

The above command is used to append the data to the existing file

NOTE: control +d

To quit the data prompt

Syntax: cat file1 file2 file3----filen

By using above command we can view are display multiple file data at a time

Redirection operators in unix :- (>,>>)

In unix we have two redirection operators which are used to redirect the data to another file are any file

When I use single redirection operator (>) the data of the destination file will be over written with the
source file

When we use double redirection operator (>>) the data of the destination file will be appended with the
source file data

Example:

cat file1 file2 > file3

By using above command we can copy the file1 and file2 content to file3

If file3 is already existed and if its already containing the data can be over return file1 and file2 data

If file3 is not existed a new file will be created with file1 and file2 data

Example: cat file1 file2 >> file3

By using above command we can copy the file1 and file2 content to file3

If file3 is already existed and if it’s already containing the data that can be appended file1 and file2 data
9

How to make a file as empty file using cat command

Ex: cat a > a

EX:2 :: cat >a

Ctrl+d

What is the difference between touch and cat commands

By using touch command we can create only ‘0’ (zero) byte files as well as ‘n’ number of files at a time

By using cat command we can create files along with data insert but we can create only one file at a time

By using touch command we can change the time stamp of the file

By using cat command we can view the data of the file

TO CREATE DIRECTORIES:

Mkdir:

Syntax : mkdir dir1 dir2 dir3 ------dirn

To create empty directories

Syntax: mkdir -p dir1/dir2/dir3/----/ dir n

To create directories recursively

To remove files and directories :

rm :

it is used to remove one or more files at a time

syntax : rm file1 file2 file3 ------filen

rm –f : f option is used to remove the files forcible

syntax: rm -f file1 file2 file3 ------- filen

rm –i :

it is used for asking permission before removing the file if you enter ‘Y’ the file will be removed

if you enter ‘N’ the file will be not removed

rmdir: it is used to remove only empty directories

rm -r : it is used to remove the directories recursively

ex: rm -r dir1/dir2/dir3

cp (copy):
10

It is used to copy the files and directories from one location to another location

Syntax: cp source file target file

Ex: cp file1 file2

The above command copies data from file1 to file2

If file to is already existed the data of file2 will be over return with file1 data

If file2 is not already existed the new file will be created

When we copy the files the time stamp of the files will be different

To copy the file with same time stamp we have to use ‘-p’ option

cp –p file1 file2

ex: cp -p dir1/dir2 file

cp -p raju/ram babu

The above command is used to copy the file under dir1/dir2 to the location ‘babu’ which is under
raju/ram

cp -r : it is used to copy the directories recursively

ex: cp –r dir1/dir2/dir3

kash /babu /prav

the above command is used to copy the directory called ‘prav’ which is under kash | babu

mv (move):

it is used to move the files are directories from one location to another location

syntax : mv source file target file

ex: mv file1 file2

By using above command we are moving file1 to file2

If file2 already existed is through the error

If file2 is not already existed a new file will be created

Ex: mv file1 /var /temp / prakash

The above command is used to move the file called as file1 to directory called prakash which is under
/temp

ls :
11

ls command is used to list the files and directories under the current directory

ls –r:

lt is used to list the files and directories in reverse alphabetical order

ls -l :

it is used to long list the files and directories

ls -lr:

it is used to list the files and directories in reverse alphabetical order with long listing

ls -t:

it is used to display the files and directories based on time stamp [based on changed and modification
time]

ls -tr:

it is used to display the files and directories based on descending time stamp

ls -ltr:

it is used to long list the files and directories in reverse time stamp it means the recently created files
and directories come to the bottom

ls -a :

it is used to list out the all files and directories along with hidden files

NOTE:

While creating any files if we prefix (.) dot before to the file name that file will become hidden file

Q. how to hidden files created?

Syntax : cat > .file name

Remove(rm):

rm .filename

Fields when we use ls –ltr (or) ls –l option :-

Ex : -rw-r--xr-x 1 prakash,kash 123 may 20 11:23 hari

↑ ↑ ↑ ↑ ↑ ↑

File (or) directory no.of user name,group name size of the file time stamp filename
type filepermissions links
12

In unix we have different types of files such as normal files, directory files, links, socket file, piped files
etc

_ normal file
d directory file
l  linked file
| piped file
S socket file

NOTE: r-read w-write x-execute

Regular commands with ls command while displaying the files:

*: ‘*’ is called as global search, when we use * it means it can be replaced with any number or any
value

Ex: ls -ltr abc*

It is used to long list the files and directories which starts with abc

?:- ‘?’ is used to replace for single character

Ex: ls -ltr a?c?

The above command displays the files and directories which are four in length and first letter should be
‘a’ and third letter should be ‘c’ and second and fourth can be anything

[ ]:- it is used to specific the multiple characters which is used to replace the single character

Ex: ls -ltr [axzy]*

The above command displays the files and directories which starts with letter ‘a’ or ‘x’ or ‘z’ or ‘y’

Ex: ls -ltr [xmn]?[pas]?

The above command displays the files and directories which are four in length and the first letter can be
‘x’ or ‘m’ or ‘n’ and third letter can be ‘p’ or ‘a’ or ‘s’ and second and fourth letter can be any thing

NOTE:

after the ls command if you give any filename only that file information will be displayed

For example if we used ls command the directory will be long listed but the files and sub-directories
inside the directory will be long listed

ls -ld dirname

it is used to long list the specific directory

ls -lr:

it is used to display the all files and directories in current directory as well as in all sub-directories
recursively
13

pwd (present working directory):

It is used to displayed the current or present working directory

cd: (changed directory)

syntax : cd location

ex: cd /var/tmp/prv

it is used to change the current directory location to ‘prv’ directory which is under /var/tmp

cd .. :

it is used to go to the one directory back word

ex : cd ../ ../ ../ ..

It is used to go to the four directories back word

Cd / :

It is used to go to the root directory

cd $ home (or) cd :

It is used to go to the home directory of the user

cd – : it is used to go to the previous working directory where you are previously working on

Head : it is used to display first ‘n’ number of line from the file

Syntax: head –n filename

Ex : head -5 filename

The above command is used to displays the first 5 lines of the file

NOTE :

If we don’t pass specific line number to the head command

It will displays first 10 lines of the file

Ex : head filename

Tail: it is used to displays last ‘n’ number lines of the file

Syntax: tail -n filename

Ex : tail -15 file name


14

NOTE:

Similar to head command if we don’t pass any specific line number to tail command by default it
displays last 10 lines of the file

(|) pipe: it is an operator which is used to take the output of previous command and send into next
command as a input

Ex: ls –ltr | head -5

The above command displays the only 5 files from all files in current directory

Q. How do you print lines from 12 to 17 in the file?

Head -17 filename | tail -6

permissions in unix :

Umask value is the value which is used to set the default permissions to a files and directories before it
get creates.

The default umask value is ‘022’

The ‘022’ value will be subtract from ‘666’ and ‘777’ for files and directories

To change the umask value we can use below syntax

Syntax: umask value

Ex: umask 546

chmod : it is used to change the permissions of the files and directories after created

syntax : chmod value file or directory

Ex: chmod 666 filename

chmod 555 filename

chmod ugo +x filename

chmod ugo +w filename

chmod g +x filename

chmod o +w filename

chmod ugo – x filename

Sticky bit :

Sticky bit is the option that is used to provide the security for the files and directories
15

If we create the sticky bit on the file or directory a pot from the user who created the file or directory no
one else can change or modify or move are remove the file or directory

Syntax : chmod +t file name or directory name  to create the sticky bit

chmod -t file name or directory name  to remove the sticky bit

How to identify weather sticky bit is placed on file or directory

When we long list the files using ls -l option if we see ‘T’ letter on the permission field it means sticky
bit is placed on the file or directory

Inode number:

Inode number is the address of the file given by the unix operating system to the files

Each and every inode number is ‘unique’

To check the inode number we can use below command

Syntax: ls -i file name (or) directory name

wc : it is used to count the characters ,words and lines of the file

syntax : wc filename

wc -c  only for charaters

wc -w  only for words

wc -l only for lines

Q. How to find out how many files and directories are there under current directory?

ls | wc -w

passwd : it is used to change the password of the user

cut :

it is used to extract the required number of characters and fields from the given file

Ex : cut -c1 filename  to extract the first character of the file

cut -c1,4 filename  to extract the first and fourth character of the file

cut -c2-5 filename  to extract the character form second position to fifth position

cut -c2-5,10-20 filename  to extract the characters from second position to fifth position and
form tenth position to twentieth position

cut -c2- filename  to extract the character from second position to end of the file
16

cut -d “ “ -f1 filename  to extract the first field of the file

cut -d “ “ -f1,4 filename  to extract the first and fourth field of the file

cut -d “ “ -f2-5,10-20 filename  to extract the field 2nd to 5th field and from 10th to 20th field

cut -d “ “ -f2- filename  to extract the field from 2nd field to end of the file

NOTE:

In the above command –d stands for ‘delimiter’ which is nothing but field separator, we need to pass
the field separator to the cut command when we are extracting the fields

PASTE :

Paste command is used to join the files horizontally

Syntax: paste file1 file2 -------filen

Ex : file1

abc xyz

100 200

file2

abc 123

cab 200

output : paste file1 file2

abc xyz abc 123

100 200 cab 200

SORT :

Sort command is used to sort the data based on ascending and descending order

1. sort filename:

It is used to sort the data based on ‘ascending order’

2. sort -r filename :

It is used to sort the data based on ‘descending order’

3. sort -n filename :

This option is used to sort the data properly when we have numbers in the data
17

4. sort -nr filename :

This option is used to numbers on with reverse order

5. sort -u filename :

This option is used to avoid the duplicates and print the unique values

6. sort -nu filename :

To avoid the duplicates and sort the data properly when we have numbers

7. sort -nru filename :

To avoid the duplicate and print the data in numerical reverse order

With the sort command we can sort the data based on desired number of field

sort -t ‘ ‘ -k3 filename (or) sort -t ‘ ‘ -nkr3 filename

UNIQ :

Uniq command is used to display the data by avoiding duplicate rows as well as to print only duplicate
rows and to print only the rows which are unique as well as to print the rows along with the count of the
row

NOTE :

Uniq command searches for the duplicate by checking cogitative rows, if any of the row is repeated then
it treats as a duplicate so that we always sort the data before we use unique command

Syntax : uniq filename

sort filename | uniq

sort filename | uniq -u

sort filename | uniq –d

sort filename | uniq –c

LINKS :

Links are basically used to reference the files or directories with another files and directories so that we
can achieve data referencing

They are two types of links

1. Hard link
2. Soft link or symbolic link

1.Hard link :

ln source file link or target file


18

NOTE :

If link file or target file is already existed the system through the error if not new file will be created

Hard link can be created only an files not on directories

When we create hard link the inode number for both source and target file is same

If we remove the source file still we can use the target file or vice versa

If source file is modified automatically the target file get modified or vice versa

When we long list the files using ls –i option if any of the two files having the same inode number it
means those are hard linked files

Soft link :

ln -s source file link or target file

Soft link can be created on both files and directories

When we create soft link the inode number for source and target files is different

When we remove the source file the target file is unuseful but if you remove the target file we can stay
use source file

NOTE:

When we remove the source file the target file is on larger in use but if still remain in the directory
and it became active when we create the source file again in feature.

If we modify the source file data automatically the target file data get modified (or) vice versa.

To identify the soft link when we do ls-l, if any of the file is showing as 1 letter in the beginning of the
line if means it’s a soft link.

COMMANDS TO OPEN THE FILE (or) LOG FILES:

Loge file is nothing but a file which stores the information about specific process (or) service in real time
scenarios we must use the below specific commands when we go through the log file

 Less

 More

View

 Vi
19

 tail -f

LESS :

Syntax : less filename

It is used to see the log file information page by page

Once we open the file using less command a separate

We can use scroll down and scroll up to view for the pages as well as we can hit entire to view the
further pages

So search for any lines which as the specific keyword are string we can type ‘/’ and string name
[/ keyword]

It will start searching for the lines which as the keyword in forward direction

To search for the string containing lines in back word direction we have to use

‘?’ string [? Keyword]

To go the end of the file we can use ‘G’ (shift +g) to make the dynamic information to be updated of the
end of the file we can use F (shift +f) (quit –q)

MORE :

Syntax : more filename

More is used to see the file information percentage by percentage. It means we are opening the pages
in percentage by percentage

Once we open the file at the bottom it is show like more specific percentage is to be view when 90%
were 10%

We can use the navigate keys when you open the more command

Only by hitting enter we can scroll down but we cannot scroll up

It is always better to use less command when compare the more command

VIEW :

Syntax : view filename

It is the command to see the file information when read only mode

Once we open the file using view command the file will open vi mode but we cannot make any changes
to the file, if you try to make the changes it’s through the errors such as file open the file read only

We can scroll up and down as well as we can search for the string just like as less command.
20

Tail -f :

It is used to see the dynamic information of the file dynamic information is nothing but the information
which is getting updated

Syntax : tail -f filename

The above command displays last 10 lines by default we can change the number of lines by using below
syntax tail –f file name

VI EDITOR:

Vi is an line editor that is used to manipulate the data of the file by using vi editor we can perform the
following actions

1. Creating new file


2. Modifying existing file
3. To view the file

In the vi editor we have 3 modes of operations

1. Command mode
2. Insert mode
3. Ex – command mode

1. command mode :

In this mode all the keys pressed by the user are interpreter to the editor commands

In the command mode the keys that are hit by the user cannot displayed on the screen by default when
we open the fill using vi –editor it will open it command mode

2. insert mode :

These mode permits to user to insert the data and update the data and delete the data each of these
operations can be perform only after changing over from command mode to insert mode

The following are the keys go to the insert mode

i  : to go to the insert command from command mode and to insert the data at the cursor position

I  : to insert the test at the beginning of the line irrespective of the cursor position

a  : to append the next to the cursor position

A  : append the data at the end of the line irrespective of the cursor position

3. command line :

h – move the cursor one character to the left

l - move the cursor one character to the right


21

j - move the cursor one character to the down the line

k - move the cursor one character to the move the cursor upon the line

w – move the cursor on right on the first character of the next word

b – move the cursor to the back to the first character

e – move the cursor to the end of the current word

0 (zero) – to go to the beginning of the current line

$ - to go to the end of the current line

F – scroll the window forward direction

B – scrolling window back word direction

G – to go beginning last line of the file

nG – to go to the beginning of nth line of the file

o – create new line below line and in insert mode

O – create new line above current line and insert mode

R – to replace the text position from cursor position

x – to delete the character at cursor position

nx – to delete the nth character at cursor position

X – to delete the character left to the cursor position

dw – to delete the word

ndw - to delete the ‘n’ words

dd – to delete the current line

ndd – to delete the ‘n’ lines

d0 (zero) – to delete the text from cursor position to beginning of the line

d$ - to delete the text from cursor position to end of the line

u – to undo the last command change

U – to undo all changes in current line

J – to join below line to the current line

Ex – command mode options:

: sh – temporally return to the shell for executing unix command


22

: w – to save the file

: q – to quit the file after save changes

: wq – save and quit

: q! – without save quit

: !pwd – to execute unix commands without exit from vi –editor

: nd – to delete nth line

: 10d – to delete 10 lines

: 5,10d – to move line ‘m’ after ‘p’

: 5 mo2 – to move 5th line after 2nd line

: m,n mop – to move lines m to n after ‘p’

: 3,5 mo2 – to move lines 3,5 line after 2nd line

: m cop – to copy line ‘m’ after ‘p’

: 5 co2 – to copy 5th line after 2nd line

: /pattern or /string – to search for a pattern in a line

: / tcs – to search for a string tcs in a file

: ? pattern or ? string – to search for a string in back word position

: n – repeate to last search command

: N – repeate the last search command in opposite direction

: se ic – set ignore case

: se noic – set no ignore case

: se nu – set number to the line

: se nonu – set no number

FIND & REPLACE :

: s/str1/str2 – replace first occurrence of str1 with str2 in current line

: s/str1/str2/g – replace all occurrence of str1 with str2 in current line

: m,n s/str1/str2 - replace first occurrence of str1 with str2 from line m to n.

: m,n s/str1/str2/g – replace all occurrence of str1 with str2 from line m to n

: 1,$ s/str1/str2 – replace first occurrence of str1 with str2 in each line from first line to end of the line
23

: 1,$ s/str1/str2/g – replace all occurrence of str1 with str2 in each line from first line to end of the line

: 1,. s/str1/str2 – replace first occurrence of str1 with str2 from first line to current line

: 1,. s/str1/str2/g – replace all occurrence of str1 with str2 from first line to current line

: .$ s/str1/str2 – replace first occurrence of str1 with str2 from current line to end of line

: .$ s/str1/str2/g – replace all occurrence of str1 with str2 from current line to end of line

: % s/str1/str2/g –to replace all occurrence in all lines

COPY PASTE :

yy – to copy current line

nyy – to copy ‘n’ lines

y0 (zero) – copy from cursor position of the line

y$ - copy line from cursor position of line

p –paste copied lines

TEE :

Tee command is used to throw the output of any command to any file as well as to make the output to
be display on the screen

Ex :

Cat file1 |tee file2

The above command displays the file1 content on the screen as well as it will copy the output to file2
[overwrite]

ls | tee file2

The above command displays the output of ls command on the screen as well as it copies the output to
file2 [override]

NOTE :

If file2 is not existed the data of file2 will be over written with the previous command data

By using –a option we can make the data to be appended to the given file

Ex : ls | tee -a file2

cat file1 file2 file3 | tee -a file4

We can use the tee command with multiple operations as below

cat file1 | head -10 | tee -a file4


24

TR (translate):

tr command is used to translate the character or numbers are any special characters as well as to delete
specific characters

Ex : tr ‘a-z’ ‘A-Z’ < filename (or)

tr [:lower:] [:upper:] < filename

The above command converts the small letters to capital letters in the given file

Ex : tr ‘ ‘ ‘\t’ < filename (or)

tr [:space:] ‘\t’ < filename

Q. how do you make space separated file as (,) separated file

Ans : tr ‘ ‘ ‘,’ < filename (or)

tr [:space:] ‘,’ < filename

Ex : tr ‘ ‘ ‘\n’ < filename (or)

tr [:space:] ‘\n’ < filename

The above command converts the spaces to new lines

Ex : tr -d ‘a-z’ < filename

The above command delete all alphabit (small) from a to z

Ex : tr -d ‘a’ < filename

The above command delete letter ‘a’ in the entire file

Ex : tr -d ‘0-9’ < filename

The above command delete all the digits in the files

Ex : tr -d ‘a-z’ < filename

The above command deletes all the alphabet (small and capital) from given file

Q. how to make a file which contains multiple lines as a single line file (or)

How to convert all the new lines with spaces (or)

How to join all the lines of a file

Ans : tr ‘\n’ ‘ ‘ < filename


25

INPUT AND OUTPUT REDIRECTION (I/O REDIRECTION) :

Device stream value

Keyboard standard input 0

Terminal screen standard output 1

Terminal screen standard output error 2

In every operating system there is standard input and standard output devices

In unix also we have standard input that is keyboard and standard output that is terminal screen as well
as standard output error that is also terminal screen

We can manage the (redirect) the output and error with 1 and 2 values

Ex : cat file1 > file2 (or) cat file1 1> file2

In the above example we have used single redirection operator to copy the standard output [by default
‘1’ ] if the command through any error that will be displayed on the screen

If we can’t to redirect the error output to some other file we can use number ‘2’

Ex : cat file2 2> file2

If the above command throughs any error that will be redirected to file2, if it’s throughs any standard
output that will be displayed on the screen

Ex: cat file1 > file2 2> file3

By using above command we can redirect both standard output and output error to the specific file
nothing will be displayed on the screen

Ex : cat file1 > file2 2> &1

In the above example we have used &1, it is used to specify the same filename which we have used for
standard output

diff :

diff command is used to find out the difference between two files

syntax : diff file1 file2

This command will show the changes to be made in file1 to become identical to file2

In the output if we see any keyword as ‘c’ it means change, if there is any letter as ‘a’ it means add, if
there is any letter ‘d’ it means delete

CMP :
26

cmp command is also used to find the difference between two files but it will not show the differences,
it shows only the line number where the first difference is found

Syntax : cmp file1 file2

ALIAS :
alias command is used to set the alias name for any command

With help of alias name we can create the short name for frequent and longer commands

Syntax : alias alias name = ‘any command’

Ex : alias x = ’ls –ltr’

To unset the alias name we can use below syntax

Syntax : unalias aliasname

To list out all the alias names we can use alias

MAN :

Man command is used to fine the information about any unix command in detail

Syntax : man command name

Ex : man cat

HISTORY:

History command is used to find out the commands which are entered by user in the recent post

We can execute the same command which we were already executed with help of (!) regration symbol
and sequence number of command

Ex : ! 200

SPLIT :

Split command is used to split the file into smaller files

By default split command split the file with 1000 lines files

Syntax : split -n filename

NOTE : Once we split the file the new files will be created with the name in the sequence as below

Xab

Xac

Xad

----
27

----

Xzz

NOTE : if we want to create the files with required starting of file we can use below syntax

Syntax : split -n prakash

Output : prakash ab

Prakash ac

---------

-----------

GREP COMMAND :

(Globally search for regular expression and print it)

Grep command is basically used to search for the lines which as the given string

Syntax : grep (option) ‘string’ filename

With the grep command we can do multiple operations with the below options

Options :

-c : (count)

It prints the number of lines through contains given string in the file

-i : (ignore case)

It ignores the case while searching for the string

-n : (number of lines)

It returns the lines trough’s contains given string along with the line number

-v : (verbose)

It returns the lines which does not have the given string

-w : (exact word)

To search for the lines which as the exact string

-l : (list of the files)

To search for the filenames through contains given string


28

-r : (recursive)

To search for the lines or filenames that has the given string in all files in current directory along with all
subdirectories recursively

-A : (after)

It display the line that contains specific string along with lines after the line that contain given string

-B : (before)

It display the line that contains specific string along with lines before the line that contains given string

-C : (command both A&B)

It display the line that contains specific string along with line around (before and after) the line that
contains given string

^ : it is used to search for the lines through starts with given string

$ : it is used to search for the lines through end with given string

Q. how do you display to ‘blank line’ in a file?

Ans : grep “^$” filename (or)

grep -n “^$” filename (or)

grep -v . filename

Q. how do you display all lines of the file without blank lines?

Ans : grep . filename (or)

grep -v “^$” filename

Q. how do you count the occurrences of any string in the file?

Ans : tr ‘ ‘ ‘\n’ < filename | grep -c “string”

Q. how do you count the number of occurrence of each and every string in the file?

Ans : tr ‘ ‘ ‘\n’ < file | sort | uniq -c

Grep with and condition [by using *] :

We can use grep command to search for the multiple strings in single line that is nothing but the lines
which contains multiple required strings

Syntax : grep “string1.* string2.* string3.* ----string n” filename

* is a special keyword that is used to perform the and condition


29

It matches 0 (zero) or more occurrences of previous character or string

egrep (or ) grep -E :

Syntax : egrep “string1 | string2 | string3 -----|string n “ filename (or)

grep -E “string1 | string2 | string3 -----| string n “ filename

egrep is used to search for the multiple strings we can called it as performing the ‘or’ condition

We can use all the options as we using grep command

fgrep (or) grep -F [fixed or fast grep] :

fgrep is used to search for the lines in a faster was than the grep command

It does not support the special characters such as ^, $,. (dot), *

syntax : fgrep “string” filename

grep -F “string” filename

NOTE :

We can use all the options in fgrep as we using grep command but we cannot use special characters

PROCESS :

A process is an instance of any program

When we run any commands or scripts or any programs a specific process will generated and it contains
one id which is called as process id

They are 3 types of process in Unix operating system

1. Daemon process
2. Orphan process
3. Zombie process

1. DAEMON PROCESS:

Daemon is the process which starts when we start the server or operating system

It keeps running in the background until the server or operating system shout down

2. ORPHAN PROCESS:

Orphan process is the process which child process does not have the parent process
30

Generally when we start any process that process can spawn or create or call ‘n’ number of sub
processes

The sub processes are called child processes

The main process which spawns the child process is called parent process

When every a parent process spawns the child process it should acknowledge the complication of child
process and remove the entries of child process from process table

In some abnormal conditions (network issues) the parent process may get terminated before the child
processes as there is no parent process for those processes

In this condition in it will hire the child process [orphan process] and remove the entries of child process
form process table

In orphan scenario manual action is not required

3. ZOMBIE PROCESS:

If any parent process ignore one of the child processes that child process will become as zombie process

In this scenario the details of child process will not get removed by parent process as well as from in it
process as it already as the parent process

It leads to last of memory leakage hence manual action is required to clear such kind of zombie process

Q. how to identify zombie process?

When we use ps –elf we get one column which shows process state if the process state is as ‘z’ it means
that process is zombie process

PS :

ps –u (users)

ps –e (each and every process)

ps –f (full list)

ps –l (long list)

ps aux (all users any user name)

USAGE:

ps –u username

ps –ef

ps -elf

ps aux
31

Username or user id: who initiated the process

PID: process id

PPID: parent process id

C : number of cpu’s allocated to the process

PRI: process priority

NI: nice value

Stime: time when the process started

Time: time that the resources spend on the process

cmd: command or script

%cpu: % of cpu spend on the process

% mem : %of memory spend of the process

STAT (or) S: process state

tty: terminal type from where the process is running

Process priority and nice value:

Process priority is nothing but the importance of the process and how much attendance the system
resources need to spend on process

Process priority ranges from 0 to 39

0 is the high priority

39 is the less priority

The default priority is 20

We can customize / manipulate the process priority based on the nice value

PRI = 20 + nice value

=20 + (-20)

=0

NICE VALUE:

Nice value is the user space which is used to defined the priority

Nice value ranges from -20 to 19


32

-20 is high priority

19 is less priority

Default nice value is 0 (zero)

To start any program with specific nice value we can use below syntax

Syntax: nice -n process name

We can re nice or said the nice value even after we initiated the process [for running process]

Syntax: renice -n -p pid

Ex : renice -n -20 1234

BACK GROUD PROCESS:

In general if we run any program an Unix terminal we call that process as fore ground process which is
visible to the user

We can run the process in screen back ground which helps to run the some other programs in the fore
ground , any process that is running in back ground is called as back ground process

To run the process in the back ground we can use ‘&’ operator at the end of the command / script

Ex : abc.sh &

If we use the & to run the process in back ground the completion and errors will be through on the
screen at any point to avoid this we can redirect errors and output to any file

Ex : “abc. sh > /var /temp /file 2>&1 &”

If we use ‘&” for the back ground process if the user logout from the session automatically the back
ground process will be aborted

To avoid this draw back we have one command called “nohup”

NOHUP (NO HANG UP):

Nohup command is used to run the process in server back ground not in the screen back ground

When we run the any process with nohup option we need to use ‘&’ also to run the process in screen
back ground. It means you running the process in the screen back ground as well as in the server back
ground

Ex : nohup abc .sh &

When we use nohup command automatically the output will be through to the file called nohup . out ,
we can redirect the output to any file by using redirection operator

Q. how to bring background process to foreground?

Jobs -l :
33

Jobs –l is used to display only the background process in the output we get the information of the
background process with 3 columns those are sequence numbers ,command (script) and PID of the
process

To get the background process to foreground we can use command call fg

FG (fore ground) :

We need to pass sequence number to fg command to get the process to foreground from background

Ex : fg 1 sequence number

fg 2  sequence number

NOTE: if we don’t pass any sequence number by default the latest are last background running process
will come to the foreground

Q. how to bring foreground process which is already running to background

Ans : while any process is running if you do ‘control +c’ the process will be permanently terminated

But if we use ‘control +z’ the process will be temporarily suspended and will became as temporarily

Background process:

We can use bg command to make the process to run in background with help of sequence number

Ex : bg 1

NOTE:

If we don’t pass any sequence number to bg command by default the latest suspended process will
become as background process

KILL:

Kill command is used to kill the process manually

In kill command we have different signals which are passed to kill command based on the requirement

Syntax : kill -signal pid

Ex : kill -9 1234

NOTE: By default -15 signal will be passed to kill command if we don’t specific any signal

We get the process id from the ps command then we can pass that pid to kill command

Q. how do you kill the process when you know only process name
34

Ans : By using ps -ef command we can list out all the processes in the server, we can pass that output
to grep command to search for the line

Q. what is the difference between kill -9 and kill -15?

Kill -9 and kill -15 both are used to kill the process in unix with help of pid

We can use kill -9 when we need to kill the process graceful and forcefully

When we need to kill the process normally we can use kill -15 which is called as normal kill and it is the
default signal to the kill command

Kill -9 will pass the signal called sig kill

Kill -15 will pass the signal called sig term

Q. how to identify kill and zombie process?

By using ps –elf (or) ps aux we can get the processes information along with the state of the process. If
the process state is ‘z’ it means it’s zombie process

By using below command we can list out only zombie processes

ps –elf | grep -Iw’z’

Once we have the pid of the process we can try killing it with kill -9 command sometimes this command
don’t work as the process is dead process

we can try kill -1 that is sig hub signal by passing the pid, incase if this once also doesn’t work then we
can pass ppid [parent process id ] to kill -17 (sig child) to kill all the child processes

In case if this once also doesn’t work then we need to kill the parent process

KILL ALL (or) P KILL :

Kill all or p kill is used to kill the process by process name

NOTE :

If we pass the process name to kill or p kill if any other processor which are running in the server with
the same keyword (name) we also get killed

Syntax: kill all –(signal) process name

P kill – (signal) process name

PGREP (process grep):

It is used to list out the processes based on process name

Syntax: pgrep -l ‘process name’

Output: pid process name


35

CRONTAB:

Crontab is a scheduling command which is used schedule the repetitive tasks in the server

This is called as job scheduling command

To schedule the job we can use below syntax

Syntax: crontab -e (edit)

Once we use crontab -e the list of jobs [if anything are already existed will be open in a ‘vi mode’

We can edit the old jobs and we can have new entry for the new job

One line contains six fields which is equal to one job

FIELDS:

min hours dom moy dow script

min – minutes starts from 00-59

hours – hours starts from 00-23

dom - date of month starts from 1-31

moy – month of year starts from 1-12

dow –day of week starts from 00-06

00 –Sunday

01 –Monday

02 – Tuesday

03 – Wednesday

04 – Thursday

05 – Friday

06 – Saturday

Script: any given script

NOTE:

If we specify star (*) in the any field it means such as every minute, every hour, every day and etc

If we specify two values under any field with (,) coma separated it means and

Ex : 11,15  11 minute and 15 minute

If we specify two values under any field ‘_’ separated it means range
36

Ex : 01-12  from hour 1 to hour 12

If we use */any value it means every nth value

Ex : */2  once and every 2 hours

NOTE: * -every, / -and, - - range, */n – every nth value

Example for crontab entries:

Q. Schedule a job to run at midnight 12 am every day?

Ans: min hours dom moy dow script

00 00 * * * abc.sh

Q. Schedule a job to run for every hour?

Ans min hours dom moy dow script

00 * * * * abc.sh

Q. Schedule a job which run at every Thursday at 7:10 am?

Ans: min hours dom moy dow script

10 07 * * 04 abc.sh

Q. Schedule a job which runs on every Saturday and Sunday at 8:59 am?

Ans: min hours dom moy dow scripts

59 20 * * 6,0 abc.sh

Q. Schedule a job which runs on all week days at 8:59 pm?

Ans: min hours dom moy dow scripts

59 20 * * 1-5 abc.sh

Q. Schedule a job which runs at first of every month if it is following on Monday?

Ans: 00 00 1 * 1 abc.sh

Q. Schedule a job which runs at every two hours?

Ans: 00 */2 * * * abc.sh

Q. Schedule a job to run at once in every 10 minutes?

Ans: */10 * * * * abc.sh

Q. Schedule a job which runs at once in every 30 seconds?


37

Ans: in crontab we cannot schedule a job for less than a minute as we don’t have seconds fields in
crontab

Special key words in crontab :

@yearly  00 00 1 1 * abc.sh

@monthly  00 00 1 * * abc.sh

@weekly  00 00 * * 00 abc.sh

@daily  00 00 * * * abc.sh

@hourly  00 * * * * abc.sh

@reboot  which runs after every reboot

Crontab –l :

Crontab –l is is used to list out all entries of cron jobs without opening through crontab –e

Crontab –r :

It is used to remove all entries of cron

Q. how to take back up of all cron jobs?

Crontab -l > any file

Q. how to deploy all the jobs (entries) from a file to crontab ?

Syntax: Crontab filename

NOTE: if we use above command all the existing entire in crontab will be replaced with entries in the file

Q. there is one production server which is getting my grade to new server, I want to copy all the jobs
to new server

Ans: first we can take the all entries of crontab to a file in the old server

Crontab –l > /var /temp /file

Then we can transfer that file to new server by using sftp or scp command

Once we have the file in new server we can deploy that file entries by using below command

crontab filename

FIND:

Find command is used to search for the files and directories based on the requirement as well as to get
the complete path of the file (or) directory

It is very useful command to find the files and directories when we don’t know the location of the file
38

Syntax: find directory to start searching -type f (or) –type d -requirement scenario or

for files and directories required string

by default current directory by default both

Search for the files and directories based on name:

Ex: find . –type f -name “abc*”

The above command search for the files under current directory which with abc in the name

NOTE:

In the directory field if we mention ‘/’ it means root directory

Ex: find . – type f -i name “*.sh”

The above command ignore the case while searching for the files

Search for the files and directories which are empty :

Ex: find . – type f -empty (or)

Find . –type d -empty

Display only files or directories without any requirement :

Ex: find . –type f (or)

find . –type d

Search for the files and directories based on permissions:

Ex: find . perm 755

To pass the multiple requirements

Ex: find . –type f -name “*.sh” -perm 755

Search for the files and directories based on links:

Ex: find . –links +2  more than 2

2  exactly 2

-2  less than 2

Search for the files and directories based on inode number :

Ex: find . –inum 1234

Search for the files and directories based on size :

Ex: find . –type f -size +10K


39

Search for the files based on characters, words and lines :

Ex: find . –type f -size +10C

Search for the files and directories based on access, created and modified time:

For day :

Ex: find . –type f -atime +10

-ctime 10

-mtime -10

For minutes :

Ex: find . –type f -a min +10

-c min 10

-m min -10

Q. search for the files and directories which are created 5 hours before?

Ans: find . –c min +300

XARGS & -EXEC :

Xargs and exec both are used to pass the list of arguments from previous command and send as one by
one argument to next command

Xargs :

If we use xargs the command will work in the below way

Ex: find . –c min -10 | xargs rm –r

View of execution

rm –r file1 file2 file3 -----

In the above scenario the command will working single short

Exec:

Find . –c min -10 –exec rm –r { } \;

In the above scenario the command will executed for multiple times

Q. how to kill all the processors which are named as proxy without using kill all (or) pkill

Ans: ps -ef | grep –i ‘proxy’ | cut -d ‘ ‘ -f2 | xargs kill -9 (or)


40

ps -ef |grep -i ‘proxy’ | awk -F ‘ ‘ ‘{print $2}’ | xargs kill -9

Q. remove all the files which are more than 10GB in size

Ans: find . –type f -size +10G -exec rm –rf { } \ ; (or)

Find . –type d -size +10G -exec rm –rf { } \ ;

Q. remove all the fi les which are older than 1month?

Ans: find . - type f -c time +30

MAX DAPTH AND MIN DEPTH IN FIND COMMAND:

Max depth and min depth both are used to limitize the search while searching for the files and
directories

Max depth:

Max depth is an option which is used to limitize search while searching for the files & directories

Ex: find -max depth 3 - iname 100

The above command search for the files which as the name 100 and it will search for the files only in
current directory it will not search files in sub directories as we specified me with max depth option if we
specific one it will search only in the current directory

If we specify ‘2’ it will search for current directory as well as one directory inside

If we specify ‘3’ it will search for current directory and two directories inside

Min depth:

It is also an option similar to max depth while limitize the search

The min depth criteria is exactly ‘1’ it will search for the file from current directory to maximum all levels

If it is ‘2’ it will search for the file after one directory from current directory

If it is ‘3’ search from after ‘2’ directory from current directory

Syntax: find -min depth 2 -iname 100

There are few processes that are running with name called ABC, how can you kill all those processes
at a time

Ps -ef | grep -w “abc” | cut -d “ “ -f2 | xargs kill -9

FREE COMMAND:

Free:
41

It is a very useful command to check the memory utilization of the server

The command will so the complete information such as what is total memory, what is buffer memory,
what is swap memory etc

Mail X:

Mail X command is used to send the emails from unix server to any receptions

We can keep reception in to list, in cc as well as in bcc

Syntax:

Mail X -s (subject) -c mailids -b mailids to list mailids

-s  subject

-c cc (carbon copy)

-b  bcc (blind carbon copy)

After we hit mail X command the prompt will act for the data which is as message body we can enter as
much as we want then type control +d (or) ‘.’ to exit from the command

Q. send an email with subject and message body with several receptions in to list with mail X
command?

Mail X - s ‘sample’ abc.com, xyz.com, xnt.com ------  enter

Q. send an email by passing some context (information) as a body of the email?

echo “hai I am ----“ | mailx –s “subject” to-list reception

Q. send an email with file output as body of the email?

Cat filename |mail x -s “subject” to – list (or) mail ids receptions

Q. send an email with file as attachment?

With the mail X command we have to use command called uuencode to attach any files to the email

uuencode “file to attach” “file to be attached with desired name” | mail X –s “subject”

Q. send an email with multiple files as attachment?

(uuencode file1 file1 file2 file2 -----) | mail X -s “subject” mailids

Q. send an email by attaching multiple files as well as with some context as body of the email?

(echo “I am x “, uuencode file1 file1 ; uuencode file2 file2 ; -----) | mail X -s “subject” mailids

Q. send an email with multiple files as attached as well as file content as body of the email?
42

(cat file ; uuencode file1 file1 ; uuencode file2 file2 ; ----) | mail X - s “subject” mailids

NOTE:

While sending an email to multiple receptions we need to separate them with ‘,’

df :

df command is used to check the usage of various partitions and file systems disk information (hard disk)

Ex:df

file system total space used space available space used space in% partition

NOTE:

By default df command shows information about all partitions, to get the details of specific parturitions
we need to pass that partition to df command

Ex: df /export /data /00 (or) file system

NOTE:

By default df command shows the information in blocks format, to get the information in kilobyte as
well as in human readable format we can use –k and –h

Ex: df -k /export /data /00 (or) file system

df -h /export /data /00 (or) file system

DU (directory usage):

du command is used to find the size of sub directories and files under current directory

Ex:du dirname

pwd

/var /tmp

Hk /var /tmp/a

10k/ var/ tmp

12k /var/ tmp/ b

2k m

3k n

NOTE:

To get the information in kilo byte as well as in human readable format we can use –k and –h option
43

Q. display top 10 most disk occupied files and directories [which contains more size] under current
directory?

du –k | sort –nr | head -10

TAR:

tar command is used to collect the files and directories and make as one archived file

Syntax: tar - cvf tar filename files or directories to tar

c  create

v  verbosely display

f  filename

Ex: tar - cvf file.tar file1 file2 file3 ----- filen

NOTE:

Once we create the tar file we can send that to any server as well as we can zip that tar file

Create compressed [zipped] tar file:

Syntax: tar -cvzf file.tar file1 file2 file3 ----filen

Display the files in tar file without extracting :

Syntax: tar -tvf file.tar

Transparent (or) display

Extract the files in tar file :

tar -xvf file.tar

NOTE:

By default tar command extracts the files to current directory, to extract it to specific directory we need
to use -c option

Ex: tar -xvf file.tar -c /var /temp

Extract desired files from tar file:

tar -xvf file.tar file1 file2 file3


44

ZIP:

Zip command is used to reduce the size of files and directories

Syntax: zip file.zip files to zipped

To zip all the files in specific directory:

zip file.zip /var / tmp / *

To zip all the files and sub directory files recursively

zip -r file.zip /var/tmp/*

To unzip the zipped files:

Syntax: unzip file.zip

List the files in zipped file without unzipping:

unzip -l file.zip

Unzip the specific file from the zipped file:

unzip file.zip file1 file2 ----- filen

gzip (gunzip):

gzip also an useful command to reduce the disk space by compression the files

Syntax: gzip files to compressed

Ex: gzip file1 file2 file3 ----- filen

gzip the files by keeping the original file :

by default we g zip command the files will rename as .gz extinction, we can use -c option to keep the
original file as it is

syntax: gzip -c file1 > file1.gz

To zip all the files in current directory along with all sub directories files recursively:

Syntax: gzip -r *  for all files gzip

To unzip the g zip files:

Syntax: gunzip file.gz


45

gunzip -r *  for all files unzip

NOTE:

To see the zipped file content we cannot use commands like cat, vi and view we need to use commands
called zcat and less

To search for any pattern [grep] in the gzip file we have to use zgrep

Ex: zgrep “pattern” file.gz

NETWORKING COMMAND:

Ping:

Ping command is used to find out the server status weather the server is up & running or not

Syntax: ping ipaddress (or) server name

NOTE:

When we use the above command we will get information some much of pockets and data as been
getting transmit, at the end of the output if we see 0% pocket loss it means server in up and running if
we see any %of pocket loss means server is down

Hostname:

Syntax: hostname

It is used to find out the server name or logged in

Hostname -i : (ip address)

It is used to find out the ip address of currently logged in server

Uptime:

Uptime command is used to find out the lost rebooted time in away how long the server is up and
running time from last reboot

In the output we can also see the load average of the server for last 1 minute and 15 minute

Who -b:

It show the last rebooted time of the server

nslookup:

it is used to find out the server name when we know the ip address or vice versa
46

nslookup ipaddress or hostname

Ifconfig:

It is used to find out the network configuration server

Netstat :

Netstat command is used to find out the network stratic of the server such as the machine connected to
which remove server port number and the status of the port etc

Syntax: netstat

netstat | head – 10

Netstat -a :

It is used to find out about all port which are listing as well as which are established the connection

Netstat -l :

It will display only the port which are listening

Q. how do you find out whether the port is listening (active) or not

netstat -l | grep “port number”

netstat -p:

it is used to find out the connection of remote server along with process id’s which are used to connect

File transfer commands:

In unix we have 3 different commands to transfer the files from one server to another server4

1. FTP
2. SFTP
3. SCP

FTP & SFTP: (file transfer protocol and secure file transfer protocol)

ftp and sftp both has the same syntaxes the following syntaxes can be used in any command

syntax: sftp hostname (or) ip address

username :
47

password: (or)

sftp username @ hostname or ip address

password :

Syntax to transfer the files:

Sftp > put filename

It is used to put the file in remote server

The filename what we are copying should be in the present working directory when we are not giving
absolute path

By default the file will be copied to the remote server’s home directory, if you want to copy it to some
other directions we need to specify directory

Ex: sftp > put filename /ver /tmp

For multiple files:

sftp > mput file1 file2 file3 -----filen (or)

sftp > mput file*

FOR DIRECTORIES :

sftp > put -r directory

From remote server to current server:

sftp > get filename

NOTE:

By default the file will be copy to present working directory of current server

Syntax: sftp > get filename /var/tmp

For multiple files:

sftp > get file1 file2 file3 ---- filen (or)

sftp > get file*

For directories:

sftp > get -r directory


48

We can execute several unix commands in sftp prompt without exit on without login to the current
server

To execute any command in current server form sftp prompt we have to use ‘!’ negation then
command

Ex: ! pwd

If we pass the any command without (!) negation the and will run in remote server

In sftp prompt we cannot use all commands to find out which are the command supported by sftp we
can type ‘help’

cd  to change the remote server current directory

lcd  to change the local server currant directory

ls  to list the remote server current directory files

lls  to list the local server current directory files

pwd  to find out remote servers present working directory

lpwd  to find out current servers present working directory

To exist from the sftp prompt we can use

Bye (or) ! (or)exit

Q) differences b/w ftp and sftp?

1 when we use ftp the encryption of the files may done are may not be done while transfer the file from
one server to another

2 when we use sftp the encryption must be done while transferring the files

3 the port number ftp is ‘21’

4 the port number for sftp is ‘22’

NOTE:

sftp run’s over ssh hence the port number for sftp is ‘22’
49

SCP: (secure copy)

scp is also used to copy the files or directories from one server to another

NOTE:

We need to establish any connection while using scp

Syntax: scp filename username @ ipaddress (or) hostname : /directory path to copy to the file

Ex : scp file1 prakash @ abc.prod.lava :/var/tmp

For directories :

Syntax:

Scp -r directory name user name @ ipaddress or hostname : / directory path to copy the directory

Ex: scp –r /dir1 /dir2 prakash @ abc .prod.lava : /var /tmp

Copying files and directories from remote machine to local machine:

Syntax : scp username @ hostname or ipaddress : file target file saving directory in local server

Ex: scp username @ abc .prod.lava : /var /tmp /dir1/dir2

For directories:

Scp -r username @ hostname or ip address : directory target directory saving directory in local server

Ex: scp -r prakash @ abc .prod.lava : /var /tmp /dir1 /dir2

ssh: (secure shell (or) secure socket host):

it is used to logging in the remote server from current server as well as execute command are scripts of
remote server from current server

Syntax:

ssh username @{hostname or ip address } to logging in to the remote server

password :

ex: ssh prakash @ prod .lava

password :
50

syntax: ssh user name @ hostname or ipaddress command or script

password: } to execute the remote server commands or scripts in local server

ex: ssh prakash @prod.lava abc.sh

password:

in unix we have an option called keygen to create password less protocol between two servers to create
the key we can use below syntax

syntax: ssh –keygen -t rsa [rsa=remote secure authentication]

NOTE:

when we create the keygen the following comments can be used without any pass word

sftp, scp, ssh

Generating public | private ‘rsa’ key pair

Enter file in which to save the key c |home |kash| .ssh| id –rsa) : /home /desktop (filename to where
the key has to be copy)

Enter passphrase (empty for no pass phrase ) : pass word to protect the file

Enter same pass phrase again : re return the pass word

Your identification as been saved in file we need to copy the above file in remote machine to perform
pass word less authentication

We can copy the file in remote machine using sftp or scp

SED : (string editor or stream editor)

sed command is used to replace the string with another string, print the specific lines based on the
pattern or line number as well as delete the lines based on the pattern or line number

sed command for replacement or string substitution:

By using ‘s’ option we can replace the string with another string

Syntax: sed ‘ ns /old string /new string /g ‘ file name

n  nth line number (by default all lines)

Occurrence  nth occurrence (by default 1st occurrence)


51

g  for all occurrences (g  global search)

Q. replace the string with another string in all lines of the file at 1 st occurrence

Ans: sed ‘s /old string /new string /’ filename (or)

sed ‘s /old string /new string ‘ filename

Ex: replace the string with another string only at 4th line in 2nd occurrence

Ans: sed ‘4s /old string /new string /2’ filename

Ex: replace the string with another string in all lines and all occurrences

Ans: sed ‘s /old string /new string / g’ filename

Ex: replace the string with another string from 2nd line to 7th line in all occurrences

Ans: sed ‘2,7s /old string /new string /g’ filename

Ex: replace the string with another string in every alternate line starting from first line to last line at
2nd occurrence

Ans: sed ‘ 1~2s /old string /new string /2 ‘ filename

In the above example we have used tilt (~) symbol to skip the specific number of lines before replacing
the string

The starting number specifies the line number to start with and the ending number specifies the lines to
skip

Ex: replace the string with another string starting from 2nd line and in all lines by skipping 3 lines

Ans: sed ‘2~3s / old string/ new string /g’ filename

NOTE:

All the changes which we make using sed command are temporary we need to redirect the output to
any file if required

We can use ‘-i’ option to make the permanent changes with sed command

Ex: sed -i ‘ s/old string / new string / g ‘ filename

Sed command -e option [extendable sed]:

By using –e option we can pass ‘n’ number of requirements to sed command

Ex: sed –e ‘2s /old string /new string/g  first requirement


52

ss /old / new /g  second requirement


10s / old /new /4  third requirement
12,15s /old /new/g fourth requirement
17~2 /old / new /g‘ file name last requirement

Q. how to comment all lines of the file?

In any file [specifically script file] if we add “#” at the starting of the line it means the line is commented
and that line will not get executed

sed ‘s/^/#/’ filename

starting of the line

Q. how to replace string with null value in all lines of the files and all occurrences?

sed ‘ s / old string / / g ‘ file name (or)

sed ‘s/old string / /g ‘ filename ( middle space to press tab button)

Q. how to replace a string with another string if the line started with specific string?

sed ‘s /^ old string / new string /g ‘ file name

Ex: sed ‘s/ ^ ram / Rahim / g ‘ file name

Q how can u comment all the lines of the file?

Sed ‘s / ^ # / / ‘ file name

Q. how to replace special characters using sed command?

Ans: with sed command we have special characters such as ^, $, .& /.

If we need to replace these values with any another value we have to kill the special meaning of these
characters

We can kill the special meaning by prefixing ‘\’ before to the character

Ex: sed ‘s/ \^ / p / ‘ filename

Ex: sed ‘s/ ‘ ‘ / , / ‘ filename

Q. how to stop any crontabs for execution (or) how to stop the job before execution?
53

Ans: in the crontab file we can add ‘#’ at the beginning of the line for any job, this is called as committing
the line

If we comment the line that specific job will not be executed

Q. how do you comment all jobs in the crontab?

Ans: crontab –l | sed ‘s/ ^/ # /’

The above operation is temprorary , if we want the permanent changes in all cron entries we need to
redirect the output to any file and we need that file to be deployed with crontab

Step1: crontab –l | sed ‘ s/ ^ / # / ‘ > new .cron.

Step2: crontab new .cron.

Sed command to print the lines (p):

We can use sed command to print the lines of the file based on pattern as well as based on line number

Syntax: sed -n ‘ / pattern / p ‘ file name (for pattern based)

Sed -n ‘ line number p’ file name (for line based)

In the above command ‘-n’ is used to surprise the repetitive lines to be printed, if we don’t use the ‘-n’
option the duplicate lines will get printed

Q. print the lines from 1 to 4 using sed command

Ans : sed -n ‘1,4p’ filename

Q. print the lines of the file which as the specific keyword?

Ans sed -n ‘ / keyword or string or pattern / p ‘ filename

Q. print the lines from 2nd line till end of the file by skipping 3 lines?

Ans: sed -n ‘2~3p’ filename

Q. print alternate lines of the file starting from 1st line (or) print all odd lines of the file?

Ans: sed –n ‘1~2p’ filename

Q. print all even lines of the file?

sed -n ‘2~2p’ filename

Q. print all lines of the file apart from 1 to 5?

sed -n ‘1,5!p’ file name


54

Q. print the lines of the file which starts with specific keyword using sed command?

Ans: sed -n ‘ /^ keyword / p ‘ filename

Ex: sed -n ‘/^ ram / p ‘ filename

Q. print the lines which ends with specific keyword using sed command?

Ans: sed -n ‘ /key word $ / p ‘ filename

Ex: sed -n ‘ / ram $ / p ‘ filename

Q. print the lines of the file which as only 3 characters in total length of the line?

Ans: sed -n ‘ / ^… $ / p ‘ filename

Q. print the blank lines using sed command?

Ans: sed -n ‘ /^ $ / p ‘ filename

Q. print non empty lines using sed command?

Ans: sed -n ‘/^$! / p ‘ filename

Sed to delete the lines(d):

sed ‘/ pattern / d ‘ filename (pattern based)

sed ‘ line number d ‘ filename (line number based)

-i  for permanent changes

Ex: delete the lines from 1 to 4 using sed command

Ans: sed ‘ 1,4d ‘ filename

Ex: delete the lines of the file which as the specific keyword?

Ans: sed ‘/ keyword or string or pattern /p ‘ filename

Ex: delete the lines from 2nd line till and of the file by skipping 3 lines?

Ans: sed ‘1~2d’ filename

Ex: delete all even lines of the file?

Ans: sed ‘ 2~2d ‘ filename


55

Ex: delete all lines of the file apart from 1 to 5

Ans: sed ‘ 1,5!d ‘ filename

Ex: delete the lines of the file which starts with specific keyword using sed command?

Ans: sed ‘/ ^ keyword / d ‘ filename

Ex: sed ‘/ ^ ram / d ‘ filename

Ex: delete the lines which ends with specific keyword using sed command?

Ans: sed ‘ / keyword $ / d ‘ filename

Ex: delete the lines of the file which as only 3 characters in total length of the file?

Ans: sed ‘ / ^ . . . $ / d ‘ filename

Ex: delete blank lines using sed command?

Ans: sed ‘ /^$ / d ‘ filename

Ex: delete the nonempty lines using sed command?

Ans: sed ‘ / ^$! / d ‘ filename

Awk:

Awk command is used to generate the report after the manipulation as well as after the filter, it perform
the following action

1. It scans a file line by line


2. It scans all data into fields
3. It compares the input lines with the pattern

Basic syntax:

awk [action] filename

john 1234 20 35  records  $0

alex 2586 30 45  records  $0

matrix 3282 40 65  records  $0

vinay 2796 60 89  records  $0

david 2790 64 37  records  $0

field1 field2 field3 field4


56

$1 $2 $3 $4

Ex: awk ‘ /alex / ‘ filename

Ans: alex 2586 30 45

Ex: awk ‘ {print $1} ‘ filename

Ans: john

alex

matrix

dinay

david

Ex: awk - F “ “ ‘ {print $2} ‘ filename

NOTE:

We need to specify the field separator after the ‘F’, we know need to specify if it is white space
separator

Ex: awk - F “ “ ‘ {print $1,$2} ‘ filename

Ans: 1234 20

2586 30

3282 40

3685 50

2790 37

Ex: awk ‘/ alex / {print $4} ‘ file

Ans: 45

Ex: awk ‘ /alex / ‘ file (or) awk ‘ / alex / { print $0 } ‘ file

Ans: alex 2596 30 45

Built in variables:

With awk command they are six built in variables which are used to customize the data

FS  field separator (by default white space)


57

RS  record separator (by default new line)

NF  number of fields

NR  number of records (in the sequence)

OFS  output field separator

ORS  output record separator

FS, RS, OFS, are used in the begin class with awk command

NF & NR can be used at the action part

Ex: awk ‘ {print NR, $0} ‘ file

Ans: 1 ------ record 1

2 ------ record 2

3 ------ record 3

Ex: awk ‘ /matrix / { print NR, $0 } ‘ filename

Ans: 3 matrix 3282 40 65

Ex: awk ‘{print NF} ‘ filename

Ans: 5

NOTE: It shows no. of fields in each record

Ex: awk ‘/vinay / {print NF} ‘ file

Ans: 5

Ex: awk ‘/vinay / {print NF, $0}’ file

Ans: 5 vinay 2790 37 37

Ex: awk ‘NF > 1’ filename

Ans: it will be display complete file

Ex: awk ‘NF <1 ‘ filename


58

Ans: the above command displays the blank lines

Ex: awk ‘NF <1‘ ‘ {print NR} ‘ filename

Ans: the above command displays the record number in sequence which is the blank lines

Ex: awk ‘NR >1’ filename

Ans: the above command extract all the record apart from first record

Operators with awk command:

Expression matches:

~  like

!~  not like

Arthematic operators:

+  addition

_  substraction

*  multiplication

/  division

Relational operators:

==  equal to

! =  not equal to

>  greater than


<  less than
> =  greater than equal
< =  less than equal

Logical operators:

&&  and
||  or
!  not

Ex: awk ‘$1 ~ / john / {print $0}’ file

The above command prints only the record which contains john in the first field
59

Ex: awk ‘$1 !~ / john / {print $0}’ file

The above command prints all the rows which are not having john as first field

Ex: awk ‘$4 ~89 {print $0}’ file

Ans: david 2790 67 37

Ex: awk ‘{print $4 + $5}’ file

Ans:

Ex: awk ‘{print $4 +100}’ file

Ans:

Ex: awk ‘$4 == 65’ file

Ans: matrix 2796 40 65

Ex: awk ‘$5 != 278’ file

Ans:

Ex: awk ‘$1 ~ / john / && $4 ==35’ file

Ans:

Ex: awk ‘$1 ~ /vinay / || $3 == 50’ file

Ans:

Q. how do you display the jobs which are scheduled between 9 am to 6 pm in crontab?

Ans: crontab –l | awk ‘$2 > = 09 && $2 < = 18

Awk command with multiple logics:

Ex: awk ‘ / john / - logic 1

>/ / - logic 2
>/ / - logic3
>/ / { } ‘ filename - logic4

Q. display the log information which is in between specific time to specific time

Ans: awk ‘ / 2018 : 02 : 05 /, / 2018 : 02 : 01 / ‘ file


60

$NF & $NR :

$NF & $NR both are used to fetch the last field of a row and 1 field in each and every row

Q. how do you extract last field or column of a file?

Ans: awk -F “ “ ‘{ print $NF } ‘ filename

Q. there are certain sales happened in 2017, I want to get total amount of all per change happened in
last quarter (oct to dec)

Sample file:

01 – 01 – 2017 2000

02 - 02 – 2017 2000

02 – 03 – 2017 3000

02 – 04 – 2017 5000

05 – 05 – 2017 6000

05 – 03 – 2017 6000

01 – 10 – 2017 7000

04 – 10 – 2017 7000

05 – 10 – 2017 7000

06 – 11 – 2017 7000

06 – 12 – 2017 8000

07 – 12 – 2017 8000

Output: awk ‘ BEGIN { sum = 0 : } $1 >= “ 01 – 10 – 2017 “ && $1 <= “ 31 – 12 – 2017 “

sum = $2 + sum

END { print sum }

awk scripting and functionality:

awk command contains 3 different sections

1. BEGIN
2. ACTION or BODY
3. END
61

1. BEGIN:

Begin section is the pre-processing section it must be perform before data gets processed and logic get
implemented.

In the begin section only we can declare all the user defined variables [x = 1] and system defined
variables [ RS, FS, ORS, OFS ]

2. ACTION or BODY:

In the action section we can have a logic and main processing section

It is like a loop that keeps on iterative until the last record

If a file contains 100 records the body section will be executed for 100 times

3. END:

In the ‘end’ section we can perform post processing

It contains logic to be executed after all input data gets processed

awk ‘BEGIN { }  begin action

execution { }

execution { } } action or body

end { } ‘ file name } end section

Example:

James, 30, sales, Rm, 2000, 300

Alex, 40, market, t.a, 3000, 400

John, 50, finance,t.l, 1000, 200

Matrix, 60, admin, t.l, 2000, 50

David, 70, techy, emp, 1500, 100

Out put: awk ‘BEGIN {FS = “ ,” ; OFS = “ “ ; print “ename

dnum dname design salary incentives “ }


62

{ print $1, $2, $3, $4, $5, $6 }

END { print “/n data has been process successful “ } ‘ filename

Q. how do you display last string of the file?

Ans: tail –l filename | awk –F “,” ‘{ print $NF } ‘

Q. how do you display last character of the file?

Ans: tail –l filename | awk –F “,” ‘{ print $NF } ‘ | rev | cut –c 1

TOP :

Top command is used to find out the highest cpu and memory utilization processes

By default top command shows the processes that are taking more cpu

If we press M [shift +m] the process which are taking more cpu will be refreshed and populated

The output of top command is as follows

The very first line of top command shows the information for how long the server is up & running and
how many users are connected and the load average of last 1 min, 5 min and 15 min

The second line shows the information about total tasks that are running and sleeping ,stopped and
zombie

In the third line it displays all cpus that are being utilize along with % of utilization

In the fourth line it displays the total memory [RAM] along with used, buffer /cache and free memory

In the fifth line it displays the swap memory information

After these the processer’s information will be displayed one row for each process

FREE:

Free command is used to find out the memory information in the server

It shows the details of buffer / cache memory and used memory and swap memory

By default the information will be displayed in kilo bytes

To get the information in GB’s we can –g [free –g]

To get the information by summarizing we can use –t option [t per total] [free –t],usage : free –gt
63

Free command o/p example : buffer /cache : 40G

used : 50G

free : 10G

swap : 10G

BUFFER MEMORY:

Buffer memory is the temporary holding memory for the data that is being transferred in the server
through external devices

CACHE MEMORY:

Cache is also temporary holding memory for the files/data that is being utilize frequently it is useful for
rapid access [quick access]

FREE MEMORY:

Free memory is the available memory for the processes

SWAP MEMORY:

swap memory is the additional memory that is used when the total memory got consumed

the memory which is being consumed by inactive pages will be pulled and consider as swap memory

Q. how do you resolve cpu & memory utilization issues? (or)

If the cpu or memory is very low on the server what kind of actions you will take to bring in to
normal?

Ans: if it is related to memory we can type free –gt to check the memory details, if it is related to cpu
we can type top to check the cpu’s utilizations

As cpu and memory utilize by processers are services we need to check for the processers those are
consuming more cpu’s or memory

By using top command we can pull the processers in order on utilization (or)

ps aux | sort –t “ “ –nrk4  for memory


64

ps aux | sort –t “ “ – nrk3  for cpu

With the help of PID we can get the process name ps –ef | grep “pid”

Once we have the process name we can check the schedule time of the process [in crontab or any
scheduling tool]

We can check the previous run timings either in log file or in scheduling tool

If the process is going to complete in next few minutes then we can closely monitor the memory and we
can wait for the process to be completed which will release the memory or cpu

If the process is going to run for longer time then by checking the priority critically or importance we can
take the judgement to kill the process

If the highest memory taking process is so critical to the production and we are in a situation like not to
kill the process them we can pick the second highest process and performs the all checks as above
before killing the process

If all the processers which are taking more memory are important we need to select the least priority
process among them to kill

Q. how do you resolve disk space issue (or) if the file systems or partition almost full, what kind of
actions you will take to clean it (or) make some space

Ans: Once we get the cpu utilization issue we can type top command to check the highest cpu
consumption process

We can get the process id & get the process details by using ps –elf | grep pid

Once we have process details we can check when the process been started as well as how long it has
been running

If the process is going to complete in few seconds or minutes of time we can wait and closely monitor
the process

If the process is just start and going to run for a long time then we can if the process is struk with
something then take it action to against the process if the process is still utilizing the more cpu if there is
a need to kill the process we need to check the impact and critically of the process

If the process is very critical & impact to the production if we killed it then we have to pic second highest
process and repeat the same analysis.

Q. if there is a log file which is currently being used and writing some data, but I won’t to clean up the
log file without removing the file for the disk space what kind of actions you will take?

Ans: we can make the file as empty file by using following options
65

1. cat file1 > file1


2. > file1
3. cpu /dev /null file1
4. truncate -s 0 file1

UNIX IMPORTANT QUESTIONS:

Q. What is the flavor of operating system that you are currently working on ?

Ans: Redhat linux

Q. what is version of Redhat linux currently using?

Ans: 6.1

Q. How do you find out what flavor and version of O.S that currently using?

Ans: Under ‘/etc’ directory they is a file called release. Which contains information about all details of
current running O.S

Q. What is kernel version that is running in working O.S?

Ans: Bu using command called - uname -a

Q. What are the types of shells in unix O.S?

Ans: sh – born shell

Bash - barn again shell

Csh – cshell

Tcsh - texis cshell

Q. What is shell that frequently use?

Ans: bash

Q. What is the difference b/w shells?

Q. How to change the time stamp of the file by referring some other file?

Ans: touch –r reference file required file

Q. How to make a file which contains the data as a empty file?

Ans: cat abc > abc


66

Q. Difference b/w touch and cat command?

Q. What is the absolute path?

Ans: In unix absolute path is nothing the complete path of any file or directory from the root

Q. What is the hidden file or hidden directory?

Ans: If we create any file or directory with prefix as (.) before to the file or directory name that will
become hidden file or hidden directory

Ex: touch .abc

Q. How to long list a single file?

Ans: ls –ltr filename

Q. How to long list a directory?

Ans: ls –ld directory name

Q. How to listout all the files and directories in all subdirectories recursively?

Ans: ls – R

Q. How to identify either sticky bit is set on file or directory?

Q. Display only the jobs that are schedule from 9Am to 6PM in crontab?

Ans: crontab –l | awk –F “ “ ‘$2 >= 9 && $2 <= 18’ jobname

Q. How to display only hidden files under current directory?

Ans: ls –a | grep “^\.”

Q. How to replace ‘/’ with letter ‘A’ in a file in all occurences?

Ans: sed ‘s/ \/ /A/g’ filename

Q. Latest created files how to move in ‘X’ variable ?

Ans: X = `ls –ltr | tail -1 | awk ‘{ print $NF }’`

Q. How to display last 3 days files?

Ans: Find . type –f –ctime +3

Q. How to display first and last line in one file?

Ans: sed –n ‘ 1p ; $p ’ filename


67

Q. How to display no.of files in current directory?

Ans: ls | wc –l

Q. How to display domain name ?

Ans : hostname –d

Q. how to display Ipaddress?

Ans: hostname –I

Q. display the lines those are starting with dot (.) to given file ?

Ans: sed –n ‘/^\./p’ filename

Q. How to delete last 30 days log files?

Ans: Find –ctime -30 | grep “*.log” | xargs rm –f

Q. how to display lines from 50 to 60 in agiven file?

Ans: head -60 filename | tail -10

Q. How do you find out how many times each string is existed in the file?

Ans: tr ‘ ‘ ‘\n’ < filename | sort -n | uniq -c

Q. How to identify weather hard link is created on the file or not ?

Q. How to identify weather soft link is created on the file or not ?

SQL QUESTIONS:

Q. Write a query to display top three highest salary?

Ans: select ename,sal from (select ename,sal from emp order by sal desc ) where rownum <=3:

Q. Write a query to display second highest salary?

Ans : select max(sal) from emp where sal < (select max(sal) from emp);

Q. Write a query to display 3rd highest salary?

Ans: select max(sal) from emp where sal<(select max(sal) from emp where sal < (select max(sal))) from
emp;

Q. Write a query to display 2nd least salary?

Ans: select min(sal) from emp where sal > (select min(sal) from emp);
68

Q. How to display duplicate rows from emp table?

Ans: select empno, count(*) from emp group by empno having count(*) >1;

Q. Delete duplicate records?

Ans: Delete from emp where rowid not in (select min(rowid) from emp group by empno);

You might also like