We have moved to www.dataGenX.net, Keep Learning with us.

Tuesday, February 26, 2013

Interview Questions : Unix/Linux : Part-6




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

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

12.   How to display zero byte size files?
ls -l | grep '^-' | awk '/^-/ {if ($5 !=0 ) print $9 }'

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

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

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

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

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

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

19.   How to replace the character '/' with ',' in a file?
sed 's/\//,/' < filename
sed 's|/|,|' < filename

20.   Write a command to find the number of files in a directory.
ls -l|grep '^-'|wc -l

           
             For more questions like this please visit ---->  Interview Questions : Unix/Linux