Unix Questions
Unix Questions
8. How to replace the n-th line in a file with a new line in Unix?
sed -i'' '10 d' filename # d stands for delete
sed -i'' '10 i new inserted line' filename # i stands for insert
11. How will you find which operating system your system is running on in UNIX?
uname -a
10. How do you display from the 5th character to the end of the line from a file?
cut -c 5- filename
2. Write a command to search for the file 'map' in the current directory?
find -name map -type f
4. Write a command to remove the first number on all lines that start with "@"?
sed '\,^@, s/[0-9][0-9]*//' < filename
5. How to print the file names in a directory that has the word "term"?
grep -l term *
The '-l' option make the grep command to print only the filename without printing the content of
the file. As soon as the grep command finds the pattern in a file, it prints the pattern and stops
searching other lines in the file.
7. How do you display the calendar for the month march in the year 1985?
The cal command can be used to display the current month calendar. You can pass the month
and year as arguments to display the required year, month combination calendar.
cal 03 1985
This will display the calendar for the March month and year 1985.
3. Write a command to find the sum of bytes (size of file) of all files in a directory.
ls -l | grep '^-'| awk 'BEGIN {sum=0} {sum = sum + $5} END {print sum}'
4. Write a command to print the lines which end with the word "end"?
grep 'end$' filename
The '$' symbol specifies the grep command to search for the pattern at the end of the
line.
5. Write a command to select only those lines containing "july" as a whole word?
grep -w july filename
The '-w' option makes the grep command to search for exact whole words. If the
specified pattern is found in a string, then it is not considered as a whole word. For
example: In the string "mikejulymak", the pattern "july" is found. However "july" is not a
whole word in that string.
9. Write a command to list the files in '/usr' directory that start with 'ch' and then
display the number of lines in each file?
wc -l /usr/ch*
Another way is
find /usr -name 'ch*' -type f -exec wc -l {} \;
1. How to display the processes that were run by your user name ?
ps -aef | grep <user_name>
2. Write a command to display all the files recursively with path under current
directory?
find . -depth -print
4. Write a command to display the third and fifth character from each line of a
file?
cut -c 3,5 filename
5. Write a command to print the fields from 10th to the end of the line. The
fields in the line are delimited by a comma?
cut -d',' -f10- filename
6. How to replace the word "Gun" with "Pen" in the first 100 lines of a file?
sed '1,00 s/Gun/Pen/' < filename
7. Write a Unix command to display the lines in a file that do not contain the
word "RAM"?
grep -v RAM filename
The '-v' option tells the grep to print the lines that do not contain the specified
pattern.
10. How to find out the usage of the CPU by the processes?
The top utility can be used to display the CPU usage by the processes.
1. Write a command to remove the prefix of the string ending with '/'.
The basename utility deletes any prefix ending in /. The usage is
mentioned below:
basename /usr/local/bin/file
This will display only file
3. How to replace the second occurrence of the word "bat" with "ball"
in a file?
sed 's/bat/ball/2' < filename
4. How to remove all the occurrences of the word "jhon" except the first
one in a line with in the entire file?
sed 's/jhon//2g' < filename
5. How to replace the word "lite" with "light" from 100th line to last line
in a file?
sed '100,$ s/lite/light/' < filename
6. How to list the files that are accessed 5 days ago in the current
directory?
find -atime 5 -type f
7. How to list the files that were modified 5 days ago in the current
directory?
find -mtime 5 -type f
8. How to list the files whose status is changed 5 days ago in the current
directory?
find -ctime 5 -type f
7. How is the command “$cat file2 “ different from “$cat >file2 and >> redirection operators
?
is the output redirection operator when used it overwrites while >> operator appends into the file.
9. Explain the steps that a shell follows while processing a command.
After the command line is terminated by the key, the shel goes ahead with processing the
command line in
one or more passes. The sequence is well defined and assumes the following order.
Parsing: The shell first breaks up the command line into words, using spaces and the delimiters,
unless
quoted. All consecutive occurrences of a space or tab are replaced here with a single space.
Variable evaluation: All words preceded by a $ are avaluated as variables, unless quoted or
escaped.
Command substitution: Any command surrounded by backquotes is executed by the shell
which then
replaces the standard output of the command into the command line.
Wild-card interpretation: The shell finally scans the command line for wild-cards (the characters
*, ?, [, ]).
Any word containing a wild-card is replaced by a sorted list of
filenames that match the pattern. The list of these filenames then forms the arguments to the
command.
PATH evaluation: It finally looks for the PATH variable to determine the sequence of directories it
has to
search in order to hunt for the command.
10. What difference between cmp and diff commands?
cmp - Compares two files byte by byte and displays the first mismatch
diff - tells the changes to be made to make the files identical
11. What is the use of ‘grep’ command?
‘grep’ is a pattern search command. It searches for the pattern, specified in the command line
with
appropriate option, in a file(s).
Syntax : grep
Example : grep 99mx mcafile
12. What is the difference between cat and more command?
Cat displays file contents. If the file is large the contents scroll off the screen before we view it. So
command 'more' is like a pager which displays the contents page by page.
13. Write a command to kill the last background job?
Kill $!
14. Which command is used to delete all files in the current directory and all its sub-
directories?
rm -r *
Unix Commands Interview Questions – Subscribe to FREE & Exclusive career resources at
www.jobsassist.com/CareerMag/
© JobsAssist.com (http://www.jobsassist.com/) and VyomWorld.com (http://www.vyomworld.com/) – Free
Student Resources
15. Write a command to display a file’s contents in various formats?
$od -cbd file_name
c - character, b - binary (octal), d-decimal, od=Octal Dump.
16. What will the following command do?
$ echo *
It is similar to 'ls' command and displays all the files in the current directory.
17. Is it possible to create new a file system in UNIX?
Yes, ‘mkfs’ is used to create a new file system.
18. Is it possible to restrict incoming message?
Yes, using the ‘mesg’ command.
19. What is the use of the command "ls -x chapter[1-5]"
ls stands for list; so it displays the list of the files that starts with 'chapter' with suffix '1' to '5',
chapter1,
chapter2, and so on.
20. Is ‘du’ a command? If so, what is its use?
Yes, it stands for ‘disk usage’. With the help of this command you can find the disk capacity and
free space
of the disk.
21. Is it possible to count number char, line in a file; if so, How?
Yes, wc-stands for word count.
wc -c for counting number of characters in a file.
wc -l for counting lines in a file.
22. Name the data structure used to maintain file identification?
‘inode’, each file has a separate inode and a unique inode number.
23. How many prompts are available in a UNIX system?
Two prompts, PS1 (Primary Prompt), PS2 (Secondary Prompt).
24. How does the kernel differentiate device files and ordinary files?
Unix Commands Interview Questions – Subscribe to FREE & Exclusive career resources at
www.jobsassist.com/CareerMag/
© JobsAssist.com (http://www.jobsassist.com/) and VyomWorld.com (http://www.vyomworld.com/) – Free
Student Resources
Kernel checks 'type' field in the file's inode structure.
25. How to switch to a super user status to gain privileges?
Use ‘su’ command. The system asks for password and when valid entry is made the user gains
super user
(admin) privileges.
26. What are shell variables?
Shell variables are special variables, a name-value pair created and maintained by the shell.
Example: PATH, HOME, MAIL and TERM
27. What is redirection?
Directing the flow of data to the file or from the file for input or output.
Example : ls > wc
28. How to terminate a process which is running and the specialty on command kill 0?
With the help of kill command we can terminate the process.
Syntax: kill pid
Kill 0 - kills all processes in your system except the login shell.
29. What is a pipe and give an example?
A pipe is two or more commands separated by pipe char '|'. That tells the shell to arrange for the
output of
the preceding command to be passed as input to the following command.
Example : ls -l | pr
The output for a command ls is the standard input of pr.
When a sequence of commands are combined using pipe, then it is called pipeline.
30. Explain kill() and its possible return values.
There are four possible results from this call:
‘kill()’ returns 0. This implies that a process exists with the given PID, and the system would allow
you to
send signals to it. It is system-dependent whether the process could be a zombie.
‘kill()’ returns -1, ‘errno == ESRCH’ either no process exists with the given PID, or security
enhancements
are causing the system to deny its existence. (On some systems, the process could be a
zombie.)
‘kill()’ returns -1, ‘errno == EPERM’ the system would not allow you to kill the specified process.
This means
that either the process exists (again, it could be a zombie) or draconian security enhancements
are present
(e.g. your process is not allowed to send signals to *anybody*).
‘kill()’ returns -1, with some other value of ‘errno’ you are in trouble! The most-used technique is
to assume
that success or failure with ‘EPERM’ implies that the process exists, and any other error implies
that it
doesn't.
An alternative exists, if you are writing specifically for a system (or all those systems) that provide
a ‘/proc’
filesystem: checking for the existence of ‘/proc/PID’ may work.
Unix Commands Interview Questions – Subscribe to FREE & Exclusive career resources at
www.jobsassist.com/CareerMag/
© JobsAssist.com (http://www.jobsassist.com/) and VyomWorld.com (http://www.vyomworld.com/) – Free
Student Resources
31. What is relative path and absolute path.
Absolute path : Exact path from root directory.
Relative path : Relative to the current path.
Shared Memory:
This is the fastest of all IPC schemes. The memory to be shared is mapped into the
address space of the processes (that are sharing). The speed achieved is attributed to the
fact that there is no kernel involvement. But this scheme needs synchronization.
4. What is major difference between the Historic Unix and the new BSD release of
Unix System V in terms of Memory Management?
Historic Unix uses Swapping – entire process is transferred to the main memory from the
swap device, whereas the Unix System V uses Demand Paging – only the part of the
process is moved to the main memory. Historic Unix uses one Swap Device and Unix
System V allow multiple Swap Devices.
5. In what way the Fault Handlers and the Interrupt handlers are different?
Fault handlers are also an interrupt handler with an exception that the interrupt handlers
cannot sleep. Fault handlers sleep in the context of the process that caused the memory
fault. The fault refers to the running process and no arbitrary processes are put to sleep.
8. What are states that the page can be in, after causing a page fault?
- On a swap device and not in memory,
- On the free page list in the main memory,
- In an executable file,
- Marked “demand zero”,
- Marked “demand fill”.
12. How the Kernel handles the copy on write bit of a page, when the bit is set?
In situations like, where the copy on write bit of a page is set and that page is shared by
more than one process, the Kernel allocates new page and copies the content to the new
page and the other processes retain their references to the old page. After copying the
Kernel updates the page table entry with the new page number. Then Kernel decrements
the reference count of the old pfdata table entry.
In cases like, where the copy on write bit is set and no processes are sharing the page, the
Kernel allows the physical page to be reused by the processes. By doing so, it clears the
copy on write bit and disassociates the page from its disk copy (if one exists), because
other process may share the disk copy. Then it removes the pfdata table entry from the
page-queue as the new copy of the virtual page is not on the swap device. It decrements
the swap-use count for the page and if count drops to 0, frees the swap space.
15. How the Kernel handles both the page stealer and the fault handler?
The page stealer and the fault handler thrash because of the shortage of the memory. If
the sum of the working sets of all processes is greater that the physical memory then the
fault handler will usually sleep because it cannot allocate pages for a process. This results
in the reduction of the system throughput because Kernel spends too much time in
overhead, rearranging the memory in the frantic pace.
16. What are conditions on which deadlock can occur while swapping the processes?
- All processes in the main memory are asleep.
- All ‘ready-to-run’ processes are swapped out.
- There is no space in the swap device for the new incoming process that are swapped out
of the main memory.
- There is no space in the main memory for the new incoming process.
22. What are data structures that are used for Demand Paging?
Kernel contains 4 data structures for Demand paging. They are,
- Page table entries,
- Disk block descriptors,
- Page frame data table (pfdata),
- Swap-use table.
23. What is the main goal of the Memory Management?
- It decides which process should reside in the main memory,
- Manages the parts of the virtual address space of a process which is non-core resident,
- Monitors the available main memory and periodically write the processes into the swap
device to provide more processes fit in the main memory simultaneously.
24. What scheme does the Kernel in Unix System V follow while choosing a swap
device among the multiple swap devices?
Kernel follows Round Robin scheme choosing a swap device among the multiple swap
devices in Unix System V.
30. What are links and symbolic links in UNIX file system?
A link is a second name (not a file) for a file. Links can be used to assign more than one
name to a file, but cannot be used to assign a directory more than one name or link
filenames on different computers.
Symbolic link ‘is’ a file that only contains the name of another file.Operation on the
symbolic link is directed to the file pointed by the it.Both the limitations of links are
eliminated in symbolic links.
Commands for linking files are:
Link ln filename1 filename2
Symbolic link ln -s filename1 filename2
44. What are the events done by the Kernel after a process is being swapped out from
the main memory?
When Kernel swaps the process out of the primary memory, it performs the following:
- Kernel decrements the Reference Count of each region of the process. If the reference
count becomes zero, swaps the region out of the main memory,
- Kernel allocates the space for the swapping process in the swap device,
- Kernel locks the other swapping process while the current swapping operation is going
on,
- The Kernel saves the swap address of the region in the region table.
45. Is the Process before and after the swap are the same? Give reason.
Process before swapping is residing in the primary memory in its original form. The
regions (text, data and stack) may not be occupied fully by the process, there may be few
empty slots in any of the regions and while swapping Kernel do not bother about the
empty slots while swapping the process out.
After swapping the process resides in the swap (secondary memory) device. The regions
swapped out will be present but only the occupied region slots but not the empty slots
that were present before assigning.
While swapping the process once again into the main memory, the Kernel referring to the
Process Memory Map, it assigns the main memory accordingly taking care of the empty
slots in the regions.
47. What are the entities that are swapped out of the main memory while swapping the
process out of the main memory?
All memory space occupied by the process, process’s u-area, and Kernel stack are
swapped out, theoretically.
Practically, if the process’s u-area contains the Address Translation Tables for the
process then Kernel implementations do not swap the u-area.
51. What are the processes that are not bothered by the swapper? Give Reason.
- Zombie process: They do not take any up physical memory.
- Processes locked in memories that are updating the region of the process.
- Kernel swaps only the sleeping processes rather than the ‘ready-to-run’ processes, as
they have the higher probability of being scheduled than the Sleeping processes.
53. What are the criteria for choosing a process for swapping into memory from the
swap device?
The resident time of the processes in the swap device, the priority of the processes and
the amount of time the processes had been swapped out.
54. What are the criteria for choosing a process for swapping out of the memory to the
swap device?
- The process’s memory resident time,
- Priority of the process and
- The nice value.
56. How the Kernel handles the fork() system call in traditional Unix and in the System
V Unix, while swapping?
Kernel in traditional Unix, makes the duplicate copy of the parent’s address space and
attaches it to the child’s process, while swapping. Kernel in System V Unix, manipulates
the region tables, page table, and pfdata table entries, by incrementing the reference count
of the region table of shared regions.
64. How do you create special files like named pipes and device files?
The system call mknod creates special files in the following sequence.
1. kernel assigns new inode,
2. sets the file type to indicate that the file is a pipe, directory or special file,
3. If it is a device file, it makes the other entries like major, minor device numbers.
For example:
If the device is a disk, major device number refers to the disk controller and minor device
number is the disk.
68. Brief about the initial process sequence while the system boots up.
While booting, special process called the ‘swapper’ or ‘scheduler’ is created with
Process-ID 0. The swapper manages memory allocation for processes and influences
CPU allocation. The swapper inturn creates 3 children:
- the process dispatcher,
- vhand and
- dbflush
with IDs 1,2 and 3 respectively.
This is done by executing the file /etc/init. Process dispatcher gives birth to the shell.
Unix keeps track of all the processes in an internal data structure called the Process Table
(listing command is ps -el).