Search This Blog

Tuesday 19 April 2016

Linux/Unix Interview Questions

1) What is Linux?
Linux is an operating system based on UNIX, and was first introduced by Linus Torvalds. It is based on the Linux Kernel, and can run on different hardware platforms manufactured by Intel, MIPS, HP, IBM, SPARC and Motorola. Another popular element in Linux is its mascot, a penguin figure named Tux.
2) What is the difference between UNIX and LINUX?
Unix originally began as a propriety operating system from Bell Laboratories, which later on spawned into different commercial versions. On the other hand, Linux is free, open source and intended as a non-propriety operating system for the masses.
3) What is BASH?
BASH is short for Bourne Again SHell. It was written by Steve Bourne as a replacement to the original Bourne Shell (represented by /bin/sh). It combines all the features from the original version of Bourne Shell, plus additional functions to make it easier and more convenient to use. It has since been adapted as the default shell for most systems running Linux.
4) What is Linux Kernel?

The Linux Kernel is a low-level systems software whose main role is to manage hardware resources for the user. It is also used to provide an interface for user-level interaction.
5) What is LILO?
LILO is a boot loader for Linux. It is used mainly to load the Linux operating system into main memory so that it can begin its operations.
6) What is a swap space?
A swap space is a certain amount of space used by Linux to temporarily hold some programs that are running concurrently. This happens when RAM does not have enough memory to hold all programs that are executing.
7) What is the advantage of open source?
Open source allows you to distribute your software, including source codes freely to anyone who is interested. People would then be able to add features and even debug and correct errors that are in the source code. They can even make it run better, and then redistribute these enhanced source code freely again. This eventually benefits everyone in the community.
8 ) What are the basic components of Linux?
Just like any other typical operating system, Linux has all of these components: kernel, shells and GUIs, system utilities, and application program. What makes Linux advantageous over other operating system is that every aspect comes with additional features and all codes for these are downloadable for free.
9) Does it help for a Linux system to have multiple desktop environments installed?
In general, one desktop environment, like KDE or Gnome, is good enough to operate without issues. It’s all a matter of preference for the user, although the system allows switching from one environment to another. Some programs will work on one environment and not work on the other, so it could also be considered a factor in selecting which environment to use.
10) What is the basic difference between BASH and DOS?
The key differences between the BASH and DOS console lies in 3 areas:
– BASH commands are case sensitive while DOS commands are not;
– under BASH, / character is a directory separator and acts as an escape character. Under DOS, / serves as a command argument delimiter and is the directory separator
– DOS follows a convention in naming files, which is 8 character file name followed by a dot and 3 character for the extension. BASH follows no such convention.
11) What is the importance of the GNU project?
This so-called Free software movement allows several advantages, such as the freedom to run programs for any purpose and freedom to study and modify a program to your needs. It also allows you to redistribute copies of a software to other people, as well as freedom to improve software and have it released to the public.
12) Describe the root account.
The root account is like a systems administrator account, and allows you full control of the system. Here you can create and maintain user accounts, assigning different permissions for each account. It is the default account every time you install Linux.
13) What is CLI?
CLI is short for Command Line Interface. This interface allows user to type declarative commands to instruct the computer to perform operations. CLI offers an advantage in that there is greater flexibility. However, other users who are already accustom with using GUI find it difficult to remember commands including attributes that come with it.
14) What is GUI?
GUI, or Graphical User Interface, makes use of images and icons that users click and manipulate as a way of communicating with the computer. Instead of having to remember and type commands, the use of graphical elements makes it easier to interact with the system, as well as adding more attraction through images, icons and colors.
15) How do you open a command prompt when issuing a command?
To open the default shell (which is where the command prompt can be found), press Ctrl-Alt-F1. This will provide a command line interface (CLI) from which you can run commands as needed.
16) How can you find out how much memory Linux is using?
From a command shell, use the “concatenate” command: cat /proc/meminfo for memory usage information. You should see a line starting something like: Mem: 64655360, etc. This is the total memory Linux thinks it has available to use.
17) What is typical size for a swap partition under a Linux system?
The preferred size for a swap partition is twice the amount of physical memory available on the system. If this is not possible, then the minimum size should be the same as the amount of memory installed.
18) What are symbolic links?
Symbolic links act similarly to shortcuts in Windows. Such links point to programs, files or directories. It also allows you instant access to it without having to go directly to the entire pathname.
19) Does the Ctrl+Alt+Del key combination work on Linux?
Yes, it does. Just like Windows, you can use this key combination to perform a system restart. One difference is that you won’t be getting any confirmation message and therefore, reboot is immediate.
20) How do you refer to the parallel port where devices such as printers are connected?
Whereas under Windows you refer to the parallel port as the LPT port, under Linux you refer to it as /dev/lp . LPT1, LPT2 and LPT3 would therefore be referred to as /dev/lp0, /dev/lp1, or /dev/lp2 under Linux.
21) Are drives such as harddrive and floppy drives represented with drive letters?
No. In Linux, each drive and device has different designations. For example, floppy drives are referred to as /dev/fd0 and /dev/fd1. IDE/EIDE hard drives are referred to as /dev/hda, /dev/hdb, /dev/hdc, and so forth.
22) How do you change permissions under Linux?
Assuming you are the system administrator or the owner of a file or directory, you can grant permission using the chmod command. Use + symbol to add permission or – symbol to deny permission, along with any of the following letters: u (user), g (group), o (others), a (all), r (read), w (write) and x (execute). For example the command chmod go+rw FILE1.TXT grants read and write access to the file FILE1.TXT, which is assigned to groups and others.
23) In Linux, what names are assigned to the different serial ports?
Serial ports are identified as /dev/ttyS0 to /dev/ttyS7. These are the equivalent names of COM1 to COM8 in Windows.
24) How do you access partitions under Linux?
Linux assigns numbers at the end of the drive identifier. For example, if the first IDE hard drive had three primary partitions, they would be named/numbered, /dev/hda1, /dev/hda2 and /dev/hda3.

25) What are hard links?
Hard links point directly to the physical file on disk, and not on the path name. This means that if you rename or move the original file, the link will not break, since the link is for the file itself, not the path where the file is located.
26) What is the maximum length for a filename under Linux?
Any filename can have a maximum of 255 characters. This limit does not include the path name, so therefore the entire pathname and filename could well exceed 255 characters.
27)What are filenames that are preceded by a dot?
In general, filenames that are preceded by a dot are hidden files. These files can be configuration files that hold important data or setup info. Setting these files as hidden makes it less likely to be accidentally deleted.
28) Explain virtual desktop.
This serves as an alternative to minimizing and maximizing different windows on the current desktop. Using virtual desktops, each desktop is a clean slate where you can open one or more programs. Rather than minimizing/restoring all those programs as needed, you can simply shuffle between virtual desktops with programs intact in each one.
29) How do you share a program across different virtual desktops under Linux?
To share a program across different virtual desktops, in the upper left-hand corner of a program window look for an icon that looks like a pushpin. Pressing this button will “pin” that application in place, making it appear in all virtual desktops, in the same position onscreen.
30) What does a nameless (empty) directory represent?
This empty directory name serves as the nameless base of the Linux file system. This serves as an attachment for all other directories, files, drives and devices.
31) What is the pwd command?
The pwd command is short for print working directory command. It’s counterpart in DOS is the cd command, and is used to display the current location in the directory tree.
32) What are daemons?
Daemons are services that provide several functions that may not be available under the base operating system. Its main task is to listen for service request and at the same time to act on these requests. After the service is done, it is then disconnected and waits for further requests.
33) How do you switch from one desktop environment to another, such as switching from KDE to Gnome?
Assuming you have these two environments installed, just log out from the graphical interface. Then at the Log in screen, type your login ID and password and choose which session type you wish to load. This choice will remain your default until you change it to something else.
34) What are the kinds of permissions under Linux?
There are 3 kinds of permissions under Linux:
– Read: users may read the files or list the directory
– Write: users may write to the file of new files to the directory
– Execute: users may run the file or lookup a specific file within a directory
35) How does case sensitivity affect the way you use commands?
When we talk about case sensitivity, commands are considered identical only if every character is encoded as is, including lowercase and uppercase letters. This means that CD, cd and Cd are three different commands. Entering a command using uppercase letters, where it should be in lowercase, will produce different outputs.
36) What are environmental variables?
Environmental variables are global settings that control the shell’s function as well as that of other Linux programs. Another common term for environmental variables is global shell variables.
37) What are the different modes when using vi editor?
There are 3 modes under vi:
– Command mode – this is the mode where you start in
– Edit mode – this is the mode that allows you to do text editing
– Ex mode – this is the mode wherein you interact with vi with instructions to process a file
38) Is it possible to use shortcut for a long pathname?
Yes, there is. A feature known as filename expansion allows you do this using the TAB key. For example, if you have a path named /home/iceman/assignments directory, you would type as follows: /ho[tab]/ice[tab]/assi[tab] . This, however, assumes that the path is unique, and that the shell you’re using supports this feature.
39) What is redirection?
Redirection is the process of directing data from one output to another. It can also be used to direct an output as an input to another process.
40) What is grep command?
grep a search command that makes use of pattern-based searching. It makes use of options and parameters that is specified along the command line and applies this pattern into searching the required file output.
41) What could possibly be the problem when a command that was issued gave a different result from the last time it was used?
One highly possible reason for getting different results from what seems to be the same command has something to do with case sensitivity issues. Since Linux is case sensitive, a command that was previously used might have been entered in a different format from the present one. For example, to lists all files in the directory, you should type the command ls, and not LS. Typing LS would either result in an error message if there is no program by that exact name exist, or may produce a different output if there is a program named LS that performs another function.
42) What are the contents in /usr/local?
It contains locally installed files. This directory actually matters in environments where files are stored on the network. Specifically, locally-installed files go to /usr/local/bin, /usr/local/lib, etc.). Another application of this directory is that it is used for software packages installed from source, or software not officially shipped with the distribution.
43) How do you terminate an ongoing process?
Every process in the system is identified by a unique process id or pid. Use the kill command followed by the pid in order to terminate that process. To terminate all process at once, use kill 0.
44) How do you insert comments in the command line prompt?
Comments are created by typing the # symbol before the actual comment text. This tells the shell to completely ignore what follows. For example: “# This is just a comment that the shell will ignore.”
45) What is command grouping and how does it work?
You can use parentheses to group commands. For example, if you want to send the current date and time along with the contents of a file named OUTPUT to a second file named MYDATES, you can apply command grouping as follows: (date cat OUTPUT) > MYDATES
46) How do you execute more than one command or program from a single command line entry?
You can combine several commands by separating each command or program using a semicolon symbol. For example, you can issue such a series of commands in a single entry:
**Note that this will be executed one after the other, in the order specified.
47) Write a command that will look for files with an extension “c”, and has the occurrence of the string “apple” in it.
Answer:

48) Write a command that will display all .txt files, including its individual permission.
Answer:

49) Write a command that will do the following:
-look for all files in the current and subsequent directories with an extension c,v
-strip the,v from the result (you can use sed command)
-use the result and use a grep command to search for all occurrences of the word ORANGE in the files.

50) What, if anything, is wrong with each of the following commands?
a) ls -l-s
b) cat file1, file2
c) ls – s Factdir
Answers:
a) there should be space between the 2 options: ls -l -s
b) do not use commas to separate arguments: cat file1 file2
c) there should be no space between hyphen and option label: ls –s Factdir
—————————————————————————————————————————————————-
Q.1: What is the core of Linux Operating System?
  1. Shell
  2. Kernel
  3. Command
  4. Script
  5. Terminal
Answer : Kernel is the core of Linux Operating System. Shell is a command Line Interpreter, Command is user Instruction to Computer, Script is collection of commands stored in a file and Terminal is a command Line Interface
Q.2: What Linus Torvalds Created?
  1. Fedora
  2. Slackware
  3. Debian
  4. Gentoo
  5. Linux
Answer : Linux Torvalds created Linux, which is the kernel (heart) of all of the above Operating System and all other Linux Operating System.
Q.3: Torvalds, Wrote most of the Linux Kernel in C++ programming Language, do you agree?
Answer : No! Linux Kernel contains 12,020,528 Lines of codes out of which 2,151,595 Lines are comments. So remaining 9,868,933 lines are codes and out of 9,868,933 Lines of codes 7,896,318 are written in C Programming Language.
The remaining Lines of code 1,972,615 is written in C++, Assembly, Perl, Shell Script, Python, Bash Script, HTML, awk, yacc, lex, sed, etc.
Note : The Number of Lines of codes varies on daily basis and an average of more than 3,509 lines are being added to Kernel.
Q.4: Linux initially was developed for intel X86 architecture but has been ported to other hardware platform than any other Operating System. Do you agree?.
Answer : Yes, I do agree. Linux was written for x86 machine, and has been ported to all kind of platform. Today’s more than 90% of supercomputers are using Linux. Linux made a very promising future in mobile phone, Tablets. In-fact we are surrounded by Linux in remote controls, space science, Research, Web, Desktop Computing. The list is endless.
Q.5: Is it legal to edit Linux Kernel?
Answer : Yes, Kernel is released under General Public Licence (GPL), and anyone can edit Linux Kernel to the extent permitted under GPL. Linux Kernel comes under the category of Free and Open Source Software (FOSS).
Q.6: What is the basic difference between UNIX and Linux Operating System.
Answer : Linux Operating System is Free and Open Source Software, the kernel of which is created by Linus Torvalds and community. Well you can not say UNIX Operating System doesn’t comes under the category of Free and Open Source Software, BSD, is a variant of UNIX which comes under the category of FOSS. Moreover Big companies like Apple, IBM, Oracle, HP, etc. are contributing to UNIX Kernel.
Q. 7: Choose the odd one out.
  1. HP-UX
  2. AIX
  3. OSX
  4. Slackware
  5. Solaris
Answer : Slackware is the odd in the above list. HP-UX, AIX, OSX, Solaris are developed by HP, IBM, APPLE, Oracle respectively and all are UNIX variant. Slackware is a Linux Operating System.
Q.8: Is Linux Operating system Virus free?
Answer : No! There doesn’t exist any Operating System on this earth that is virus free. However Linux is known to have least number of Viruses, till date, yes even less than UNIX OS. Linux has had about 60-100 viruses listed till date. None of them actively spreading nowadays. A rough estimate of UNIX viruses is between 85 -120 viruses reported till date.
Q.9: Linux is which kind of Operating System?
  1. Multi User
  2. Multi Tasking
  3. Multi Process
  4. All of the above
  5. None of the above
Answer : All of the Above. Linux is an Operating System which supports Multi User, Running a Number of Processes performing different tasks simultaneously.
Q.10: Syntax of any Linux command is:
  1. command [options] [arguments]
  2. command options [arguments]
  3. command [options] [arguments]
  4. command options arguments
Answer : The correct Syntax of Linux Command is Command [options] [arguments].
Q.11: Choose the odd one out.
  1. Vi
  2. vim
  3. cd
  4. nano
Answer : The odd one in the above list is cd. Vi, vim and nano are editors which is useful in editing files, while cd command is used for changing directory.
—————————————————————————————————————————————————-

20 UNIX Shell Scripting Interview Questions…and Answers!!! – Part I

As someone who has written a large number of both simple and complex UNIX shell scripts during the past 15 years, the following are some basic shell scripting interview questions that I would ask if I needed to hire someone to write new shell scripts or maintain preexisting scripts for me.
These type of questions would help me to determine If the candidate for the position has spent any time actually writing shell scripts, either on the job or as part of a shell scripting course or training class, or has just read about them in books or online.
Let’s get started…
(1) What is a UNIX shell?
The UNIX shell is a program that serves as the interface between the user and the UNIX operating system. It is not part of the kernel, but communicates directly with the kernel. The shell translates the commands you type in to a format which the computer can understand. It is essentially a command line interpreter.
Details: 
Visual representation of the UNIX operating system environment:
The UNIX Operating System Environment
List of commonly used UNIX shells:
· The Bourne Shell (sh)
· The C Shell (csh or tsch)
· The Bourne Again Shell (bash)
· The Korn Shell (ksh)
(2) What needs to be done before you can run a shell script from the command line prompt?
You need to make the shell script executable using the UNIX chmod command.
Details: 
This chmod command makes the shell script file “example1” executable for the user (owner) only:
$ chmod u+x example1
this syntax makes it executable for all (everyone):
$ chmod a+x example1
You can optionally use octal notation to set UNIX permissions using the chmod command (e.g., $ chmod 755 example1). This topic is beyond the scope of this article, but you can find more information by entering “unix file permissions chmod numeric notation” in your favorite search engine.
(3) How do you terminate a shell script if statement?
With fi, which is “if” spelled backwards.
Details:
The shell script example below uses an if statement to check if a file assigned to the variable myfile exists and is a regular file:
#!/bin/ksh
myfile=$1
if [ -f $myfile ]
then
echo “$myfile exists”
fi
exit 0
(See shell scripting interview question #6 below if you do not know what $1 in this example means.)
(4) What UNIX operating system command would you use to display the shell’s environment variables?
Running the “env” command will display the shell environment variables.
Details: 
Sample env command output:
$ env
HISTFILE=/home/lfl/.history
PATH=/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin
SHELL=/bin/ksh
HOSTNAME=livefirelabs.com
USER=lfl
MAIL=/var/spool/mail/lfl
HOME=/home/lfl
HISTSIZE=1000
It would also be good to understand the purpose of the common shell environment variables that are listed in the env command output.
(5) What code would you use in a shell script to determine if a directory exists?
The UNIX test command with the -d option can be used to determine if a directory exists.
Details: 
The following test command expression would be used to verify the existence of a specified directory, which is stored in the variable $mydir:
if [ -d $mydir ]
then
command(s)
fi
If the value stored in the variable mydir exists and is a directory file, the command(s) located between then and fi will be executed.
You can consult the test command’s man page (“$ man test”) to see what test command options are available for use.
(6) How do you access command line arguments from within a shell script?
Arguments passed from the command line to a shell script can be accessed within the shell script by using a $ (dollar sign) immediately followed with the argument’s numeric position on the command line.
Details: 
For example, $1 would be used within a script to access the first argument passed from the command line, $2 the second, $3 the third and so on. Bonus: $0 contains the name of the script itself.
(7) How would you use AWK to extract the sixth field from a line of text containing colon (:) delimited fields that is stored in a variable called passwd_line?echo $passwd_line | awk -F: ‘{ print $6 }’
Details: 
Consider this line of text stored in the variable $passwd_line –
$ echo $passwd_line
mail:x:8:12:mail:/var/spool/mail:
$
(Background: The lines in the system passwd file are delimited (separated) by a colon (:)…$passwd_line contains a single line from the passwd file.)
The output of the echo command is piped to AWK. The -f option for the awk command informs awk of what the field separator is (colon in this example), and print $6 instructs awk to print the 6th field in the line.
(8) What does 2>&1 mean and when is it typically used?
The 2>&1 is typically used when running a command with its standard output redirected to a file. For example, consider:
command > file 2>&1
Anything that is sent to command’s standard output will be redirected to “file” in this example.
The 2 (from 2>&1) is the UNIX file descriptor used by standard error (stderr). Therefore, 2>&1 causes the shell to send anything headed to standard error to the same place messages to standard output (1) are sent…which is “file” in the above example.
To make this a little clearer, the > in between “command” and “file” in the example is equivalent to 1>.
Before you continue reading… Has this article been helpful to you? Would it benefit others? If you answered “yes” to either question, kindly share the page.
MORE READERS = MORE FUTURE ARTICLES
Thank you for sharing.
(9) What are some ways to debug a shell script problem?
Although this is somewhat dependent on what the problem is, there are a few commonly used methods for debugging shell script problems.
One method, which is frequently used across all programming languages, is to insert some debug statements within the shell script to output information to help pinpoint where and why the problem is being introduced.
Another method specific to shell scripting is using “set -x” to enable debugging.
Details:
Consider the following shell script example (notice that “set -x” is commented out at this time):
#!/bin/ksh
#set -x
i=1
while [ $i -lt 6 ]
do
print “in loop iteration: $i”
((i+=1))
done
exit
This script will produce the following output:
$ ./script1
in loop iteration: 1
in loop iteration: 2
in loop iteration: 3
in loop iteration: 4
in loop iteration: 5
$
If we uncomment (remove the #) from the “set -x” line in the script, this is what is displayed when the script runs:
$ ./script1
+ i=1
+ [ 1 -lt 6 ]
+ print in loop iteration: 1
in loop iteration: 1
+ let i+=1
+ [ 2 -lt 6 ]
+ print in loop iteration: 2
in loop iteration: 2
+ let i+=1
+ [ 3 -lt 6 ]
+ print in loop iteration: 3
in loop iteration: 3
+ let i+=1
+ [ 4 -lt 6 ]
+ print in loop iteration: 4
in loop iteration: 4
+ let i+=1
+ [ 5 -lt 6 ]
+ print in loop iteration: 5
in loop iteration: 5
+ let i+=1
+ [ 6 -lt 6 ]
+ exit
$
In addition to displaying the intended output (“in loop iteration” lines), enabling debugging with “set -x” also shows each line of execution preceded by a plus sign (+).
Although this can become unwieldly for larger shell scripts, it should be obvious how useful this can be when debugging a shell script to identify the root cause of a problem.
(10) Within a UNIX shell scripting loop construct, what is the difference between the break and continue?
Using break within a shell scripting loop construct will cause the entire loop to terminate. A continue will cause the current iteration to terminate, but the loop will continue on the next iteration.
Do you need to practice writing and executing UNIX shell scripts (on a REAL SERVER) before the interview? With a highly concentrated effort, either of these online courses can be completed within 24 hours…
UNIX and Linux Operating System Fundamentals contains a very good “Introduction to UNIX Shell Scripting” module, and should be taken if you are new to the UNIX and Linux operating system environments or need a refresher on key concepts.
UNIX Shell Scripting is a good option if you are already comfortable with UNIX or Linux and just need to sharpen your knowledge about shell scripting and the UNIX shell in general.
…or you can optionally select a subset of modules from the course to focus on prior to the interview and then return to work on the remaining topics as your schedule permits.
Both courses include access to an Internet Lab system for completing the course’s hands-on exercises, which are used to re-enforce the key concepts presented in the course. Any questions you may have while taking the course are answered by an experienced UNIX technologist
—————————————————————————————————————————————————-

Beginners UNIX Interview Questions Answers

  1. Write command to list all the links from a directory?
In this UNIX command interview questions interviewer is generally checking whether user knows basic use of “ls” “grep” and regular expression etc. You can write command like:
ls -lrt | grep “^l”
  1. Create a read-only file in your home directory?
This is a simple UNIX command interview questions where you need to create a file and change its parameter to read-only by using chmod command you can also change your umask to create read only file.
$ touch file $ chmod 400 file

  1. How will you find which operating system your system is running on in UNIX?
By using command “uname -a” in UNIX

  1. How will you run a process in background? How will you bring that into foreground and how will you kill that process?
For running a process in background use “&” in command line. For bringing it back in foreground use command “fg jobid” and for getting job id you use command jobs, for killing that process find PID and use kill -9 PID command. This is indeed a good Unix Command interview questions because many of programmer not familiar with background process in UNIX.
  1. How do you know if a remote host is alive or not?
You can check these by using either ping or telnet command in UNIX. This question is most asked in various Unix command Interview because its most basic networking test anybody wants to do it.
  1. How do you see command line history in UNIX?
Very useful indeed, use history command along with grep command in UNIX to find any relevant command you have already executed. Purpose of this Unix Command Interview Questions is probably to check how familiar candidate is from available tools in UNIX operation system.

  1. How do you copy file from one host to other?
Many options but you can say by using “scp” command. You can also use rsynccommand to answer this UNIX interview question or even sftp would be ok.

  1. How do you find which process is taking how much CPU?
By using “top” command in UNIX, there could be multiple follow-up UNIX command interview questions based upon response of this because “TOP” command has various interactive options to sort result based upon various parameter.

  1. How do you check how much space left in current drive ?
By using “df” command in UNIX. For example “df -h .” will list how full your current drive is. This is part of anyone day to day activity so I think this Unix Interview question will be to check anyone who claims to working in UNIX but not really working on it.

  1. What is the difference between Swapping and Paging?
Swapping:
Whole process is moved from the swap device to the main memory for execution. Process size must be less than or equal to the available main memory. It is easier to implementation and overhead to the system. Swapping systems does not handle the memory more flexibly as compared to the paging systems.
Paging:
Only the required memory pages are moved to main memory from the swap device for execution. Process size does not matter. Gives the concept of the virtual memory. It provides greater flexibility in mapping the virtual address space into the physical memory of the machine. Allows more number of processes to fit in the main memory simultaneously. Allows the greater process size than the available physical memory. Demand paging systems handle the memory more flexibly.

Intermediate UNIX Interview Questions Answers

  1. What is difference between ps -ef and ps -auxwww?
This is indeed a good Unix Interview Command Question and I have faced this issue while ago where one culprit process was not visible by execute ps –ef command and we are wondering which process is holding the file.
ps -ef will omit process with very long command line while ps -auxwww will list those process as well.

  1. How do you find how many cpu are in your system and there details?
By looking into file /etc/cpuinfo for example you can use below command:
cat /proc/cpuinfo


  1. What is difference between HardLink and SoftLink in UNIX?
I have discussed this Unix Command Interview questions  in my blog post difference between Soft link and Hard link in Unix


  1. What is Zombie process in UNIX? How do you find Zombie process in UNIX?
When a program forks and the child finishes before the parent, the kernel still keeps some of its information about the child in case the parent might need it – for example, the parent may need to check the child’s exit status. To be able to get this information, the parent calls ‘wait()’; In the interval between the child terminating and the parent calling ‘wait()’, the child is said to be a’zombie’ (If you do ‘ps’, the child will have a ‘Z’ in its status field to indicate this.)
Zombie : The process is dead but have not been removed from the process table.


  1. What is “chmod” command? What do you understand by this line “r– -w- –x?
chmod command is used to change permission of a file or directory in UNIX. The line you see shows the permission for three different set of people : user, group and others. User is the currently logged in user, while group is for all other member which are part of certain group and others means anyone other than user and group member. Each group has three permissions rwx stands for read, write and execute and they are written as user_group_others. So in above line, user has only read permission, group members has write permissions and other people has only execute permission. If it is a directory then you need execute permission to go inside that directory. See here for more detailed answer.


  1. There is a file some where in your system which contains word “UnixCommandInterviewQuestions” How will find that file in Unix?
By using find command in UNIX for details see here 10 example of using find command in Unix

  1. In a file word UNIX is appearing many times? How will you count number?
grep -c “Unix” filename

  1. How do you set environment variable which will be accessible form sub shell?
By using export command,  for example export count=1 will be available on all sub shell.

  1. How do you check if a particular process is listening on a particular port on remote host?
By using telnet command for example “telnet hostname port”, if it able to successfully connect then some process is listening on that port. To read more about telnet read networking command in UNIX

  1. How do you find whether your system is 32 bit or 64 bit ?
Either by using “uname -a” command or by using “arch” command.

Advanced UNIX Interview Questions and Answers

  1. How do you find which processes are using a particular file?
By using lsof command in UNIX. It wills list down PID of all the process which is using a particular file.

  1. How do you find which remote hosts are connecting to your host on a particular port say 10123?
By using netstat command execute netstat -a | grep “port” and it will list the entire host which is connected to this host on port 10123.

  1. What is nohup in UNIX?
    nohup is a special command which is used to run process in background, but it is slightly different than & which is normally used for putting a process in background. An UNIX process started with nohup will not stop even if the user who has stared log off from system. While background process started with & will stop as soon as user logoff.
  1. What is ephemeral port in UNIX?
Ephemeral ports are port used by Operating system for client sockets. There is a specific range on which OS can open any port specified by ephemeral port range.

  1. If one process is inserting data into your MySQL database? How will you check how many rows inserted into every second?
Purpose of this Unix Command Interview is asking about watch command in UNIX which is repeatedly execute command provided with specified delay.

  1. There is a file Unix_Test.txt which contains words Unix, how will you replace all Unix to UNIX?
You can answer this Unix Command Interview question by using SED command in UNIX for example you can execute following command to replace all Unix word to UNIX
sed s/Unix/UNIX/g fileName

  1. You have a tab separated file which contains Name, Address and Phone Number, list down all Phone Number without there name and Addresses?
To answer this Unix Command Interview question you can either you AWK or CUT command here. CUT use tab as default separator so you can use
cut -f3 filename

  1. Your application home directory is full? How will you find which directory is taking how much space?
By using disk usage (DU) command in Unix for example du –sh . | grep G  will list down all the directory which has GIGS in Size.

  1. How do you find for how many days your Server is up?
By using uptime command in UNIX

  1. You have an IP address in your network how will you find hostname and vice versa?
This is a standard UNIX command interview question asked by everybody and I guess everybody knows its answer as well. By using nslookup command in UNIX, you can read more about convert IP Address to hostname in Unix here.

I hope this UNIX command interview questions and answers would be useful for quick glance before going for any UNIX or Java job interview. Please share any interesting UNIX command interview you have come across and I will add into this list. If you are going for any Unix interview on brokerage firm or stock trading company or any Investment bank you can have a quick look here, though most of questions you might already know but its good to review it. if you like this you can see my otherUNIX command tutorial for beginners as well
—————————————————————————————————————————————————-
What is the core of Linux Operating System?
The core of the Linux operating system is Kernel. It is broken down into Shell, Command, Script, and Terminal. Shell is a command Line Interpreter, Command is user Instruction to Computer, Script is collection of commands stored in a file, and Terminal is a command Line Interface.
What is the basic difference between UNIX and Linux Operating System?
Linux is free and open-source software (allowing programmers to program with Linux not around it), the kernel of which is created by Linus Torvalds and community. UNIX, on the other hand, is UNIX is copyrighted name only big companies are allowed to use the UNIX copyright and name, so IBM AIX and Sun Solaris and HP-UX all are UNIX operating systems.
What is an INODE?
All files have its description stored in a structure called ‘inode’. The inode contains info about the file-size, access and modification time, permission and so on. In addition to descriptions about the file, the inode contains pointers to the data blocks of the file.
State the syntax of any Linux command.
The correct syntax of Linux command is Command [options] [arguments]. Master the Linux command line with this guide.
Now let’s move on to the meatier questions that are more likely to be asked:
What is the difference between TCP and UDP?
The basic difference is that TCP establishes a connection before sending data and this allows it to control the dataflow and guarantee that all packets get delivered. UDP simply chucks datagrams onto the wire and if some get lost or arrive in bad order there’s no way to request a resend. However UDP has low network overhead so some services such as DNS resolution, SNMP, DHCP, RIP and VOIP use UDP for its speed and any errors are usually dealt with on the application layer rather than network layer.
How does DNS resolution work?
A client application requests an IP address from the name server usually by connecting to UDP port 53. The name server will attempt to resolve the FQDN based on its resolver library, which may contain authoritative information about the host requested or cached data about that name from an earlier query. If the name server does not already have the answer, it will turn to root name servers to determine the authoritative for the FQDN in question. Then, with that information, it will query the authoritative name servers for that name to determine the IP address.
What is an MX record?
An MX record numerically ranks the mail servers you would prefer to receive email for a domain. The MX record with the lowest number is preferred over the others, but you can set multiple email servers with the same value for simple load balancing.
Please describe the Linux boot-up sequence.
There are seven steps to the boot-up sequence. 1) BIOS (basic input/output system) – executes the MBR where Boot Loader sits, 2) MBR- Master boot reads Kernel into memory, 3) GRUB (Grand Unified Bootloader) Kernel starts Init process, 4) Kernel – Kernel executes the /sbin/init program.  Init reads inittab, executes rc.sysinit, 5) Init – the rc script than starts services to reach the default run level and 6) Run level programs – these programs are executed from /etc/rc.d/rc*.dl/
How do you search for a pattern and then replace it in an entire file?
You use Sed, or in Vi editor, the search uses character ‘s’ slash the pattern to be searched, slash the pattern to replace it with, slash ‘g’ which stands for entire file.
How do you list and flush all IPtables?
First you use the –L switch to view all the currently present rules and then –F to flush them.
What is a shell? What are their names?
The shell is the part of the system with which the user interacts. A Unix shell interprets commands such as “pwd”, “cd” or “traceroute” and sends the proper instructions to the actual operating system itself. The shells currently available areAns SH, BASH, CSH, TCSH, NOLOGIN, KSH. Other functions of a shell include scripting capability, path memory, multitasking, and file handling.
What is a zombie?
Cheeky answers get bonus points for this one. But in the Linux world, a zombie process is the process  output of ‘ps’ by the presence of ‘Z’ in the STAT column. Zombies are essentially the premature processes whose mature parent processes died without reaping its children. Note that zombies can’t be killed with the usual ‘kill’ signal.
—————————————————————————————————————————————————-
1. What is UNIX?
It may sound as a trivial question, but you’d be amazed just how many people fail to give a clear definition, because simply stating that “Unix is an operating system” is not likely to impress the interviewer. The correct answer should look something like this:
UNIX is a multi-user multitasking-optimized operating system that can run on various hardware platforms.
2. How is UNIX different from Linux?
Linux is basically an open-source clone of UNIX so, while a lot of similarities between the two operating systems do exist, there are also a lot of differences. The main advantage of UNIX is that all the core components of the operating system come from the same vendor, which means greater overall stability and better software support from vendors. UNIX releases are more stable and consistent than Linux releases, making UNIX the better choice for enterprise use.
3. What is a kernel?
The kernel is the program that acts as a middle layer between software and hardware. When a program requires access to certain resources or processing power, the kernel is responsible for sending the correct signals to the CPU and managing all other running programs and services so that the resources are correctly allocated and no conflicts occur.
4. What is the difference between multi-user and multi-tasking?
Multi-tasking means that a user can run multiple tasks simultaneously on a single machine, whereas multi-user means that multiple users can operate simultaneously on a machine.
5. What is a UNIX shell?
A UNIX shell is an interface that acts as a command interpreter, translating user input into machine-understandable language and then passing it to the kernel for execution.
Once you ace the basic stuff, the interviewer is most likely to move on to some questions involving commands. Keep in mind that his role is not to see how good you are at memorizing commands, but rather how well you understand what those commands do and determine your ability to choose the right command in a certain situation. In order to successfully pass this part of the interview, make sure you formulate your answers as clear and as detailed as possible. You should consider taking a course to improve your communication skills prior to the interview.

UNIX Commands

1. What command can you use to display the first 3 lines of text from a file and how does it work?
There are two commands that can be used to complete this task:
        head -3 test.txt – this uses the “head” command, along with the “-3” parameter that indicates the number of lines to be displayed;
        sed ‘4,$ d’ test.txt – this command uses the Sed text editor to perform the task. If the command was simply “sed test.txt” the whole file would have been displayed; however, in our example the delete parameter was used (d) to make Sed delete everything between the 4th and the last line (defined by the $ parameter), leaving only the first 3 lines of the file. It is important to mention that Sed does not actually delete the lines from the file itself, but just from the output result.
2. How can you remove the 7th line from a file?
The easiest way is by using the following command: sed -i ‘7 d’ test.txt
Unlike the previous Sed command, this command also has the “-i” parameter, which tells Sed to make the change in the actual file.
3. What is piping?
Piping is a technique that consists of entering two or more consecutive commands, separated by the pipe symbol “|”. Once the first command is executed, its output will be used as input for the second command, the output of the second command will be used as input for the third and so on, until the whole chain of commands is executed.
4. How do you reverse a string?
You can reverse a string by using a simple piping of two commands: echo “Mary” | rev
The first command will generate the output “Mary”, which will become the input for the rev command, making it return the reverse: “yraM”.
5. How can you find out what a command does?
You use man <command-name> in order to bring up the manual page that describes the actions of the specified command and any other additional options and parameters that command might have.
—————————————————————————————————————————————————-

Unix List of Basic Commands for QA and Test Engineer

Unix List of  Basic Commands for QA and Test Engineer
File and directory listing Commands
$ ls      command is used for directory and file listing with there permission.
$ ls -l   list files in current directory.
$ ls -a  list hidden files.
Directories commands
$ pwd -command is used for knowing user current working directory or you can say print working directory.
$ cd – similar to DOS cd .it is use to change directory.
$ mkdir – use to create new directory.
$ rmdir – use to remove directory.
File Manipulation commands
$ cat filename – command is use to display file contents without paging.
$ cp source destination – command is use to copy file.
$ mv oldname  newname – command is use to rename file .
$ rm filename – command use to delete file.
$ pico  filename – command use to create or modify file.
$ More filename -List the contents of file pausing after each screen (press Space to continue listing).
$ grep string file-Search file for occurrences of string.
$ less filename – Display filename one screenful.A pager similar to (better than) more.
$ pg filename – View filename one screenfull at atime (a pager).
Special Control Characters
The control characters that have special meanings in a UNIX environment. Denoted by the character ^ followed by a letter, each control character can be generated by holding down Control and pressing the specified letter, then releasing both keys.
^c
Interrupt a process (applicable for most processes); used to halt a process immediately.
Pressing ^c, for example, enables you to stop the listing of a long file. However, if the file is very long, there may be a noticeable time delay before the process is actually interrupted.
^d
Terminate a process; end-of-file marker (EOF). If a process does not have a clean termination command and pressing ^c does not work, try pressing ^d.
^s
Suspend terminal output to your system. To resume terminal output, press ^q. ^s is usually referred to as XON.
To view text a page at a time, use the more command.
^q
Resume terminal output suspended by pressing ^s (see above). ^q is usually referred to as XOFF.
^z
Suspend the foreground process, that is, place the current active program in a paused state.
Type the fg command to reactivate a process in the foreground. A foreground process will usually keep control of your keyboard and terminal.
Type the bg command to reactivate a process in the background. A background process will let you type other commands on the keyboard while it is running in a detached state.
^u
Erase command line.
^w
Erase previous word on a UNIX command line. A word is a string of characters separated by a space or tab.
^r Retype current command line
OTHER COMMANDS
$ print file –Send file to the UNIX laser printer.
$ man command – Display information about command.
$ apropos keyword – List commands/programs that contain keyword in their description.
$ du – List the number of Kbytes for each directory below current working directory.
$ talk username – Interactively communicate with username; username has to be logged-in.
$ finger username – Find the username for username.
$ w – Display current activity on the system.
$ who – Show users that are currently logged-in.
$ tin –  A program to read a collection of newsgroups (new articles arrive daily).
$ help [command] – If specified without command, a brief introduction is listed.Ifcommand is specified, help displays the man page for command.
$ pine – Send email through the Internet
$ quota –v – Display disk usage and disk quota. This command does not display your email quota.
$ mydiskspace – Display uhunix disk usage only. This command does not display your email quota.
$ write username – Writes files or text to username.
$ !! – Repeats last command.
Process management
$ kill  – Command use to terminate process.example: kill – 5555
$ nice program name – Command use to run program  with lower priority.Recommended for running background processes.
$ ps – Command use to list no of processes running in a system.
$ top – show (continuously) what process(es) is running.
—————————————————————————————————————————————————

Linux/Unix Commands

File Management becomes easy if you know the right commands.
Sometimes, commands are also referred as “programs” since whenever you run a command, actually, it’s the corresponding program code, written for the command, which is being executed.
Let’s learn the must know Linux commands.

Please be patient. The Video will load in some time. If you still face issue viewing video click here

Listing files (ls)


If you want to see the list of files on your UNIX or Linux system, use the ‘ls’command.
It shows the files /directories in your current directory.

Note:
  • Directories are denoted in blue color.
  • Files are denoted in white.
  • You will find similar color schemes in different flavors of Linux.
Suppose, your “Music” folder has following sub-directories and files.
You can use ‘ls-R’ to shows all the files not only in directories but also subdirectories 

NOTE: The command is case-sensitive. If you enter, “ls – r” you will get an error
‘ls -al’ gives detailed information of the files. The command provides information in a columnar format. The columns provide the following information:

1st Column
File type and access permissions
2nd Column
# of HardLinks to the File
3rd Column
Owner and the creator of the file
4th Column
Group of the owner
5th Column
File size in Bytes
6th Column
Date and Time
7th Column
Directory or File name

Let’s see an example –

Listing Hidden Files

Hidden items in UNIX/Linux begin with –at the start, of the file or directory.
Any Directory/file starting with a ‘.’ will not be seen unless you request for it.  To view hidden files, use the command
ls  – a

Creating & Viewing Files

The ‘cat’ command is used to display text files. It can also be used for copying, combining and creating new text files.  Let’s see how it works
To create a new file, use the command
  1.  cat > filename
  2.  Add content
  3.  Press ‘ctrl + d’ to return to command prompt.
To view a file, use the command –
cat
Let’s see the file we just created –
Let’s see another file sample2
The syntax to combine 2 files is –
cat file1 file2 > newfilename
Let’s combine sample 1 and sample 2.
As soon as you insert this command and hit enter, the files are concatenated, but you do not see a result. This is because Bash Shell (Terminal) is silent type.  It will never give you a confirmation message like “OK” or “Command Successfully Executed”. It will only show a message when something goes wrong or when an error has occurred.
In order to view the new combo file “sample” use the command
cat sample
Note: Only text files can be displayed and combined using this command.

Deleting Files

The ‘rm’ command removes files from the system without confirmation.  To delete a file use syntax –
rm  

Moving and Re-naming files

In order to move a file, use the command
mv
Suppose we want to move the file “sample2” to location /home/guru99/Documents. Executing the command
mv sample2  /home/guru99/Documents

mv command needs super user permission. Currently, we are executing the command as a standard user. Hence we get the above error. To overcome the error use command
sudo
Sudo program allows regular users to run programs with the security privileges of the superuser or root.
Sudo command will ask for password authentication. Though, you do not need to know the root password. You can supply your own password. After authentication, the system will invoke the requested command.
Sudo maintains a log of each command run. System administrators can trackback the person responsible for undesirable changes in the system
For renaming file:
mv filename newfilename
NOTE: By default, the password you entered for sudo is retained for 15 minutes per terminal. This eliminates the need of entering the password time and again.
You only need root/sudo privileges, only if the command involves files or directories not owned by the user or group running the comman

Directory Manipulations

Enough with File manipulations!  Let’s learn some directory commands
Creating Directories
Directories can be created on a Linux operating system using the following command
mkdir
This command will create a subdirectory in your present working directory, which is usually your “Home Directory”.
For example,
mkdir mydirectory
If you want to create a directory in a different location other than ‘Home directory’, you could use the following command –
mkdir
For example:
mkdir /tmp/MUSIC
will create a directory ‘Music’ under ‘/tmp’ directory
You can also create more than one directory at a time.

Removing Directories

In order to remove a directory, use the command –
rmdir
Example
rmdir mydirectory
will delete the directory mydirectory
Tip: Ensure that there is no file / sub-directory under the directory that you want to delete. Delete the files/sub-directory first before deleting the parent directory.

Renaming Directory

The ‘mv’ (move) command (covered earlier) can also be used for renaming directories. Use the below given format:
mv directoryname newdirectoryname
Let us try it:

Other Important Commands

The ‘Man’ command

Man stands for manual which is a reference book of a Linux operating system. It is similar to HELP file found in popular softwares.
To get help on any command that you do not understand, you can type
man
The terminal would open the manual page for that command.
For an example, if we type man man and hit enter; terminal would give us information on man command

The History Command

History command shows all the commands that you have used in the past for the current terminal session. This can help you refer to  the old commands you have entered and re-use them in your operations again.

The clear command

This command clears all the clutter on the terminal and gives you a clean window to work on, just like when you launch the terminal.

Pasting commands into the terminal

Many a times you would have to type in long commands on the Terminal. Well, it can be annoying at times, and if you want to avoid such a situation then copy, pasting the commands can come to rescue.
For copying, the text from a source, you would use Ctrl + c, but for pasting it on the Terminal you need to useCtrl + Shift + v. You can also try Shift + Insert or select Edit>Paste on the menu

Summary


Below is a summary of commands we have learned in this tutorial
CommandDescription
lsLists all files and directories in the present working directory
ls – RLists files in sub-directories as well
ls – aLists hidden files as well
ls – alLists files and directories with detailed information like permissions , size , owner etc.
cat > filenameCreates a new file
cat filenameDisplays the file content
cat file file2 > file3Joins two files (file1, file2) and stores the output in a new file (file3)
mv  file “new file path”Moves the files to the new location
mv filename new_file_nameRenames the file to a new filename
sudoAllows regular users to run programs with the security privileges of the superuser or root
rmDeletes a file
manGives help information on a command
historyGives a  list of all past commands typed in the current terminal session
clearClears the terminal
mkdirCreates a new directory in the present working directory
mkdirCreate a new directory at the specified path
rmdirDeletes a directory
mvRenames a directory

No comments:

Post a Comment