01: 14 Unix must-know interview questions & answers

Q1 How do you remove the Control-M characters from a file?
A1 Control-M is a carriage return on keyboard. The ^M is the keyboard equivalent to \r. In a file originated from DOS/Windows the \r\n is used for an end of line carriage return, whereas in Unix it is \n for a new line.

So, if created a file in DOS/Windows and copied it to a Unix machine, you need to convert the carriage returns from \r\n to \n. You need to remove \r.

Using the sed command that replaces Control-M with nothing

Note: The ^M is typed on the command line with ctrl+v and ctrl+M

You can also use a vi editor and type :%s/^M//g to remove the control-M characters.

Q2 How will you search for a property named “inbox” within a number of .properties files including all sub folders?
A2 Using the find and grep commands. Yoo can use xargs command or -exec attribute. xargs is faster.

/dev/null means a null device or a black hole. It suppresses all messages that might be issued by the executed command.

\; executes a separate command for each file found by find, whereas \+ appends the files and executes a single command on all of them. The \ is an escape character for ;. Just ; means end of command for the find, but \; means end of command for -exec attribute.

| (i.e. pipe) in Unix is used to combine two or more commands, and in this, the output of one command (i.e. find) acts as input to another command (i.e xargs), and this command’s output may act as input to the next command, and so on.

The above -exec will execute:

and xargs can do in a single line:

The file1, file2, etc are returned by the find command and substituted in the place holder {}. {} is the default place holder, and in xargs you can change the place holder with the -I option.

Q3 How will you run a shell script even after you log out of the session?
A3 “nohup” to run even after logging out of the session. & to run as a background process. The output will be directed to nohup.out

If you don’t want the output to be directed to nohup.out.

> /dev/null 2>&1 means that stderr (i.e. 2) is redirected (>&) to stdout (i.e. 1) and that the latter is being redirected to /dev/null (i.e. null device or a black hole). In short the output and error messages are suppressed.

& at the end means run as a background process.

Q4 How will you identify the jar file that has a particular class or resource file?
A4 For example, to find the jar files that has the “MyConnection.class” class file. This is handy for identifying class loading issues in Java.

With -print0, find will separate the full filenames in the output with a null character (i.e. a zero byte) instead of the newline character that -print uses. xargs must then be called with -0. This allows file names that contain newlines or other types of white space to be correctly interpreted by programs that process the find output.

Q5 How will you reuse some of the commands that you have already used?
A5 Using the “history” command.

Q6 How will you find the list of log files that have userid “B1234”?
A6 Using the “grep” command with option “l”.

1. Use the up arrow to view the previous command and press enter to execute it.
2. Type !! and press enter from the command line to execute the last command again
3. Type !-1 and press enter from the command line to execute the last command !-2 for command before the last and so on.
4. Type history followed by enter, which prints a list of last few commands, and then !25 to execute a particular command

Q7 What do you uderstand by 2>&1 in UNIX?
A7 In UNIX you have STDIN, which is denoted by number 0, STDOUT, which is denoted by 1, and STDERR, which is denoted by 2. So, the above line means, the error messages go to STDERR, which is redirected to STDOUT. So, the error messages go to where ever the STDOUT goes to. For example, The following command creates a multiple directories for a maven based Java project. if the the directory creation is successful, change directory to “project” and print the directory tree structure. The STDOUT and STDERR are directed to the file named maven-project.log under the project folder.

The output will be something like

Q8 What is /dev/null?
A8 It is a blackhole.

will fail silently and nothing will be printed if there is no “temp” folder. The message has gone into the blackhole. If there is a “temp” folder, the present working directory (i.e. pwd) will be printed out.

What is $(command;)?

$(pwd) means executing the command pwd in a subshell. The command pwd outputs the present working directory. You will see another example below that lines 1 to 4 from a file named accounts.patch. The command is executed in a sub (aka child) shell.

Q8 How would you go about the following scenario — you had to move to another directory temporarily to look at a file, and then move back to the directory where you were?
A8 One way is to when you are already in /tmp folder

The better way is to use the “pushd” and “popd” commands. These commands make use of a “stack” data structure using the “Last In First Out” approach. when you are already in /tmp folder

changes to the /projects/JMeter folder and prints the stack

The /projects/JMeter will be popped out of the stack and the directory will change back to /tmp.

If you want pushd to not print the stack, you could direct the output to the black hole /dev/null as shown below.

The above is a trivial example, but in real life, you may want to navigate between more number of directories and this stack based approach will come in very handy without having to use the “cd” command. Also, very useful in Shell scripts. Use it astutely without having to build up a gigantic directory stack full of useless directories.

Q9 In Unix, only nine command line arguments can be accessed using positional parameters. How would you go about having access to more than 9 argumnets?
A9 Using the “shift” command. For example, in unix, when you run a command like

The $0 is test-bash.sh, and $1 is file1, $2 file2 and so on till $9, which is file9. In the program, if you want to access file10 after processing file1 to file9, you need to use the “shift” command.

All it does is move all the command line arguments to the left by 1 position. Which means the file1 will be moved out, and file2 becomes $1 and file10 becomes $2. If you shift it agian, the file3 becomes $1 and file11 becomes $9. In a nutshell

$# – Total number of arguments.

$0 – Command or the script name.

$1, $2, $3 – First, second and third args respectively.

$* – All the command line arguments starting from $1.

$@ – Same as $* except when it is quoted “$*” expands to $1c$2c$3c… where c is the first character of $IFS, bash’s internal field separator variable.

Parameters greater than 9 can be accessed using curly braces around the number; for instance, ${10} would be the tenth parameter, and ${100} would be the 100th. The curly braces can be used with $*, etc as ${*}.

Q10 How will you empty or clear the contents of a file?
A10 Direct /dev/null (i.e. black hole) to the file

Q11 What does if [ $val -eq $? ] mean in a shell script?
A11 $? returns the exit status code of the last command. 0 means success and other numbers mean failure exit codes.

Q12 How will you go about concatenating the account numbers in a number of text files to a single file?

Let’s say we have a accounts.patch file with 4 files containing account numbers.

A12 Firstly, cd to the folder where the “accounts.patch” file is, and then type the following command on a shell command line.

Explanation:

-n option in sed means disable automatic printing.

1, 4p in sed means print lines 1 to 4 from accounts.patch

for txt-file in $(sed -n ‘1,4p’ account_sqls.patch); means for each line read from accounts.patch.

do cat $txt-file >> accounts_combined.txt means append (i.e >>) the contents of each file to a different file “accounts_combined.txt”.

Q13 How will you go about executing multiple maven commands on unix? For example, build two separate projects one after another, for example — project1 and project2. The project2 to should only build if project1 successfully builds.
A13 Use the && control operator to combine two commands so that the second is run only if the first command returns a zero exit status. In other words, if the first command runs successfully, the second command runs. If the first command fails, the second command does not run at all. For example:

Similarly, the || control operator separates two commands and runs the second command only if the first command returns a non-zero exit status, that is if it fails. In other words, if the first command is successful, the second command does not run. This operator is often used when testing for whether a given directory exists and, if not, it creates one.

the -p option allows you to create any relevant parent directories as well. The && and || control characters can be used together.

Another control character that separates commands is ;, which means run the second command regardless of what the exit status of the previous command is.

Q14 How will you go about listing all the users’ cron jobs?
A14 If you are a root user, you can list the cron jobs for all the users as shown below:

-d: in cut command means : is used as the delimiter to separate each column.

-f1 means cut or extract the first column as output.

To edit cron jobs

To list the jobs

More Unix interview Q&As

100+ Unix/Linux interview questions & answers


300+ Java Interview FAQs

Tutorials on Java & Big Data