Interview Questions: - (Unix)
Unix : -
Q) Write a single command to print your name one time?
Ans –
$ echo “Baishnab Charan Patra”
Q) Write a script to print your name 7 times?
Ans –
#! /bin/sh
a=0
while [ $a -lt 7 ]
do
echo “Baishanb Charan Patra”
a=`expr $a + 1`
done
Q) Write a script if a file is file type or not?
Ans –
#! /bin/sh
echo “Enter a file name : “
read fn
if [ -f $fn ]
then
echo “$fn is file type.”
else
echo “$fn is not file type.”
Fi
Q) What is the difference between $* and $@?
Ans –
$* -> Print all command line arguments passsed in a script.
$@ -> Print all command line arguments within double quotes individually passes in a script.
Q) What is $?, $# and $0?
Ans –
$? -> the exit status of last command.
$# -> total number of command line arguments
$0 -> the current script file name
Q) Top 5 processes using high CPU utilisation?
Ans –
$ top –c –b | head -5
Where –b = batch mode
Here you will see processes with command sorted as per their CPU usage.
or
$ top –b | head -5
Here you will see processes with commands along with their paths as per CPU usage.
-top command refresh in every 5 seconds.
or
$ ps -eo pid,ppid,%mem,%cpu,cmd --sort=-%cpu | head -5
where
ps : To find process
-e : Select all processes.
-o : To customize a output format.
–sort=-%cpu : Sort the ouput based on CPU usage.
PID : Unique ID of the process.
PPID : Unique ID of the parent process.
%MEM : The percentage of RAM used by the process.
%CPU : The percentage of CPU used by the process.
Command : Name of the process.
Q) Top 5 processes using high memory utilisation?
Ans –
$ ps -eo pid,ppid,%mem,%cpu,cmd --sort=-%mem | head -5
ps : To find process
-e : Select all processes.
-o : To customize a output format.
–sort=-%mem : Sort the ouput based on memory usage.
PID : Unique ID of the process.
PPID : Unique ID of the parent process.
%MEM : The percentage of RAM used by the process.
%CPU : The percentage of CPU used by the process.
Command : Name of the process.
Q) Command to print all directories inside a directory?
Ans –
$ ls –d
Or (for long listing)
$ ls –ld
Q) Command to print the sum of file sizes in a directory?
Ans –
$ du –sch
Where s and c used to summarize and print a total size of all files recrusively.
h -> print in human readable format by default GB.
Or
$ du –scb
Where s and c used to summarize and print a total size of all files recrusively.
b -> prints in byte format.
Q) How to check if f1 and f2 are same file or not?
Ans –
$ diff f1 f2
If no output -> f1 and f2 are same.
If some output -> f1 and f2 are not same.
Q) How to check if last command/script we have ran, it went successful or not?
Ans –
$ echo $?
If output is zero -> command/script ran successfully.
If output is non zero -> command/script ran unsuccessfully.
Q) How to remove duplicates in the file?
Ans –
$ awk ‘!a[$0]++’ file_name.txt > dont_dup.txt
Q) How to find largest size length sentence from a file?
Ans –
$ awk '{print NF, $0}' file_name | sort -nr | head -1
No comments:
Post a Comment