UNIX INTERVIEW QUESTIONS AND ANSWERS – 4

UNIX-INTERVIEW-QUESTIONS-AND-ANSWERS-4


31) What would be a simple way to continuously monitor the log file for a service that is running?

The simplest and common way to do this would be by using the command:

tail -F $LOGFILE

where $LOGFILE is an environment variable corresponding to the path to the log file to be monitored.

By default, the Linux tail command prints the last 10 lines of a given file to standard output. The -F option causes additional file content to be displayed in real time as the file continues to grow. This yields a simple mechanism for monitoring services via their log files in close to real time.

Two other specific command line options of interest in this context are:

The -s option causes tail to sleep for a specified number of seconds between updates (e.g., ‘tail -F -s 10’ will update the displayed file contents roughly every 10 seconds rather than in close to real time as the file is updated).

The -n option can be used to specify a number of lines other than 10 to initially display (e.g., ‘tail -n 20 -F’ will first display the last 20 lines of the file and will then continue updating the output in realtime).


32) How do you insert a new line or text after every n lines in Unix?

Let us consider a file. The requirement is to insert a line with text “UNIX” after every 2 lines. Below awk script can insert string “UNIX” in a file for each 2 lines.

$ awk '1;!(NR%2){print "UNIX";}' emp_data.txt

OR

$ sed 'N;s/.*/&\nUNIX/' emp_data.txt

awk-command-insert-text-any-lines

Read also: Unix interview questions and answers part – 1


33) How to remove the Control-M character from a file in Unix?

$ dos2unix filename

OR

$ sed 's/^M//g' filename


34) How to print tomorrow’s date in Unix?

$ date -d "1 day"

date-command-in-unix-tomorrow-date

Read also: Unix interview questions and answers part – 2


35) How will you find which operating system your system is running on in Unix?


By using command “uname -a” in Unix

$ uname -a

uname-command-in-unix


36) 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 “ps”, for killing that process find PID and use ‘kill -9 PID’ command.


37) How do you copy file from one host to other?

We have many options to transfer files from one host to another host, you can say by using “scp” command. You can also use rsync command to answer this or even sftp would be okay.

Read also: Unix interview questions and answers part – 3


38) 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.

df-command-in-unix-to-find-free-space-unix-interview-question


39) Write a command to display the first 5 characters from each line of a file?

$ cut -c -5 filename

cut-command-in-unix-interview-questions


40) What is the significance of ‘$?’ ?

$? gives the exit status of the last command that was executed.

return-code-echo-unix-interview-questions
0 (Zero) – means command returns successful.
Non Zero – means command returns unsuccessful.

Comments

comments

Leave a Reply