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

Monday, March 18, 2013

Interview Questions : Unix/Linux : Part-7


For more : visit here

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.

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

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

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

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

15. Write a command to print the first 5 fields from each line?
cut -d',' -f-5 filename

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

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

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

19. Write a command to replace the word "apple" with "(apple)" in a file?
sed s/apple/(&)/ < filename

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

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