0% found this document useful (0 votes)
3 views15 pages

Unix Interview Questions

The document provides a comprehensive list of Unix interview questions and answers, covering various commands and their usage. It includes topics such as file manipulation, string processing, system information retrieval, and command history. Each question is accompanied by a command or explanation, making it a useful resource for preparing for Unix-related interviews.

Uploaded by

divyadara63
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views15 pages

Unix Interview Questions

The document provides a comprehensive list of Unix interview questions and answers, covering various commands and their usage. It includes topics such as file manipulation, string processing, system information retrieval, and command history. Each question is accompanied by a command or explanation, making it a useful resource for preparing for Unix-related interviews.

Uploaded by

divyadara63
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Top Unix Interview Questions - Part 1

1. How to display the 10th line of a file?


head -10 filename | tail -1

2. How to remove the header from a file?


sed -i '1 d' filename

3. How to remove the footer from a file?


sed -i '$ d' filename

4. Write a command to find the length of a line in a file?


The below command can be used to get a line from a file.
sed –n '<n> p' filename
We will see how to find the length of 10th line in a file
sed -n '10 p' filename|wc -c

5. How to get the nth word of a line in Unix?


cut –f<n> -d' '

6. How to reverse a string in unix?


echo "java" | rev

7. How to get the last word from a line in Unix file?


echo "unix is good" | rev | cut -f1 -d' ' | rev

8. How to replace the n-th line in a file with a new line in Unix?
sed -i'' '10 d' filename # d stands for delete
sed -i'' '10 i new inserted line' filename # i stands for insert

9. How to check if the last command was successful in Unix?


echo $?

10. Write command to list all the links from a directory?


ls -lrt | grep "^l"

11. How will you find which operating system your system is running on in UNIX?
uname -a

12. Create a read-only file in your home directory?


touch file; chmod 400 file

13. How do you see command line history in UNIX?


The 'history' command can be used to get the list of commands that we are executed.

14. How to display the first 20 lines of a file?


By default, the head command displays the first 10 lines from a file. If we change the option of
head, then we can display as many lines as we want.
head -20 filename
An alternative solution is using the sed command
sed '21,$ d' filename
The d option here deletes the lines from 21 to the end of the file

15. Write a command to print the last line of a file?


The tail command can be used to display the last lines from a file.
tail -1 filename
Alternative solutions are:
sed -n '$ p' filename
awk 'END{print $0}' filename

Top Unix Interview Questions - Part 2

1. How do you rename the files in a directory with _new as suffix?


ls -lrt|grep '^-'| awk '{print "mv "$9" "$9".new"}' | sh

2. Write a command to convert a string from lower case to upper case?


echo "apple" | tr [a-z] [A-Z]

3. Write a command to convert a string to Initcap.


echo apple | awk '{print toupper(substr($1,1,1)) tolower(substr($1,2))}'

4. Write a command to redirect the output of date command to multiple files?


The tee command writes the output to multiple files and also displays the output on the terminal.
date | tee -a file1 file2 file3

5. How do you list the hidden files in current directory?


ls -a | grep '^\.'

6. List out some of the Hot Keys available in bash shell?

 Ctrl+l - Clears the Screen.


 Ctrl+r - Does a search in previously given commands in shell.
 Ctrl+u - Clears the typing before the hotkey.
 Ctrl+a - Places cursor at the beginning of the command at shell.
 Ctrl+e - Places cursor at the end of the command at shell.
 Ctrl+d - Kills the shell.
 Ctrl+z - Places the currently running process into background.

7. How do you make an existing file empty?


cat /dev/null > filename

8. How do you remove the first number on 10th line in file?


sed '10 s/[0-9][0-9]*//' < filename

9. What is the difference between join -v and join -a?


join -v : outputs only matched lines between two files.
join -a : In addition to the matched lines, this will output unmatched lines also.

10. How do you display from the 5th character to the end of the line from a file?
cut -c 5- filename
Top Unix Interview Questions - Part 3

1. Display all the files in current directory sorted by size?


ls -l | grep '^-' | awk '{print $5,$9}' |sort -n|awk '{print $2}'

2. Write a command to search for the file 'map' in the current directory?
find -name map -type f

3. How to display the first 10 characters from each line of a file?


cut -c -10 filename

4. Write a command to remove the first number on all lines that start with "@"?
sed '\,^@, s/[0-9][0-9]*//' < filename

5. How to print the file names in a directory that has the word "term"?
grep -l term *
The '-l' option make the grep command to print only the filename without printing the content of
the file. As soon as the grep command finds the pattern in a file, it prints the pattern and stops
searching other lines in the file.

6. How to run awk command specified in a file?


awk -f filename

7. How do you display the calendar for the month march in the year 1985?
The cal command can be used to display the current month calendar. You can pass the month
and year as arguments to display the required year, month combination calendar.
cal 03 1985
This will display the calendar for the March month and year 1985.

8. Write a command to find the total number of lines in a file?


wc -l filename
Other ways to pring the total number of lines are
awk 'BEGIN {sum=0} {sum=sum+1} END {print sum}' filename
awk 'END{print NR}' filename

9. How to duplicate empty lines in a file?


sed '/^$/ p' < filename

10. Explain iostat, vmstat and netstat?

 Iostat: reports on terminal, disk and tape I/O activity.


 Vmstat: reports on virtual memory statistics for processes, disk, tape and CPU activity.
 Netstat: reports on the contents of network data structures.

Top Unix Interview Questions - Part 4

 1. How do you write the contents of 3 files into a single file?


cat file1 file2 file3 > file
2. How to display the fields in a text file in reverse order?
awk 'BEGIN {ORS=""} { for(i=NF;i>0;i--) print $i," "; print "\n"}' filename

3. Write a command to find the sum of bytes (size of file) of all files in a directory.
ls -l | grep '^-'| awk 'BEGIN {sum=0} {sum = sum + $5} END {print sum}'

4. Write a command to print the lines which end with the word "end"?
grep 'end$' filename
The '$' symbol specifies the grep command to search for the pattern at the end of the
line.

5. Write a command to select only those lines containing "july" as a whole word?
grep -w july filename
The '-w' option makes the grep command to search for exact whole words. If the
specified pattern is found in a string, then it is not considered as a whole word. For
example: In the string "mikejulymak", the pattern "july" is found. However "july" is not a
whole word in that string.

6. How to remove the first 10 lines from a file?


sed '1,10 d' < filename

7. Write a command to duplicate each line in a file?


sed 'p' < filename

8. How to extract the username from 'who am i' comamnd?


who am i | cut -f1 -d' '

9. Write a command to list the files in '/usr' directory that start with 'ch' and then display
the number of lines in each file?
wc -l /usr/ch*
Another way is
find /usr -name 'ch*' -type f -exec wc -l {} \;

10. How to remove blank lines in a file ?


grep -v ‘^$’ filename > new_filename

Top Unix Interview Questions - Part 5

 1. How to display the processes that were run by your user name ?
ps -aef | grep <user_name>

2. Write a command to display all the files recursively with path under current directory?
find . -depth -print

3. Display zero byte size files in the current directory?


find -size 0 -type f

4. Write a command to display the third and fifth character from each line of a file?
cut -c 3,5 filename

5. Write a command to print the fields from 10th to the end of the line. The fields in the
line are delimited by a comma?
cut -d',' -f10- filename

6. How to replace the word "Gun" with "Pen" in the first 100 lines of a file?
sed '1,00 s/Gun/Pen/' < filename

7. Write a Unix command to display the lines in a file that do not contain the word
"RAM"?
grep -v RAM filename
The '-v' option tells the grep to print the lines that do not contain the specified pattern.

8. How to print the squares of numbers from 1 to 10 using awk command


awk 'BEGIN { for(i=1;i<=10;i++) {print "square of",i,"is",i*i;}}'

9. Write a command to display the files in the directory by file size?


ls -l | grep '^-' |sort -nr -k 5

10. How to find out the usage of the CPU by the processes?
The top utility can be used to display the CPU usage by the processes.

Top Unix Interview Questions - Part 6

 1. Write a command to remove the prefix of the string ending with '/'.
The basename utility deletes any prefix ending in /. The usage is mentioned below:
basename /usr/local/bin/file
This will display only file

2. How to display zero byte size files?


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

3. How to replace the second occurrence of the word "bat" with "ball" in a file?
sed 's/bat/ball/2' < filename

4. How to remove all the occurrences of the word "jhon" except the first one in a line with
in the entire file?
sed 's/jhon//2g' < filename

5. How to replace the word "lite" with "light" from 100th line to last line in a file?
sed '100,$ s/lite/light/' < filename

6. How to list the files that are accessed 5 days ago in the current directory?
find -atime 5 -type f

7. How to list the files that were modified 5 days ago in the current directory?
find -mtime 5 -type f

8. How to list the files whose status is changed 5 days ago in the current directory?
find -ctime 5 -type f

9. How to replace the character '/' with ',' in a file?


sed 's/\//,/' < filename
sed 's|/|,|' < filename

10. Write a command to find the number of files in a directory.


ls -l|grep '^-'|wc -l

Top Unix Interview Questions - Part 7

 1. Write a command to display your name 100 times.


The Yes utility can be used to repeatedly output a line with the specified string or 'y'.
yes <your_name> | head -100

2. Write a command to display the first 10 characters from each line of a file?
cut -c -10 filename

3. The fields in each line are delimited by comma. Write a command to display third field
from each line of a file?
cut -d',' -f2 filename

4. Write a command to print the fields from 10 to 20 from each line of a file?
cut -d',' -f10-20 filename

5. Write a command to print the first 5 fields from each line?


cut -d',' -f-5 filename

6. By default the cut command displays the entire line if there is no delimiter in it. Which
cut option is used to supress these kind of lines?
The -s option is used to supress the lines that do not contain the delimiter.

7. Write a command to replace the word "bad" with "good" in file?


sed s/bad/good/ < filename

8. Write a command to replace the word "bad" with "good" globally in a file?
sed s/bad/good/g < filename

9. Write a command to replace the word "apple" with "(apple)" in a file?


sed s/apple/(&)/ < filename

10. Write a command to switch the two consecutive words "apple" and "mango" in a file?
sed 's/\(apple\) \(mango\)/\2 \1/' < filename

11. Write a command to display the characters from 10 to 20 from each line of a file?
cut -c 10-20 filename

Top Unix Interview Questions - Part 8

 1. Write a command to print the lines that has the the pattern "july" in all the files in a
particular directory?
grep july *
This will print all the lines in all files that contain the word “july” along with the file name.
If any of the files contain words like "JULY" or "July", the above command would not
print those lines.

2. Write a command to print the lines that has the word "july" in all the files in a directory
and also suppress the filename in the output.
grep -h july *

3. Write a command to print the lines that has the word "july" while ignoring the case.
grep -i july *
The option i make the grep command to treat the pattern as case insensitive.

4. When you use a single file as input to the grep command to search for a pattern, it
won't print the filename in the output. Now write a grep command to print the filename in
the output without using the '-H' option.
grep pattern filename /dev/null
The /dev/null or null device is special file that discards the data written to it. So, the
/dev/null is always an empty file.
Another way to print the filename is using the '-H' option. The grep command for this is
grep -H pattern filename

5. Write a command to print the file names in a directory that does not contain the word
"july"?
grep -L july *
The '-L' option makes the grep command to print the filenames that do not contain the
specified pattern.

6. Write a command to print the line numbers along with the line that has the word "july"?
grep -n july filename
The '-n' option is used to print the line numbers in a file. The line numbers start from 1

7. Write a command to print the lines that starts with the word "start"?
grep '^start' filename
The '^' symbol specifies the grep command to search for the pattern at the start of the
line.

8. In the text file, some lines are delimited by colon and some are delimited by space.
Write a command to print the third field of each line.
awk '{ if( $0 ~ /:/ ) { FS=":"; } else { FS =" "; } print $3 }' filename

9. Write a command to print the line number before each line?


awk '{print NR, $0}' filename

10. Write a command to print the second and third line of a file without using NR.
awk 'BEGIN {RS="";FS="\n"} {print $2,$3}' filename

11. How to create an alias for the complex command and remove the alias?
The alias utility is used to create the alias for a command. The below command creates
alias for ps -aef command.
alias pg='ps -aef'
If you use pg, it will work the same way as ps -aef.
To remove the alias simply use the unalias command as
unalias pg
12. Write a command to display todays date in the format of 'yyyy-mm-dd'?
The date command can be used to display todays date with time
date '+%Y-%m-%d'

1.Write a command to display a file’s contents in various formats?


$od -cbd file_name c – character, b – binary (octal), d-decimal, od=Octal Dump.

2. What will the following command do?


$ echo *

It is similar to ‘ls’ command and displays all the files in the current directory.
3. Is it possible to create new a file system in UNIX?
Yes, ‘mkfs’ is used to create a new file system.

4. Is it possible to restrict incoming message?


Yes, using the ‘mesg’ command.

5.. What is the use of the command “ls -x chapter[1-5]”

ls stands for list; so it displays the list of the files that starts with ‘chapter’ with suffix ’1′ to ’5′,
chapter1,
chapter2, and so on.

6. Is ‘du’ a command? If so, what is its use?


Yes, it stands for ‘disk usage’. With the help of this command you can find the disk capacity
and free space
of the disk.

7. Is it possible to count number char, line in a file; if so, How?


Yes, wc-stands for word count.
wc -c for counting number of characters in a file.
wc -l for counting lines in a file.

8. Name the data structure used to maintain file identification?

‘inode’, each file has a separate inode and a unique inode number.

9. How many prompts are available in a UNIX system?

Two prompts, PS1 (Primary Prompt), PS2 (Secondary Prompt).


10. How does the kernel differentiate device files and ordinary files?

Kernel checks ‘type’ field in the file’s inode structure.

11. How to switch to a super user status to gain privileges?


Use ‘su’ command. The system asks for password and when valid entry is made the user
gains super user
(admin) privileges.
12. What are shell variables?
Shell variables are special variables, a name-value pair created and maintained by the shell.
Example: PATH, HOME, MAIL and TERM
13. What is redirection?
Directing the flow of data to the file or from the file for input or output.
Example : ls > wc
14. How to terminate a process which is running and the specialty on command kill 0?
With the help of kill command we can terminate the process.
Syntax: kill pid
Kill 0 – kills all processes in your system except the login shell.
15. What is a pipe and give an example?
A pipe is two or more commands separated by pipe char ‘|’. That tells the shell to arrange for
the output of
the preceding command to be passed as input to the following command.
Example : ls -l | pr
The output for a command ls is the standard input of pr.
When a sequence of commands are combined using pipe, then it is called pipeline.
16. Explain kill() and its possible return values.
There are four possible results from this call:
‘kill()’ returns 0. This implies that a process exists with the given PID, and the system would
allow you to
send signals to it. It is system-dependent whether the process could be a zombie.
‘kill()’ returns -1, ‘errno == ESRCH’ either no process exists with the given PID, or security
enhancements
are causing the system to deny its existence. (On some systems, the process could be a
zombie.)
‘kill()’ returns -1, ‘errno == EPERM’ the system would not allow you to kill the specified
process. This means
that either the process exists (again, it could be a zombie) or draconian security
enhancements are present
(e.g. your process is not allowed to send signals to *anybody*).
‘kill()’ returns -1, with some other value of ‘errno’ you are in trouble! The most-used
technique is to assume
that success or failure with ‘EPERM’ implies that the process exists, and any other error
implies that it
doesn’t.
An alternative exists, if you are writing specifically for a system (or all those systems) that
provide a ‘/proc’
filesystem: checking for the existence of ‘/proc/PID’ may wWhat is relative path and absolute
path.
Absolute path : Exact path from root directory.
Relative path : Relative to the current path may work.

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"
2. 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
read more about file and directory permission in unix and linux here.

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

4. 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.

5. 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.

6. 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.

7. How do you copy file from one host to other?


Many options but you can say by using "scp" command. You can also use rsync command to
answer this UNIX interview question or even sftp would be ok.
8. 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.

9. 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.

10. 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.

2. 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

3. 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

4. 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.

5. What is "chmod" command? What do you understand by this line “r-- -w- --x?

6. 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

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

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

9. 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

10. How do you find whether your system is 32 bit or 64 bit ?


Either by using "uname -a" command or by using "arch" command.

11. How to remove blank line in a file in vi mode?

:g/^$/d

The "g" command tells to find every line that matches the pattern "^$" and
perform an operation on it. In this case, the operation commanded is "d" for
delete.

1. What does the command “$ls | wc –l > file1” do?

ls becomes the input to wc which counts the number of lines it receives as input and instead of
displaying this count , the value is stored in file1.

2.Which of the following commands is not a filter

a)man , (b) cat , (c) pg , (d) head


Ans: man
A filter is a program which can receive a flow of data from std input, process (or filter) it and
send the result
to the std output.

4. What difference between cmp and diff commands?

cmp – Compares two files byte by byte and displays the first mismatch diff – tells the changes to
be made to make the files identical.
5. What is the use of ‘grep’ command?
‘grep’ is a pattern search command. It searches for the pattern, specified in the command line
with
appropriate option, in a file(s).
Syntax : grep
Example : grep 99mx mcafile
6. What is the difference between cat and more command?
Cat displays file contents. If the file is large the contents scroll off the screen before we view it.
So command ‘more’ is like a pager which displays the contents page by page.
7.Write a command to kill the last background job?
Kill $!
8.Which command is used to delete all files in the current directory and all its sub-directories?

rm -r *

How to remove the first line / header from a file?

sed –i '1 d' file.txt

How to remove the last line/ trailer from a file in Unix

sed –i '$ d' file.txt

How to remove certain lines from a file in Unix?

sed –i '5,7 d' file.txt

How to get the last word from a line in Unix file?

$>echo "C for Cat" | rev | cut -f1 -d' ' | rev
Cat

How to replace the n-th line in a file with a new line in Unix?

Step 1: remove the n-th line

$>sed -i'' '10 d' file.txt # d stands for delete

Step 2: insert a new line at n-th line position

$>sed -i'' '10 i This is the new line' file.txt # i stands for insert

How to connect to Oracle database from within shell script?

$>res=`sqlplus -s username/password@database_name <<EOF


SET HEAD OFF;
select count(*) from dual;
EXIT;
EOF`
$> echo $res
1

You might also like