Top 25 UNIX commands Interview Questions and Answers for Software Testing professionals
What is a typical syntax being followed when issuing commands in shell?
Typical command syntax under the UNIX shell follows the format:
Command [-argument] [-argument] [–argument] [file]
Is there a way to erase all files in the current directory, including all its sub-directories, using only one command?
Yes, that is possible. Use “rm –r *” for this purpose. The rm command is for deleting files. The –r option will erase directories and subdirectories, including files within. The asterisk represents all entries.
UNIX Interview Questions for freshers experienced :-
1. What Is Unix?
It is a portable operating system that is designed for both efficient multi-tasking and mult-user functions. Its portability allows it to run on different hardware platforms. It was written is C and lets user do processing and control under a shell.
2. What Are Filters?
The term Filter is often used to refer to any program that can take input from standard input, perform some operation on that input, and write the results to standard output. A Filter is also any program that can be used between two other programs in a pipeline.
3. What Is A Typical Syntax Being Followed When Issuing Commands In Shell?
Typical command syntax under the UNIX shell follows the format:
Command [-argument] [-argument] [–argument] [file]
4. Is There A Way To Erase All Files In The Current Directory, Including All Its Sub-directories, Using Only One Command?
Yes, that is possible. Use “rm –r *” for this purpose. The rm command is for deleting files. The –r option will erase directories and subdirectories, including files within. The asterisk represents all entries.
5. What Is The Chief Difference Between The –v And –x Option S To Set?
The –v option echoes each command before arguments and variables have been substituted for; the –x option echoes the commands after substitution has taken place.
6. What Is Kernel?
Kernel is the UNIX operating system. It is the master program that controls the computer’s resources, allotting them to different users and to different tasks. However, the kernel doesn’t deal directly with a user. Instead, it starts up a separate, interactive program, called a shell, for each user when he/she logs on.
7. What Is Shell?
A shell acts as an interface between the user and the system. As a command interpreter, the shell takes commands and sets them up for execution.
8. What Are The Key Features Of The Korn Shell?
9. What Are Some Common Shells And What Are Their Indicators?
sh – Bourne shell csh – C SHell bash – Bourne Again Shell tcsh – enhanced C Shell zsh – Z SHell ksh – Korn SHell
10. Differentiate Multiuser From Multitask?
Multiuser means that more than one person can use the computer at the same time. Multitask means that even a single user can have the computer work on more than one task or program at the same time.
11. What Is Command Substitution?
Command substitution is one of the steps being performed every time commands are processed by the shell. Commands that are enclosed in backquotes are executed by the shell. This will then replace the standard output of the command and displayed on the command line.
12. What Is A Directory?
Every file is assigned to a directory. A directory is a specialized form of file that maintains a list of all files in it.
13. What Is Inode?
An inode is an entry created on a section of the disk set aside for a file system. The inode contains nearly all there is to know about a file, which includes the location on the disk where the file starts, the size of the file, when the file was last used, when the file was last changed, what the various read, write and execute permissions are, who owns the file, and other information.
14. You Have A File Called Tonky In The Directory Honky. Later You Add New Material To Tonky. What Changes Take Place In The Directory, Inode, And File?
The directory entry is unchanged, since the name and inode number remain unchanged. In the inode file, the file size, time of last access, and time of last modification are updated. In the file itself, the new material is added.
15. Describe File Systems In Unix?
Understanding file systems in UNIX has to do with knowing how files and inodes are stored on a system. What happens is that a disk or portion of a disk is set aside to store files and the inode entries. The entire functional unit is referred to as a file system.
16. Differentiate Relative Path From Absolute Path?
Relative path refers to the path relative to the current path. Absolute path, on the other hand, refers to the exact path as referenced from the root directory.
17. Explain The Importance Of Directories In A Unix System?
Files in a directory can actually be a directory itself; it would be called a subdirectory of the original. This capability makes it possible to develop a tree-like structure of directories and files, which is crucial in maintaining an organizational scheme.
18. Briefly Describe The Shell’s Responsibilities?
19. What Are Shell Variables?
Shell variables are a combination of a name ( identifier), and an assigned value, which exist within the shell. These variables may have default values, or whose values can be manually set using the appropriate assignment command. Examples of shell variable are PATH, TERM and HOME.
20. What Are The Differences Among A System Call, A Library Function, And A Unix Command?
A system call is part of the programming for the kernel. A library function is a program that is not part of the kernel but which is available to users of the system. UNIX commands, however, are stand-alone programs; they may incorporate both system calls and library functions in their programming.
21. What Is Bash Shell?
It is a free shell designed to work on the UNIX system. Being the default shell for most UNIX-based systems, it combines features that are available both in the C and Korn Shell.
22. Enumerate some of the most commonly used network commands in UNIX
23. Differentiate cmp command from diff command.
The cmp command is used mainly to compare two files byte by byte, after which the first encountered mismatch is shown. On the other hand, the diff command is used to indicate the changes that is to be made in order to make the two files identical to each other.
24. What is the use of -l when listing a directory?
-l, which is normally used in listing command like ls, is used to show files in a long format, one file per line. Long format refers to additional information that is associated with the file, such as ownership, permissions, data and filesize.
25. What is piping?
Piping, represented by the pipe character “|”, is used to combine two or more commands together. The output of the first command serves as input the next command, and so on.
26. What is a superuser?
A superuser is a special type user who has open access to all files and commands on a system. Note that the superuser’s login is usually root, and is protected by a so-called root password.
27. How do you determine and set the path in UNIX?
Each time you enter a command, a variable named PATH or path will define in which directory the shell will search for that command. In cases wherein an error message was returned, the reason maybe that the command was not in your path, or that the command itself does not exist. You can also manually set the path using the “set path = [directory path]” command.
28. Is it possible to see information about a process while it is being executed?
Every process is uniquely identified by a process identifier. It is possible to view details and status regarding a process by using the ps command.
29. What is the standard convention being followed when naming files in UNIX?
One important rule when naming files is that characters that have special meaning are not allowed, such as * / & and %. A directory, being a special type of file, follows the same naming convention as that of files. Letters and numbers are used, along with characters like underscore and dot characters.
30. Why is it that it is not advisable to use root as the default login?
The root account is very important, and with abusive usage, can easily lead to system damage. That’s because safeguards that normally apply to user accounts are not applicable to the root account.
31. What is the use of the tee command?
The tee command does two things: one is to get data from the standard input and send it to standard output; the second is that it redirects a copy of that input data into a file that was specified.
32. Differentiate cat command from more command.
When using the cat command to display file contents, large data that does not fit on the screen would scroll off without pausing, therefore making it difficult to view. On the other hand, using the more command is more appropriate in such cases because it will display file contents one screen page at a time.
33. What is parsing?
Parsing is the process of breaking up of a command line into words. This is made possible by using delimiters and spaces. In the event that tabs or multiple spaces are part of the command, these are eventually replaced by a single space.
34. What is pid?
Pid is short for Process ID. It is used primarily to identify every process that runs on the UNIX system, whether it runs on the foreground or runs at the background. Every pid is considered unique.
35. How does the system know where one command ends and another begins?
Normally, the newline character, which is generated by the ENTER or RETURN key, acts as the signpost. However, the semicolon and the ampersand characters can also serve as command terminators.
37. What is wild-card interpretation?
When a command line contains wild-card characters such as ‘*’ or ‘?’, these are replaced by the shell with a sorted list of files whose pattern matches the input command. Wild-card characters are used to setup a list of files for processing, instead of having it specified one at a time.
38. What is the output of this command? $who | sort –logfile > newfile
In this command, the output from the command “who” becomes the input to the “sort” command. At the same time, “sort” opens logfile, arranges it together with the output from the command “who”, and places the final sorted output to the file newfile.
39. How do you switch from any user type to a super user type?
In order to switch from any user type to a superuser, you use the su command. However, you will be asked to key in the correct superuser password before full access privileges are granted to you.
40. What would be the effect of changing the value of PATH to:.:/usr/della/bin: /bin: /usr/bin
This would cause the shell to look in the /usr/della/bin directory after looking in the current directory and before looking in the /bin directory when searching for a command file.
41. Write a command that will display files in the current directory, in a colored, long format.
42. Write a command that will find all text files in a directory such that it does not contain the word “amazing” in any form (that is, it must include the words Amazing, AMAZING, or aMAZINg)
43. Write a command that will output the sorted contents of a file named IN.TXT and place the output in another file named OUT.TXT, while at the same time excluding duplicate entries.
sort IN.TXT | uniq > OUT.TXT
44. Write a command that will allow a UNIX system to shut down in 15 minutes, after which it will perform a reboot.
45. What command will change your prompt to MYPROMPT: ?
To change a prompt, we use the PS1 command, such as this:
PS1 = ‘MYPROMPT:’
46. What does this command do? cat food 1 > kitty
it redirects the output of cat food into the file kitty; the command is the same as: cat food > kitty
47. What is wrong with this interactive shell script?
echo What month is this? read $month echo $month is as good a month as any.
Initially, the question mark should be escaped (?) so that it is not interpreted as a shell metacharacter. Second, it should be read month, not read $month.
48. Write a shell script that requests the user’s age and then echoes it, along with some suitable comment.
echo Hello! What’s your age? read age echo $age! I’ll be obsolete by that age!
49. Write a script that prints out date information in this order: time, day of week, day number, month, year (sample output: 17:34:51 PDT Sun 12 Feb 2012)
set ‘date’ echo $4 $5 $1 $3 $2 $6
50. Write a script that will show the following as output:Give me a U!U!Give ma a N!N!Give me a I!I!Give me a X!X!
for i in U N I X do
51. What are the processes that are not bothered by the swapper? Give Reason.
52. What are the requirements for a swapper to work? The swapper works on the highest scheduling priority. Firstly it will look for any sleeping process, if not found then it will look for the ready-to-run process for swapping. But the major requirement for the swapper to work the ready-to-run process must be core-resident for at least 2 seconds before swapping out. And for swapping in the process must have been resided in the swap device for at least 2 seconds. If the requirement is not satisfied then the swapper will go into the wait state on that event and it is awaken once in a second by the Kernel.
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?
55. What do you mean by nice value? Nice value is the value that controls {increments or decrements} the priority of the process. This value that is returned by the nice () system call. The equation for using nice value is: Priority = (“recent CPU usage”/constant) + (base- priority) + (nice value) Only the administrator can supply the nice value. The nice () system call works for the running process only. Nice value of one process cannot affect the nice value of the other process.
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.
57. Difference between the fork() and vfork() system call? During the fork() system call the Kernel makes a copy of the parent process’s address space and attaches it to the child process. But the vfork() system call do not makes any copy of the parent’s address space, so it is faster than the fork() system call. The child process as a result of the vfork() system call executes exec() system call. The child process from vfork() system call executes in the parent’s address space (this can overwrite the parent’s data and stack ) which suspends the parent process until the child process exits.
58. What is BSS(Block Started by Symbol)? A data representation at the machine level, that has initial values when a program starts and tells about how much space the kernel allocates for the un-initialized data. Kernel initializes it to zero at run-time.
59. What is Page-Stealer process? This is the Kernel process that makes rooms for the incoming pages, by swapping the memory pages that are not the part of the working set of a process. Page-Stealer is created by the Kernel at the system initialization and invokes it throughout the lifetime of the system. Kernel locks a region when a process faults on a page in the region, so that page stealer cannot steal the page, which is being faulted in.
60. Name two paging states for a page in memory? The two paging states are:
61. What are the phases of swapping a page from the memory?
62. What is page fault? Its types? Page fault refers to the situation of not having a page in the main memory when any process references it. There are two types of page fault :
63. What is a FIFO? FIFO are otherwise called as ‘named pipes’. FIFO (first-in-first-out) is a special file which is said to be data transient. Once data is read from named pipe, it cannot be read again. Also, data can be read only in the order written. It is used in interprocess communication where a process writes to one end of the pipe (producer) and the other reads from the other end (consumer).
64. How do you create special files like named pipes and device files? The system call mknod creates special files in the following sequence.
For example: If the device is a disk, major device number refers to the disk controller and minor device number is the disk.
65. Discuss the mount and unmount system calls The privileged mount system call is used to attach a file system to a directory of another file system; the unmount system call detaches a file system. When you mount another file system on to your directory, you are essentially splicing one directory tree onto a branch in another directory tree. The first argument to mount call is the mount point, that is , a directory in the current file naming system. The second argument is the file system to mount to that point. When you insert a cdrom to your unix system’s drive, the file system in the cdrom automatically mounts to /dev/cdrom in your system.
66. How does the inode map to data block of a file? Inode has 13 block addresses. The first 10 are direct block addresses of the first 10 data blocks in the file. The 11th address points to a one-level index block. The 12th address points to a two-level (double in-direction) index block. The 13th address points to a three-level(triple in-direction)index block. This provides a very large maximum file size with efficient access to large files, but also small files are accessed directly in one disk read.
67. What is a shell? A shell is an interactive user interface to an operating system services that allows an user to enter commands as character strings or through a graphical user interface. The shell converts them to system calls to the OS or forks off a process to execute the command. System call results and other information from the OS are presented to the user through an interactive interface. Commonly used shells are sh,csh,ks etc.
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:
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).
69. What are various IDs associated with a process? Unix identifies each process with a unique integer called ProcessID. The process that executes the request for creation of a process is called the ‘parent process’ whose PID is ‘Parent Process ID’. Every process is associated with a particular user called the ‘owner’ who has privileges over the process. The identification for the user is ‘UserID’. Owner is the user who executes the process. Process also has ‘Effective User ID’ which determines the access privileges for accessing resources like files. getpid() -process id getppid() -parent process id getuid() -user id geteuid() -effective user id
70. Explain fork() system call. The `fork()’ used to create a new process from an existing process. The new process is called the child process, and the existing process is called the parent. We can tell which is which by checking the return value from `fork()’. The parent gets the child’s pid returned to him, but the child gets 0 returned to him.
71. For which kind of fault the page is checked first? The page is first checked for the validity fault, as soon as it is found that the page is invalid (valid bit is clear), the validity fault handler returns immediately, and the process incur the validity page fault. Kernel handles the validity fault and the process will incur the protection fault if any one is present.
72. In what way the protection fault handler concludes? After finishing the execution of the fault handler, it sets the modify and protection bits and clears the copy on write bit. It recalculates the process-priority and checks for signals.
73. 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.
74. Explain different types of Unix systems. The most widely used are: 1. System V (AT&T) 2. AIX (IBM) 3. BSD (Berkeley) 4. Solaris (Sun) 5. Xenix ( A PC version of Unix)
75. Explain kernal and shell. Kernal: It carries out basic operating system functions such as allocating memory, accessing files and handling communications. Shell:A shell provides the user interface to the kernal.There are 3 major shells : C-shell, Bourne shell , Korn shell
76. What is ex and vi ? ex is Unix line editor and vi is the standard Unix screen editor.
77. Which are typical system directories below the root directory? (1)/bin: contains many programs which will be executed by users (2)/etc : files used by administrator (3)/dev: hardware devices (4)/lib: system libraries (5)/usr: application software (6)/home: home directories for different systems.
78. Construct pipes to execute the following jobs?
79.Explain the following commands? $ ls > file1
$ banner hi-fi > message
$ cat par.3 par.4 par.5 >> report
$ date ; who > logfile
$ (date ; who) > logfile
80. What is the significance of the “tee” command? It reads the standard input and sends it to the standard output while redirecting a copy of what it has read to the file specified by the user.
81. What does the command “ $who | sort –logfile > newfile” do? The input from a pipe can be combined with the input from a file . The trick is to use the special symbol “-“ (a hyphen) for those commands that recognize the hyphen as std input.
In the above command the output from who becomes the std input to sort , meanwhile sort opens the file logfile, the contents of this file is sorted together with the output of who (rep by the hyphen) and the sorted output is redirected to the file newfile.
82. What does the command “$ls | wc –l > file1” do? ls becomes the input to wc which counts the number of lines it receives as input and instead of displaying this count , the value is stored in file1.
83.Which of the following commands is not a filter man , (b) cat , (c) pg , (d) head man A filter is a program which can receive a flow of data from std input, process (or filter) it and send the result to the std output.
84. 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.
85. Explain the steps that a shell follows while processing a command. After the command line is terminated by the key, the shell 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 valuated as variables, unless quoted or escaped.
Command substitution: Any command surrounded by back quotes 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.
86. 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
87. 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
88. 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.
89. Write a command to kill the last background job? Kill $!
90. Which command is used to delete all files in the current directory and all its sub-directories? rm -r *
91. 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.
92. What will the following command do? $ echo *
It is similar to ‘ls’ command and displays all the files in the current directory.
93. Is it possible to create new a file system in UNIX? Yes, ‘mkfs’ is used to create a new file system.
94. Is it possible to restrict incoming message? Yes, using the ‘mesg’ command.
95. 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.
96. 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.
97. 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.
98. Name the data structure used to maintain file identification? ‘inode’, each file has a separate inode and a unique inode number.
99. How many prompts are available in a UNIX system? Two prompts, PS1 (Primary Prompt), PS2 (Secondary Prompt).
100. How does the kernel differentiate device files and ordinary files? Kernel checks ‘type’ field in the file’s inode structure.
Q Enlist common shells with their indicators.
The following table enlists the most common shells along with their indicators;
Shell |
Indicators |
---|---|
Bourne Shell |
sh |
C Shell |
csh |
Bourne Again shell |
Bash |
Enhanced C shell |
tcsh |
Z Shell |
zsh |
Korn Shell |
ksh |
Shell table – UNIX Interview Questions – Edureka
FAQ
How do I prepare for a UNIX interview?
- Write a command to list all the links from a directory? …
- Create a read-only file in your home directory? …
- How will you find which operating system your system is running on in UNIX? …
- How will you run a process in background? …
- How do you know if a remote host is alive or not?
What are the 3 main parts of UNIX?
- Explain Unix Architecture. …
- Define a single-user system. …
- Name a few significant features of UNIX? …
- Can you write a command to erase all files in the current directory including all its sub-directories? …
- Describe a link in UNIX. …
- Describe pipes in Unix.