Unix Shell Scripting Interview Questions And Answers:-
Unix interview questions are very important for all programming job interviews because now a days most of the IT companies and investment banks use some forms of Unix servers – Linux, Solaris, IBM AIX etc for trading system and support jobs. Trading systems require the most stable and secure servers which can run continuously without failure. And believe it, Unix servers offer that great stability and security. Hence these Unix interview questions and answers are mostly asked during production support jobs as well as Java development job interviews in various investment banks. Also, As production support kind of tasks of investment banks are mostly outsourced to service based IT companies so this Unix commands tutorial will equally benefit software engineers looking to work in those IT companies.
I have myself worked for an Investment bank for 7 years and trust me, no single day gone without typing a Unix command on a Solaris or Linux machine though my work was not totally based on Unix, it was a mix of JAVA, C, SQL, Perl and Unix. In the following tutorial, I have prepared the interview questions on two broad categories, basic Unix commands and advanced Unix commands. These will be useful for freshers as well as experience holders. I have tried my best to prepare the exhaustive list with examples but it will be great if you guys also can contribute some more Unix interview questions asked during your interview. Please also let me know your feedback about this Unix commands tutorial.
The Brief Overview Of The Unix Operating System:-
Before we jump to the Unix interview questions and answers, lets have a brief look at Unix OS. The full form of Unix is Uniplexed Information and Computing System. Here the letters CS have been transformed to X, for sonority. Unix is a multi-user and multitasking computer operating system. Unix OS is capable of handling activities from multiple users at the same time. Every user can run multiple programs at the same time in Unix. Unix was developed in AT&T’s Bell Labs research center by Ken Thompson, Dennis Ritchie, and others in 1969. Initially Unix operating system was first programmed in assembly language but later in 1973 it was reprogrammed in C programming language. Unix operating systems are widely being used in servers, personal computers and mobile devices. Unix also helped contribute to the explosion of the world wide web by making TCP/IP protocol more widely available.
The UNIX operating system is made up of three parts; the kernel, the shell and the system configuration files. The kernel of Unix is the core of the UNIX operating system. It controls the allocation of time and hardware resources to the programs and handles the file store and communications in response to system calls. Users communicate with the kernel through a program known as the shell. The shell acts as an interface between the user and the kernel. The shell is a command line interpreter; it translates commands entered by the user and converts them into a language that is understood by the kernel.
The Unix operating systems and applications are available for free, but without support or documentation. As a result many different versions of UNIX came into existence, although they share common similarities. Following are the popular varieties of UNIX:-
Ultrix (DEC – Digital Equipment Corporation)
BSD Unix (Berkeley Software Distribution – FreeBSD, OpenBSD)
SCO Unix (SCO Group Inc.)
AIX (IBM – International Business Machines Corporation)
IRIX (SGI – Silicon Graphics Incorporated)
Solaris (Sun – Sun Microsystems)
Mac OS X (Macintosh – Apple Computer, Inc.)
Linux (RedHat, Mandrake, Knopix, SuSE, Debian, Ubuntu …)
Basic Unix Commands Tutorial With Examples:-
What Are The Main Features Of UNIX?
This is one of the very basic Unix interview questions and answers. The main features of Unix are as follows:-
Hierarchical File System
Pipes and Filters
What is LS Command In Unix?
The “ls” command is the most useful command in this Unix commands tutorial. It is used to list down all the files and folders in the current directory. It can be used with multiple options like -l, -r, -t, -a and -h or taking two or more together in options. Lets take a look on the meaning of these options and how you can display the result in your required format. Various Unix commands interview questions can be based on just these options of “ls” command.
- ‘ls –a’ Unix command is used to list down the hidden files in the current directory. The name of the hidden files start with “.”
- ‘ls –l’ command is used to list down the files and folders in alphabetical order.
- ‘ls –lt’ command is used to list down files/folders sorted with modified time.
- ‘ls –ltr’ command is used to list down files/folders sorted with modified time in reverse order.
- ‘ls –lh’ command is used to display file size in human readable format (KB, MB etc.).
How To Use Date Command in Unix?
The date commands are important Unix shell scripting interview questions, it is used to retrieve current date and time. It is also used to display or calculate a date in a format you specify. The super-users (root) use it to set the system clock. Without using any options, the Unix date command displays the current system date and time, including the day of the week, month, time, timezone, and year. Date command has many formatting options, they can be found using “date –help” command. It can be used to determine the day of the week which is very useful in Unix scripts and sometime naming the command logs.
What Is The Cut Command In Unix?
The cut command in Unix is the utility used for text processing, It is used to cut sections from every line of file and write the result to standard output. In simple terms you can use this command to extract portion of text from the file by selecting columns by byte position, character and delimiter.
What is Alias Command in Unix?
The alias command in Unix allows a user to create simple short names or even consisting of just a single character for long complex commands and then use them in the same way that ordinary commands are used. The syntax to create alias is as follows:
alias shortname='This is a very long command'
Please note that aliases created in this way are recognized only by the shell session in which they are created, and they work only for the user who creates them. When the shell session is closed the alias will be lost. To make an alias persistent on every login you can simply add them to your .profile file as this file is user specific file and this is executed every time a user logs in to shell. Following are some Unix commands tutorial examples you can use to add aliases to your .profile. Similarly you can add any of the frequently used long commands there to save your time daily.
alias rm='rm -i' alias ..='cd ..' alias c='clear' alias l='ls -lrt' alias h='history'
How To Run Command In Background In Unix?
When you want to start a Unix command that is going to take a longer time to finish, you just don’t want to monitor it till it completes. In such a case you can run the command in background simply by adding ‘&’ sign after it. Further you can redirect the log to appropriate files so that they don’t appear on your terminal and meanwhile you can execute some other process without waiting for the previous process to complete. Learning to run command in background is of immense importance in the Unix commands tutorial, lets learn one more way to do it in the next question.
What Is Nohup In Unix?
There are some huge processes which take many hours to finish. The ‘nohup’ is a special command to run a process in the background. The process started with nohup Unix command does not terminate even after the user who started the command logs off from the system. So if you know that you are going to log off sometime soon and the process will take longer than you can wait, then use the nohup rather than & sign.
How To Use Tail Command In Unix?
The tail command in Unix is prints the last 10 lines of the file when used without options like the example below.
$ tail filename.txt
If you want to print the last n lines then use -n option with tail. Replace n with any number for example “tail -100 filename.txt”.
This Unix command is also useful to view the log file of any process while the process actually running, the log file will keep growing and you can monitor what happening in that process currently. The command can be terminated anytime using CTRL+C.
$ tail -f processname.log
How To Count Number Of Lines In A File In Unix?
The wc (short for word count) is a command in Unix operating systems. The command can use to retrieve the number of bytes, number of characters, number of words or the number of lines in a file using various options like -c, -m, -w or -l respectively.
Example syntax is “wc –l filename” – this will return number of lines in the filename.
In a file word test is appearing many times? How will you count number?
This is another good questions in Unix commands tutorial and it is also an important Unix interview question and answer. If you just grep the word “test” in the file then the command will show you all lines which contain the word in question. But we have to keep in mind that the word “test” may appear multiple times in a single line, also what if the word “test” appear somewhere as “Test”. You know that Unix is case sensitive. So you have to use -i option along with -o option to grep command to be on safer side. Have a look on the following example of the Unix commands tutorial:
What Is Uptime Command In Unix?
The “uptime” command in Unix operating Systems is used to find the number of days that the Server is up. It is one of the important Unix interview questions and answers as you can be indirectly asked to find when was the last time the server got rebooted. And your answer should be to use the uptime command.
How To Use Find Command In Unix?
The “find” Unix command can be extremely useful if you’ve forgotten in which directory you put a file, and you don’t even remember the full name of the file but just some part of the file name you remember. Use the find command with -iname options to ignore the case and use wildcard character with the file name as shown in the below Unix command example. The command will search all the filenames matching your filename string in that directory and all sub directories under that. So its better to run the command from the root directory so that all the occurrences of the file can be searched. Due to its usage this command is also very important from Unix interview questions and answers point of view.
How To Compare Two Files in Unix
Sometimes we may need to check if the two text files are same or there are any differences between the contents of both files. There are two ways to compare the text files depending on what result we want to see.
Following cmp unix command compares two files byte by byte and if there is difference then the byte and line number at which the first difference occurred is reported. But if there is no difference then there is no output.
$ cmp file1 file2
Following diff command compares the contents of two files. Whatever content differ in two files is displayed.
$ diff file1 file2
There are various options available with diff command on which many difficult Unix interview questions can be formed. For example diff command used with:
-b option ignores changes in amount of white spaces. -B option ignores changes due to blank lines. -i option ignores changes due to upper case and lower case letters. -w option ignores white space when comparing lines, similar to -b option. -y option to display output side by side.
Describe the gzip/gunzip Unix commands?
File compression Unix commands interview questions are very common as there are multiple compression and decompression utilities available with many options. The gzip command creates a compressed file in the same directory. After the file is compressed, at some stage you may need to uncomress the file and gunzip command uncompresses the file compressed by gzip command. Following are some file compression and decompression unix commands one must know for production support jobs as well as technical interview purpose:-
$ gzip filename — will compress the file and name will become filename.gz. Original file will disappear.
$ gzip -d filename.gz -- will uncomress the file. $ gunzip filename.gz -- uncompress the filename.gz and remove the .gz from filename. $ gunzip -c filename.gz -- view the contents of compressed file on screen. $ gunzip -f filename.gz -- force to uncompress and override the uncompressed file name without asking for user confirmation.
There are other utilities as well to compress the files like bzip2 and bunzip2 which work similar way as gzip and gunzip unix commands.
What Is Free Command In Unix?
The free Unix command provides information about used and available space of physical memory and swap memory on any computer running on Linux/Unix like operating systems. If command is run without any option then it will display result in bytes. You can also get the result in Kb, MB, or GB by using -k, -m or -g options respectively as shown in the following free Unix command with example using -g option. Use it with -t option to see the total memory in a separate line. This is a very handy command in Unix commands tutorial as many a times we have to quickly check the available space in the file system. You can easily keep an eye on the available free memory to avoid any last minute troubles.
What Are Pipes In Unix/Linux Programming ?
Pipes are used to connect two or more Unix commands together. The output of the first command works as the input for the second command, and so on. The vertical bar symbol ( | ) represents pipe in Unix and Unix-like operating systems.
Lets understand pipes with an simple example. In the following command we have used cat command on a file that is big enough to span on multiple pages and doesn’t fit on the single screen. With only “cat filename” we will see the last page of the file and all other pages will quickly move up. However if we pipe the output of the ‘cat’ command to ‘less’ command then we will see one page at a time and pressing space-bar will display further pages.
$ cat filename | less
You can also use ‘more’ or ‘pg’ in place of ‘less’ for the same purpose.
Explain Some Filter Unix Commands With Examples.
A filter is a small program in Unix-like operating systems that transforms plain text into highly specific results by performing some actions on it. The plain text may be in the form of huge file and the specific result may be the small set of expected result extracted from that huge file. Following is the very useful and one of the basic Unix commands with example of a filter, the ‘grep’ command. This program look for a certain pattern in a file or list of files and displays only those lines on the screen which contains the given pattern.
There are numerous other filters available on Unix-like systems, a few of which are awk, cat, cut, grep, head, less, more, sed, sort, tail and wc.
Advanced Unix Commands Tutorial With Examples:-
What Does It Mean By Super User In Unix?
The user with access to all files and commands on a system is called super user. Generally, super user login is to root account protected by root password and has the ability to run any command without any restriction. Super user also has ability to login to user accounts without requiring the password.
What is “chmod” Command?
The chmod command in Unix/Linux and like operating systems is used to change the access permissions for a file or directory. The access permissions are read (r), write (w) and execute (x). The type of users is user (u), group (g) and all other users (o). If you need to give execute permission to yourself on a Unix shell script which you just have created for some purpose, then you will need do “chmod u+x script.sh”. You will not be able to execute the your shell script without giving yourself the execute permission on that script.
Following Unix command example gives the full access to the group on a specific file.
$ chmod g+rwx file.txt
And the below command revokes all access from the group on a specific file.
$ chmod g-rwx file.txt
Read more about file permissions in Unix like operating systems.
How To Find The Currently Running Process In Unix Server?
This is indeed a very good Unix commands interview Question. “ps –ef” command is used to display information of currently running process. Use it with pipe and grep for any specific process. There is one more command “ps –auwwx” which can also be used for the same purpose. The difference between “ps –ef” and “ps –auwwx” commands is that the first one omits the process with very long command line but the later one will list out those commands as well.
How To Find IP Address From Host Name in Unix?
In this Unix interview question and answer, we will learn how to find IP address of any host in a UNIX based system e.g. Linux, Solaris etc. You always need either IP address or host name to connect to any host in a UNIX based network. Following is the list of UNIX commands which can be used to find IP address of any host:
ifconfig -- Can only check IP address of the local host. nslookup -- Can be used to find IP address of both local host and remote host. host -- Can be used to find IP address of both local host and remote host.
What is Zombie Process in UNIX?
This can be one of the challenging and advanced Unix commands interview question. To better understand the concept of zombie process, we should be aware of what is a process, parent process and child process, fork() and wait() system calls etc. A process is a set of instructions that are to be executed by the operating system. When the process is executed, its entry is made into the process table in the memory. Depending on operating system and how big is the process, there can be multiple threads of a process. The main program is called the parent process which executes a fork() system call to create other threads called child processes. The parent process keeps a track of all child process’s exit status once child process completes by executing wait() system call, this wait() system call removes child process id and all its information completely from memory.
On Unix/Unix like operating systems, a zombie process is a process that has completed execution but still has an entry in the process table. This entry is still needed to allow the parent process to read the child’s exit status. However, if a parent fails to execute wait() system call, the child process will be left in the process table. This child process is called a zombie process which is actually dead/completed but its process id is left as if it was alive.
How Do You Find Zombie Process in UNIX And How to Kill it?
Utilities like the top Unix command and the ps Unix command display zombie processes. Unlike normal processes, the kill command will not be able to kill a zombie process. One way to kill the zombie process is by sending the SIGCHLD signal to the parent process. This signal tells the parent process to execute the wait() call and clean up its zombie children from memory. You can send the signal with the kill command as shown below, replace pid in the command with the parent’s process PID:
$ kill -s SIGCHLD pid
However, if the parent process is not programmed properly and is ignoring SIGCHLD signals, this Unix command won’t help. You’ll then have to kill or close the zombies’ parent process. Just like the previous question, this Unix interview question is also equally important.
Explain Mount and Unmount Unix Commands.
Mount command: As the name suggests, the “mount” command attaches a storage device or file system onto an existing directory structure and thus making it accessible to users.
Unmount command: The “unmount” command informs the system to complete any pending read and write operations and then safely detaches the mounted file system.
How do you find which processes are using a particular file?
Sometimes you may need to rename, remove or move the file, directory or mount from perticular Unix file system but you can’t do that as you get the error saying file is busy or in other words being used by some process. Following ‘fuser’ and ‘lsof’ Unix command examples are useful to find the process ids of all the processes using the filename. Once you get the process ids then you can further investigate and decide what to do with those processes before taking any action on the file, directory or mount. Here, since the usage of these commands is important for production support hence it is one of the important Unix commands interview questions one must be aware of while preparing for technical interviews.
$ lsof filename $ fuser filename
Enlist Some Commonly used Network Unix Commands.
There are many Unix interview questions formed based on the network commands. Following are the commonly used networking Unix commands:-
- telnet: it is used for remote login as well as to check whether the remote host is alive.
- ping: used for checking network connectivity to a remote system and speed of connection.
- traceroute: This command provides a list of all the routers and response time when reaching out to a remote system.
- hostname: determines the IP address and domain name of the system on which the user is currently logged in.
- nslookup: performs DNS query to find the IP address for particular system name and vice-versa.
- whois: You can find a lot more information about a domain using the whois command.
- netstat: it tells about ongoing network connection on the local system and ports, routing tables, interfaces statistics, etc.