Unix Note Book
Unix Note Book
UNIX
1. Requirement analysis
2. Design
3. Development
4. Testing
5. Maintains /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
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
Ticketing tools
Monitoring tools
Unix architecture
Unix is an operating system that contains set of programs and acts as a link between user and
computer.
Example : redhat linux, sun solaries, hp unix , ibm aix, Ubuntu and so on
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.
Csh – c shell
Note:
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.
Ans: bash
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
$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
who am I :
it gives the current user information such as username, terminal name, date and time, since user logged
in date.
Output :
Who:
It gives the information about all users who are currently logged in to the machine / server.
Output :
logname or whoami :
tty :
It gives the terminal type of the current user who is currently logged in.
/dev/pts/0
6
Last :
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
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 :
date :
It is used to find out the current date and time and time zone of the server.
Out put :
date +%m = 01
Touch :
Touch is used to create zero (0) byte files as well as ‘n’ number of files at a time.
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.
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 –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?
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
------
-------
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
The above command is used to append the data to the existing file
NOTE: control +d
By using above command we can view are display multiple file data at a time
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:
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
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
Ctrl+d
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
TO CREATE DIRECTORIES:
Mkdir:
rm :
rm –i :
it is used for asking permission before removing the file if you enter ‘Y’ the file will be removed
ex: rm -r dir1/dir2/dir3
cp (copy):
10
It is used to copy the files and directories from one location to another location
If file to is already existed the data of file2 will be over return with file1 data
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
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
ex: cp –r dir1/dir2/dir3
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
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:
ls -l :
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
Remove(rm):
rm .filename
↑ ↑ ↑ ↑ ↑ ↑
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
*: ‘*’ is called as global search, when we use * it means it can be replaced with any number or any
value
It is used to long list the files and directories which starts with abc
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
The above command displays the files and directories which starts with letter ‘a’ or ‘x’ or ‘z’ or ‘y’
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
ls -lr:
it is used to display the all files and directories in current directory as well as in all sub-directories
recursively
13
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 .. :
Cd / :
cd $ home (or) cd :
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
Ex : head -5 filename
The above command is used to displays the first 5 lines of the file
NOTE :
Ex : head filename
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
The above command displays the only 5 files from all files in current directory
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 ‘022’ value will be subtract from ‘666’ and ‘777’ for files and directories
chmod : it is used to change the permissions of the files and directories after created
chmod g +x filename
chmod o +w 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
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
syntax : wc filename
Q. How to find out how many files and directories are there under current directory?
ls | wc -w
cut :
it is used to extract the required number of characters and fields from the given 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,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 :
Ex : file1
abc xyz
100 200
file2
abc 123
cab 200
SORT :
Sort command is used to sort the data based on ascending and descending order
1. sort filename:
2. sort -r filename :
3. sort -n filename :
This option is used to sort the data properly when we have numbers in the data
17
5. sort -u filename :
This option is used to avoid the duplicates and print the unique values
To avoid the duplicates and sort the data properly when we have numbers
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
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
LINKS :
Links are basically used to reference the files or directories with another files and directories so that we
can achieve data referencing
1. Hard link
2. Soft link or symbolic link
1.Hard link :
NOTE :
If link file or target file is already existed the system through the error if not new file will be created
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 :
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.
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 :
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
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 :
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
It is always better to use less command when compare the more command
VIEW :
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
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. 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
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 : append the data at the end of the line irrespective of the cursor position
3. command 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
d0 (zero) – to delete the text from cursor position to beginning of the 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
COPY PASTE :
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 :
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
TR (translate):
tr command is used to translate the character or numbers are any special characters as well as to delete
specific characters
The above command converts the small letters to capital letters in the given file
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)
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
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’
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
By using above command we can redirect both standard output and output error to the specific file
nothing will be displayed on the screen
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
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
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
MAN :
Man command is used to fine the information about any unix command in detail
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 :
By default split command split the file with 1000 lines files
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
Output : prakash ab
Prakash ac
---------
-----------
GREP COMMAND :
Grep command is basically used to search for the lines which as the given string
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)
-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)
-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
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
grep -v . filename
Q. how do you display all lines of the file without blank lines?
Q. how do you count the number of occurrence of each and every string in the file?
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
egrep is used to search for the multiple strings we can called it as performing the ‘or’ condition
fgrep is used to search for the lines in a faster was than the grep command
NOTE :
We can use all the options in fgrep as we using grep command but we cannot use special characters
PROCESS :
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
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 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
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
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 –f (full list)
ps –l (long list)
USAGE:
ps –u username
ps –ef
ps -elf
ps aux
31
PID: process id
Process priority is nothing but the importance of the process and how much attendance the system
resources need to spend on process
We can customize / manipulate the process priority based on the nice value
=20 + (-20)
=0
NICE VALUE:
Nice value is the user space which is used to defined the priority
19 is less priority
To start any program with specific nice value we can use below syntax
We can re nice or said the nice value even after we initiated the process [for running 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
If we use ‘&” for the back ground process if the user logout from the session automatically the back
ground process will be aborted
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
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
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
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
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:
In kill command we have different signals which are passed to kill command based on the requirement
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
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
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
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
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
CRONTAB:
Crontab is a scheduling command which is used schedule the repetitive tasks in the server
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
FIELDS:
00 –Sunday
01 –Monday
02 – Tuesday
03 – Wednesday
04 – Thursday
05 – Friday
06 – Saturday
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
If we specify two values under any field ‘_’ separated it means range
36
00 00 * * * abc.sh
00 * * * * abc.sh
10 07 * * 04 abc.sh
Q. Schedule a job which runs on every Saturday and Sunday at 8:59 am?
59 20 * * 6,0 abc.sh
59 20 * * 1-5 abc.sh
Ans: 00 00 1 * 1 abc.sh
Ans: in crontab we cannot schedule a job for less than a minute as we don’t have seconds fields 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
Crontab –l :
Crontab –l is is used to list out all entries of cron jobs without opening through crontab –e
Crontab –r :
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
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
The above command search for the files under current directory which with abc in the name
NOTE:
The above command ignore the case while searching for the files
find . –type d
2 exactly 2
-2 less than 2
Search for the files and directories based on access, created and modified time:
For day :
-ctime 10
-mtime -10
For minutes :
-c min 10
-m min -10
Q. search for the files and directories which are created 5 hours before?
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 :
View of execution
Exec:
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
Q. remove all the files which are more than 10GB in size
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
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:
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
There are few processes that are running with name called ABC, how can you kill all those processes
at a time
FREE COMMAND:
Free:
41
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
Syntax:
-s subject
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?
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 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
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
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
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?
TAR:
tar command is used to collect the files and directories and make as one archived file
c create
v verbosely display
f filename
NOTE:
Once we create the tar file we can send that to any server as well as we can zip that tar file
NOTE:
By default tar command extracts the files to current directory, to extract it to specific directory we need
to use -c option
ZIP:
unzip -l file.zip
gzip (gunzip):
gzip also an useful command to reduce the disk space by compression the files
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
To zip all the files in current directory along with all sub directories files recursively:
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
NETWORKING COMMAND:
Ping:
Ping command is used to find out the server status weather the server is up & running or not
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
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:
nslookup:
it is used to find out the server name when we know the ip address or vice versa
46
Ifconfig:
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 :
Q. how do you find out whether the port is listening (active) or not
netstat -p:
it is used to find out the connection of remote server along with process id’s which are used to connect
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
username :
47
password: (or)
password :
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
FOR DIRECTORIES :
NOTE:
By default the file will be copy to present working directory of current server
For directories:
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’
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
NOTE:
sftp run’s over ssh hence the port number for sftp is ‘22’
49
scp is also used to copy the files or directories from one server to another
NOTE:
Syntax: scp filename username @ ipaddress (or) hostname : /directory path to copy to the file
For directories :
Syntax:
Scp -r directory name user name @ ipaddress or hostname : / directory path to copy the directory
Syntax : scp username @ hostname or ipaddress : file target file saving directory in local server
For directories:
Scp -r username @ hostname or ip address : directory target directory saving directory in local server
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:
password :
password :
50
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
NOTE:
when we create the keygen the following comments can be used without any pass word
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
Your identification as been saved in file we need to copy the above file in remote machine to perform
pass word less authentication
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
By using ‘s’ option we can replace the string with another string
Q. replace the string with another string in all lines of the file at 1 st occurrence
Ex: replace the string with another string only at 4th line in 2nd occurrence
Ex: replace the string with another string in all lines and all occurrences
Ex: replace the string with another string from 2nd line to 7th line in all occurrences
Ex: replace the string with another string in every alternate line starting from first line to last line at
2nd occurrence
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
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
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
Q. how to replace string with null value in all lines of the files and all occurrences?
Q. how to replace a string with another string if the line started with specific string?
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
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
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
We can use sed command to print the lines of the file based on pattern as well as based on line number
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 2nd line till end of the file by skipping 3 lines?
Q. print alternate lines of the file starting from 1st line (or) print all odd lines of the file?
Q. print the lines of the file which starts with specific keyword using sed command?
Q. print the lines which ends with specific keyword using sed command?
Q. print the lines of the file which as only 3 characters in total length of the line?
Ex: delete the lines of the file which as the specific keyword?
Ex: delete the lines from 2nd line till and of the file by skipping 3 lines?
Ex: delete the lines of the file which starts with specific keyword using sed command?
Ex: delete the lines which ends with specific keyword using sed command?
Ex: delete the lines of the file which as only 3 characters in total length of the file?
Awk:
Awk command is used to generate the report after the manipulation as well as after the filter, it perform
the following action
Basic syntax:
$1 $2 $3 $4
Ans: john
alex
matrix
dinay
david
NOTE:
We need to specify the field separator after the ‘F’, we know need to specify if it is white space
separator
Ans: 1234 20
2586 30
3282 40
3685 50
2790 37
Ans: 45
Built in variables:
With awk command they are six built in variables which are used to customize the data
NF number of fields
FS, RS, OFS, are used in the begin class with awk command
2 ------ record 2
3 ------ record 3
Ans: 5
Ans: 5
Ans: the above command displays the record number in sequence which is the blank lines
Ans: the above command extract all the record apart from first record
Expression matches:
~ like
!~ not like
Arthematic operators:
+ addition
_ substraction
* multiplication
/ division
Relational operators:
== equal to
! = not equal to
Logical operators:
&& and
|| or
! not
The above command prints only the record which contains john in the first field
59
The above command prints all the rows which are not having john as first field
Ans:
Ans:
Ans:
Ans:
Ans:
Q. how do you display the jobs which are scheduled between 9 am to 6 pm in crontab?
>/ / - logic 2
>/ / - logic3
>/ / { } ‘ filename - logic4
Q. display the log information which is in between specific time to specific time
$NF & $NR both are used to fetch the last field of a row and 1 field in each and every row
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
sum = $2 + sum
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
If a file contains 100 records the body section will be executed for 100 times
3. END:
execution { }
Example:
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 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
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
To get the information by summarizing we can use –t option [t per total] [free –t],usage : free –gt
63
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:
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
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)
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
Q. What is the flavor of operating system that you are currently working on ?
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
Csh – cshell
Ans: bash
Q. How to change the time stamp of the file by referring some other file?
Ans: In unix absolute path is nothing the complete path of any file or directory from the root
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
Q. How to listout all the files and directories in all subdirectories recursively?
Ans: ls – R
Q. Display only the jobs that are schedule from 9Am to 6PM in crontab?
Ans: ls | wc –l
Ans : hostname –d
Ans: hostname –I
Q. display the lines those are starting with dot (.) to given file ?
Q. How do you find out how many times each string is existed in the file?
SQL QUESTIONS:
Ans: select ename,sal from (select ename,sal from emp order by sal desc ) where rownum <=3:
Ans : select max(sal) from emp where sal < (select max(sal) from emp);
Ans: select max(sal) from emp where sal<(select max(sal) from emp where sal < (select max(sal))) from
emp;
Ans: select min(sal) from emp where sal > (select min(sal) from emp);
68
Ans: select empno, count(*) from emp group by empno having count(*) >1;
Ans: Delete from emp where rowid not in (select min(rowid) from emp group by empno);