UNIX INTERVIEW QUESTIONS AND ANSWERS – 2

UNIX-INTERVIEW-QUESTIONS-AND-ANSWERS-2

11.Write a command to list the files in ‘/usr’ directory that start with ‘bo’ and then display the number of lines in each file?

$ wc -l /usr/bo*

Or

$ find /usr -name 'bo*' -type f -exec wc -l {} \;

12.How to remove blank lines in a file?

$ grep -v ‘^$’ filename > new_filename

grep-command-unix-to-remove-blank-lines

Read also: Unix interview questions and answers part – 1

13.Write command to remove the prefix of the string ending with ‘/’?

The basename command deletes any prefix ending in ‘/’.

$ basename /usr/local/bin/file

This will display only file name.

basename-command-to-remove-slashes

14.How to display zero byte size files?

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

Or

$ find $dir -size 0

find-command-unix-to-print-zero-byte-file

15.How to replace the second occurrence of the word “unix” with “linux” in a file?

$ sed 's/unix/linux/2' filename

sed-command-unix-replace-second-occurence-word

16.How to remove all the occurrences of the word “unix” except the first one in a line with in the entire file?

$ sed 's/unix//2g' filename

sed-command-unix-remove-all-occurences-except-first-one

Read also: Unix interview questions and answers part – 3

17.How to replace the word “unix” with “linux” from 3rd line to last line in a file?

$ sed '3,$ s/unix/linux/' < filename

sed-command-unix-replace-all-occurences-from-specific-line

18.How to list the files that are accessed 3 days ago in the current directory?

$ find -atime 3 -type f

19.How to list the files that were modified 3 days ago in the current directory?

$ find -mtime 3 -type f

20.How to list the files whose status is changed 3 days ago in the current directory?

$ find -ctime 3 -type f

Read also: Unix Interview Questions-1

Comments

comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: