This course contains Top 50 Unix interview questions that are asked in a technical interview. The focus is on commands and concepts inside Unix. It is an important topic for a software developer to know about Unix.
It is a compilation of advanced Unix interview questions after attending dozens of technical interviews in top-notch companies like- Oracle, Google, Ebay, Amazon etc. Each question is accompanied with an answer so that you can prepare for job interview in short time.
Often, these questions and concepts are used in our daily programming work. But these are most helpful when an Interviewer is trying to test your deep knowledge of Unix concepts.
What are the topics covered in this course?
We cover a wide range of topics in this course. We have questions on Unix commands, concepts etc.
How will this course help me?
By attending this course, you do not have to spend time searching the Internet for Unix interview questions. We have already compiled the list of the most popular and the latest Unix Interview questions.
Are there answers in this course?
Yes, in this course each question is followed by an answer. So you can save time in interview preparation.
What is the best way of viewing this course?
You have to just watch the course from beginning to end. Once you go through all the videos, try to answer the questions in your own words. Also mark the questions that you could not answer by yourself. Then, in second pass go through only the difficult questions. After going through this course 2-3 times, you will be well prepared to face a technical interview in Unix topic.
What is the level of questions in this course?
This course contains questions that are good for a Fresher to an Architect level. The difficulty level of question varies in the course from a Fresher to an Experienced professional.
What are the sample questions covered in this course?
Sample questions covered in this course are as follows:
Q 1. How will you remove all files in current directory? Including the files that are two levels down in a sub-directory.
In Unix we have rm command to remove files and sub-directories. With rm command we have –r option that stands for recursive. The –r option can delete all files in a directory recursively.
It means if we our current directory structure is as follows:
-> Level_1_dir ->Level_2_dir
-> Level_1_dir ->Level_2_dir->a.txt
With rm –r * command we can delete the file a.txt as well as sub-directories Level_1_dir and Level_2_dir.
rm – r *
The asterisk (*) is a wild card character that stands for all the files with any name.
Q 2. What is the difference between the –v and –x options in Bash shell scripts?
In a BASH Unix shell we can specify the options –v and –x on top of a script as follows:
#!/bin/bash -x –v
With –x option BASH shell will echo the commands like for, select, case etc. after substituting the arguments and variables. So it will be an expanded form of the command that shows all the actions of the script. It is very useful for debugging a shell script.
With –v option BASH shell will echo every command before substituting the values of arguments and variables. In –v option Unix will print each line as it reads.
In –v option, If we run the script, the shell prints the entire file and then executes. If we run the script interactively, it shows each command after pressing enter.
Q. What is a Filter in Unix command?
In Unix there are many Filter commands like- cat, awk, grep, head, tail cut etc.
A Filter is a software program that takes an input and produces an output, and it can be used in a stream operation.
E.g. cut -d : -f 2 /etc/passwd | grep abc
We can mix and match multiple filters to create a complex command that can solve a problem.
Awk and Sed are complex filters that provide fully programmable features.
Even Data scientists use Unix filters to get the overview of data stored in the files.
Q. What is Kernel in Unix operating system?
Kernel is the central core component of a Unix operating system (OS).
A Kernel is the main component that can control everything within Unix OS.
It is the first program that is loaded on startup of Unix OS. Once it is loaded it will manage the rest of the startup process.
Kernel manages memory, scheduling as well as communication with peripherals like printers, keyboards etc.
But Kernel does not directly interact with a user. For a new task, Kernel will spawn a shell and user will work in a shell.
Kernel provides many system calls. A software program interacts with Kernel by using system calls.
Kernel has a protected memory area that cannot be overwritten accidentally by any process.
Q. What is a Shell in Unix OS?
Shell in Unix is a user interface that is used by a user to access Unix services.
Generally a Unix Shell is a command line interface (CLI) in which users enter commands by typing or uploading a file.
We use a Shell to run different commands and programs on Unix operating system.
A Shell also has a command interpreter that can take our commands and send these to be executed by Unix operating system.
Some of the popular Shells on Unix are: Korn shell, BASH, C shell etc.
Q. What are the different shells in Unix that you know about?
Unix has many flavors of Shell. Some of these are as follows:
· Bourne shell: We use sh for Bourne shell.
· Bourne Again shell: We use bash to run this shell.
· Korn shell: We can use ksh to for Korn shell.
· Z shell: The command to use this is zsh
· C shell: We use csh to run C shell.
· Enhanced C shell: tcsh is the command for enhanced C shell.
Q. What is the first character of the output in ls –l command?
We use ls -l command to list the files and directories in a directory. With -l option we get long listing format.
In this format the first character identifies the entry type. The entry type can be one of the following:
b Block special file
c Character special file
l Symbolic link
s Socket link
- Regular file
In general we see d for directory and - for a regular file.
Q. What is the difference between Multi-tasking and Multi-user environment?
In a Multi-tasking environment, same user can submit more than one tasks and operating system will execute them at the same time.
In a Multi-user environment, more than one user can interact with the operating system at the same time.
Q. What is Command Substitution in Unix?
Command substitution is a mechanism by which Shell passes the output of a command as an argument to another command. We can even use it to set a variable or use an argument list in a for loop.
E.g. rm `cat files_to_delete`
In this example files_to_delete is a file containing the list of files to be deleted. cat command outputs this file and gives the output to rm command. rm command deletes the files.
In general Command Substitution is represented by back quotes `.
Q. What is an Inode in Unix?
An Inode is a Data Structure in Unix that denotes a file or a directory on file system. It contains information about file like- location of file on the disk, access mode, ownership, file type etc.
Each Inode has a number that is used in the index table. Unix kernel uses Inode number to access the contents of an Inode.
We can use ls -i command to get the inode number of a file.
Q. What is the difference between absolute path and relative path in Unix file system?
Absolute path is the complete path of a file or directory from the root directory. In general root directory is represented by / symbol. If we are in a directory and want to know the absolute path, we can use pwd command.
Relative path is the path relative the current location in directory.
E.g. In a directory structure /var/user/kevin/mail if we are in kevin directory then pwd command will give absolute path as /var/user/kevin.
Absolute path of mail folder is /var/user/kevin/mail. For mail folder ./mail is the relative path of mail directory from kevin folder.
Q. What are the main responsibilities of a Unix Shell?
Some of the main responsibilities of a Unix Shell are as follows:
1. Program Execution: A shell is responsible for executing the commands and script files in Unix. User can either interactively enter the commands in Command Line Interface called terminal or they can run a script file containing a program.
2. Environment Setup: A shell can define the environment for a user. We can set many environment variables in a shell and use the value of these variables in our program.
3. Interpreter: A shell acts as an interpreter for our scripts. It has a built in programming language that can be used to implement the logic.
4. Pipeline: A shell also can hookup a pipeline of commands. When we run multiple commands separated by | pipe character, the shell takes the output of a command and passes it to next one in the pipeline.
5. I/O Redirection: Shell is also responsible for taking input from command line interface (CLI) and sending the output back to CLI. We use >, <, >> characters for this purpose.
Q. What is a Shell variable?
A Unix Shell variable is an internal variable that a shell maintains. It is local to that Shell. It is not made available to the parent shell or child shell.
We generally use lower case names for shell variables in C shell.
We can set the value of a shell variable by set command.
E.g. % set max_threads=10
To delete a Shell variable we can use unset command.
To use a Shell variable in a script we use $ sign in front of the variable name.
E.g. echo $max_threads
Q. What are the important Shell variables that are initialized on starting a Shell?
There are following important Shell variables that are automatically initialized when a Shell starts:
These Shell variables take values from environment variables.
If we change the value of these Shell variables then the corresponding environment variable value is also changed.
Q. How will you set the value of Environment variables in Unix?
We can use 'setenv' command to set the value of environment variables.
E.g. % setenv [Name] [value]
% setenv MAX_TIME 10
To print the value of environment variable we can use 'printenv' command.
E.g. % printenv MAX_TIME
If we just use printenv then it lists all the environment variables and their values.
To unset or delete an environment variable we use unsetenv command.
E.g. % unsetenv MAX_TIME
To use an environment variable in a command we use the prefix $ with the name of variable.
Q. What is the special rule about Shell and Environment variable in Bourne Shell?
In Bourne Shell, there is not much difference between Shell variable and Environment variable.
Once we start a Bourne Shell, it gets the value of environment variables and defines a corresponding Shell variable. From that time onwards the shell only refers to Shell variable. But if a change is made to a Shell variable, then we have to explicitly export it to environment so that other shell or child processes can use it.
Also for Shell variables we use set and unset commands.
Q. What are the networking commands in Unix that you have used?
Some of the popular networking commands in Unix that we use are as follows:
Q. What is the difference between a System Call and a library function?
System calls are low-level kernel calls. These are handled by the kernel. System calls are implemented in kernel of Unix. An application has to execute special hardware and system dependent instruction to run a System call.
A library function is also a low level call but it is implemented in user space. A library call is a regular function call whose code resides in a shared library.
Q. What is a Pipeline in Unix?
A Pipeline in Unix is a chain of commands that are connected through a stream in such a way that output of one command becomes input for another command.
E.g. ls –l | grep “abc” | wc –l
In the above example we have created pipeline of three commands ls, grep and wc.
First ls –l command is executed and gives the list of files in a directory. Then grep command searches for any line with word “abc” in it. Finally wc –l command counts the number of lines that are returned by grep command.
In general a Pipeline is uni-directional. The data flows from left to right direction.
Q. What is the use of tee command in Unix?
We use tee command in a shell to read the input by user (standard input) and write it to screen (standard output) as well as to a file.
We can use tee command to split the output of a program so that it is visible on command line interface (CLI) as well as stored on a file for later use.
Syntax is tee [-a] [-i] [file …]
Q. How will you count the number of lines and words in a file in Unix?
We can use wc (word count) command for counting the number of lines and words in a file. The wc command provides very good options for collecting statistics of a file. Some of these options are:
In case we give more than one files as input to wc command then it gives statistics for individual files as well as the total statistics for all files.
Q. What is Bash shell?
Bash stands for Bourne Again Shell. It is free software written to replace Bourne shell.
We can see following line in shell scripts for Bash shell.
In Bash we use ~/.profile at login to set environment variables.
In Bash we can execute commands in batch mode or concurrent mode.
In batch mode commands are separated by semi colon.
% command1; command2
In concurrent mode we separate commands by & symbol.
% command1 & command2
Q. How will you search for a name in Unix files?
We can use grep command to search for a name or any text in a Unix file.
Grep stands for Globally search a Regular Expression and Print.
Grep command can search for a text in one file as well as multiple files.
We can also specify the text to be searched in regular expression pattern.
% grep ^z *.txt
Above command searches for lines starting with letter z in all the .txt files in current directory.
Q. What is the difference between whoami and who am i commands in Unix?
Both the commands whoami and who am i are used to get the user information in Unix.
When we login as root user on the network, then both whoami and who am i commands will show the user as root.
But when any other user let say john logs in remotely and runs su –root, whoami will show root, but who am i will show the original user john.
Q. What is a Superuser in Unix?
Superuser is a special user account. It is used for Unix system administration. This user can access all files on the file system. Also Superuser can also run any command on a system.
Generally Superuser permission is given to root user.
Most of the users work on their own user accounts. But when they need to run some additional commands, they can use su to switch to Superuser account.
It is a best practice to not use Superuser account for regular operations.
Q. How will you check the information about a process in Unix?
We can use ps command to check the status of a process in Unix. It is short for Process Status.
On running ps command we get the list of processes that are executing in the Unix environment.
Generally we use ps –ef command. In this e stands for every process and f stands for full format.
This command gives us id of the process. We can use this id to kill the process.
Q. What is the use of more command with cat command?
We generally use cat command to display the contents of a file.
If a file is very big then the contents of the file will not fit in screen, therefore screen will scroll forward and in the end we just see the last page of information from a file.
With more command we can pause the scrolling of data from a file in display. If we use cat command with more then we just see the first page of a file first. On pressing enter button, more command will keep changing the page. In this way it is easier to view information in a file.
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 case because it will display file contents one screen page at a time.
Q. We wrote a shell script in Unix but it is not doing anything. What could be the reason?
After writing a shell script we have to give it execute permission so that it can be run in Unix shell.
We can use chmod command to change the permission of a file in Unix. In general we use chmod +x to give execute permission to users for executing the shell script.
E.g. chmod +x abc.txt will give execute permission to users for executing the file abc.txt.
With chmod command we can also specify to which user/group the permission should be granted. The options are:
Q. What is the significance of 755 in chmod 755 command?
We use chmod command to change the permissions of a file in Unix. In this command we can pass the file permissions in the form of a three-digit number.
In this number 755, first digit 7 is the permissions given to owner, second digit 5 is the permissions of group and third digit 5 is the permissions of all others.
Also the numbers 7 and 5 are made from following rules:
4 = read permission
2 = write permission
1 = execute permission
So 7 = 4 + 2 + 1 = Read + Write + Execute permission
5 = 4 + 1 = Read + Execute permission
In out example 755 means, owner has read, write and execute permissions. Group and others have read and execute permissions.
Q. How can we run a process in background in Unix? How can we kill a process running in background?
In Unix shell we can use symbol & to run a command in background.
E.g. % ls –lrt &
Once we use & option it runs the process in background and prints the process ID. We cannot down this process ID for using it in kill command.
We can also use ps –ef command to get the process ID of processes running in background.
Once we know the process ID of a process we can kill it by following command:
% kill -9 processId
Q. How will you create a read only file in Unix?
We can create a file with Vi editor, cat or any other command. Once the file is created we have to give read only permissions to file. To change file permission to read only we use following command:
% chmod 400 filename
Q. How does alias work in Unix?
We use alias in Unix to give a short name to a long command. We can even use it to combine multiple commands and give a short convenient name.
E.g. alias c=’clear’
With this alias we just need to type c for running clear command.
In bash we store alias in .bash_profile file.
To get the list of all active alias in a shell we can run the alias command without any argument on command line.
alias ki='kill -9'
Q. How can you redirect I/O in Unix?
In Unix we can redirect the output of command or operation to a file instead of command line interface (CLI). For this we sue redirection pointers. These are symbols > and >>.
If we want to write the output of ls –lrt command to a file we use following:
% ls –lrt > fileList.txt
If we want to copy one file to another file we use following:
% cat srcFile > copyFile
If we want to append the contents of one file at the end of another file we use following:
% cat srcFile >> appendToFile
Q. What are the main steps taken by a Unix Shell for processing a command?
A Unix Shell takes following main steps to process a command:
Q. What is a Sticky bit in Unix?
A Sticky bit is a file/directory permission feature in Unix.
Sometimes when we give write permission to another user then that user can delete the file without the owner knowing about it. To prevent such an accidental deletion of file we use sticky bit.
When we mark a file/directory with a sticky bit, no user other than owner of file/directory gets the privilege to delete a file/directory.
To set the sticky bit we use following command:
% chmod +t filename
When we do ls for a file or directory, the entries with sticky bit are listed with letter t in the end of permissions.
E.g. % ls –lrt
-rwxrwxrwt 5 abc abc 4096 Jan 1 10:10 abc.txt
To remove the sticky bit we use following command:
% chmod –t filename
Q. What are the different outputs from Kill command in Unix?
Kill command in Unix can return following outputs:
I. 0: It means Kill command was successful
II. -1: When we get -1 from Kill command it shows that there was some error. In addition to -1 we get EPERM or ESRCH in output.
EPERM denotes that system does not permit the process to be killed.
ESRCH denotes that process with PID mentioned in Kill command does not exist anymore. Or due to security restrictions we cannot access that process.
Q. How will you customize your environment in Unix?
In Unix, almost all the popular shells provide options to customize the environment by using environment variables. To make these customizations permanent we can write these to special files that are specific to a user in a shell.
Once we write our customizations to these files, we keep on getting same customization when we open a new shell with same user account.
The special files for storing customization information for different shells at login time are:
Q. What are the popular commands for user management in Unix?
In Unix we use following commands for User Management:
Q. How will you debug a shell script in Unix?
A shell script is a program that can be executed in Unix shell. Sometimes a shell script does not work as intended. To debug and find the problem in shell script we can use the options provided by shell to debug the script.
In bash shell there are x and v options that can be used while running a script.
% bash –xv <scriptName>
With option v all the input lines are printed by shell. With option x all the simple commands are printed in expanded format. We can see all the arguments passed to a command with –x option.
Q. What is the difference between a Zombie and Orphan process in Unix?
Zombie is a defunct child process in Unix that still has entry in process table.
Sometimes a child process is terminated in Unix, but the parent process still waits on it.
A Zombie process is different from an Orphan process. An orphan process is a child process whose parent process had died. Once a process is orphan it is adopted by init process. So effectively it is not an orphan.
Therefore if a process exits without cleaning its child processes, they do not become Zombie. Instead init process adopts these child processes.
Zombie processes are the ones that are not yet adopted by init process.
Q. How will you check if a remote host is still alive?
We can use one of the networking commands in Unix. It is called ping. With ping command we can ping a remote host.
Ping utility sends packets in an IP network with ICMP protocol. Once the packet goes from source to destination and comes back it records the time.
We can even specify the number of packets we want to send so that we collect more statistics to confirm the result.
% ping www.google.com
Another option is to use telnet to remote host to check its status.
Q. How will you get the last executed command in Unix?
We can use history command to get the list commands that were executed in Unix. Since we are only interested in the last executed command we have to use tail to get the last entry.
Exact command would be as follows:
% history | tail -2
Q. What is the meaning of “2>&1” in a Unix shell?
In Unix shell file descriptor 1 is for standard output.
File description 2 is for standard error.
We can use “2>&1” in a command so that all the errors from standard error go to standard output.
% cat file 2>&1
Q. How will you find which process is taking most CPU time in Unix?
In Unix, we can use top command to list the CPU time and memory used by various processes. The top command lists the process IDs and CPU time, memory etc used by top most processes.
Top command keeps refreshing the screen at a specified interval. So we can see over the time which process is always appearing on the top most row in the result of top command.
This is the process that is consuming most CPU time.
Q. How will you find which processes are using a file?
We can use lsof command to find the list of Process IDs of the processes that are accessing a file in Unix.
Lsof stands for List Open Files.
Sample command is:
% lsof /var
It will list the processes that are accessing /var directory in current unix system.
We can use options –i, -n and –P for different uses.
% lsof –i will only list IP sockets.
Q. What is the purpose of nohup in Unix?
In Unix, nohup command can be used to run a command in background. But it is different from & option to run a process in background.
Nohup stands for No Hangup. A nohup process does not stop even if the Unix user that started the process has logged out from the system.
But the process started with option & will stop when the user that started the process logs off.
Q. How will you remove blank lines from a file in Unix?
We can use grep command for this option. Grep command gives –v option to exclude lines that do not match a pattern.
In an empty line there is nothing from start to end. In Grep command, ^ denotes that start of line and $ denotes the end of line.
% grep –v ‘^$’ lists the lines that are empty from start to the end.
Once we get this result, we can use > operator to write the output to a new file. So exact command will be:
% grep –v ‘^$’ file1.txt > file2.txt
Q. How will you find the remote hosts that are connecting to your system on a specific port in Unix?
We can use netstat command for this purpose. Netstat command lists the statistics about network connections. We can grep for the port in which we are interested.
Exact command will be:
% netstst –a | grep “port number”
Q. What is xargs in Unix?
We use xargs command to build and execute commands that take input from standard input. It is generally used in chaining of commands.
Xargs breaks the list of arguments into small sub lists that can be handled by a command.
Following is a sample command:
% find /path -type f -print | xargs rm
The above command uses find to get the list of all files in /path directory. Then xargs command passes this list to rm command so that they can be deleted.
I am a Software Architect with expertise in Cloud Computing, Amazon Web Services, Microservices, Data Science, Hadoop, Spark, Machine Learning and Java architecture. Learning, using and sharing Technology is my passion.
I have built systems that are running enterprise software of companies across the world. I have gained a lot of knowledge by working hands-on on these large scale software projects.
With these courses I aim to share my knowledge with the future Software Engineers, Developers, Leaders and Architects .
I am sure the knowledge in these courses can give you extra power to win in life.
All the best!!