Top Linux/Unix Interview Questions - Solved

Linux servers are mostly used to deploy software applications. To interact with the server, one must know its commands and should be able to create scripts based on these command call shell scripting in order to automate things or perform some tasks like handling files or automating jobs etc. This page will include interview questions around these commands like sed, awk etc along with shell scripting interview questions. We encourage you to grasp these concepts and logics and do a hands-on before going for an interview.

Q: How to replace a pattern in a file?


sed -i "s/unix/UNIX/g" filename

it will replace 'unix' with 'UNIX' for all the occurance in the file.

Q: How to remove header and trailer?

To remove header :

sed -i "1d" filename

To remove trailer :

sed -i "$d" filename

Q: How to display 10th line of a linux file?


sed -n '10p' filename
head -10 | tail -1

Q: length of a particular line of a linux file?


sed -n '10p' filename | wc -c

Q: Get last word of a line using linux command?


echo "Linux is good" | rev | cut -d " " -f1 | rev

here "-d" is for delimiter, which is single space here.

Q: Remove blank lines from a file using linux command?


sed '/^$/d' filename

Q: Print 3rd to 5th line of a file using linux command?


sed -n '3,5p' filename

Q: Print lines that do not start with word 'panther' using linux command?


sed -n '/panther/!p' filename

Q: Append header using awk linux command?


awk 'BEGIN{printf "Serial \t Name \t Subject \t Grade \n"} {print}' filename.dat

Q: How would you assign value to a variable using awk linux command?


awk -v name=pam 'BEGIN{printf "Name=%s \n", name}

Q: How would you print only those lines which are greater than 25 characters?


awk 'length($0) > 25' file.dat

Q: How would you print only selected columns?


awk '{print$3 "\t" $4}' file.dat

Q: How would count and print matched pattern?


awk '/a/{++cnt} END {print "count = ", cnt}' file.dat

Q: How would print even and odd number of records?

for even :

awk 'NR%2==0' file.dat



for odd :

awk 'NR%2!=0' file.dat

Q: How would you check total size of all the files within a directory?


ls -l | awk 'BEGIN {sum_files=0} {sum_files=sum_files+ $5 } END {print sum_files}'

Q: Explain the basic AWK workflow.

Below workflow is showing how aws command gets executed :

Q: Source has send a DAT file in which data is having multiple delimitors for different files like some lines are space delimited and some are colon. We have to print third field of each line.


awk '{ if($0 ~ /:/){FS=":";} else {FS=" ";} Print$3 }'

Q: Source is sending files continuously as per the schedule to a particular directory. You need to check all the files which are not having any data in order to highlight the source system.

Below command will list down the files having size of zero byte.

ls -lrt | awk '/^-/ { if($5 == 0) print $9 }'

Q: You have a requirement to print line number before each line in a dat file. How would you do it with a linux command?

Below command will perform the required action :

awk '{ print NR , $0 }' filename.dat

Q: While creating files, developer has given incorrect extensions to file. How would you correct the extension with linux command?

Below command will update the file extension :

ls -F | awk '{print " mv "$1" "$1".new " }' | sh

Q: While creating files, your scheduled process has entered some fields in reverse order. How would you print those fields correctly ?

Below command can be used to reverse the fields:

awk ' BEGIN {ORS=""} {for (i=NF;i>0;i--) print$i, " "; print "n"} ' filename.dat

Q: How would you update automatic user id selection in linux ?

This kind of interview question is for admin specific roles.
There is a file in linux called "login.defs" which is usually placed at /etc/ path.
Within this file one can update values of UD_MIN and UID_MAX values.

Q: What are tar file and how would you create it in linux ?

Tar file is a kind of zipped file which can zip multiple files within one single tar file.
To create a tar file use command : tar -cvf .
To read a tar file use command : tar -xvf .