Top Unix Interview Questions
Top Unix Interview Questions
Top Unix Interview Questions
8. How to replace the n-th line in a file with a new line in Unix?
echo $?
11. How will you find which operating system your system is running on in UNIX?
uname -a
The d option here deletes the lines from 21 to the end of the file
15. Write a command to print the last line of a file?
The tail command can be used to display the last lines from a file.
tail -1 filename
ls -a | grep '^\.'
filename
10. How do you display from the 5th character to the end of the line from a file?
cut -c 5- filename
2. Write a command to search for the file 'map' in the current directory?
4. Write a command to remove the first number on all lines that start with "@"?
5. How to print the file names in a directory that has the word "term"?
grep -l term *
The '-l' option make the grep command to print only the filename without printing the content of the
file. As soon as the grep command finds the pattern in a file, it prints the pattern and stops searching
other lines in the file.
6. How to run awk command specified in a file?
awk -f filename
7. How do you display the calendar for the month march in the year 1985?
The cal command can be used to display the current month calendar. You can pass the month and
year as arguments to display the required year, month combination calendar.
cal 03 1985
This will display the calendar for the March month and year 1985.
8. Write a command to find the total number of lines in a file?
wc -l filename
Vmstat: reports on virtual memory statistics for processes, disk, tape and CPU activity.
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"?
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?
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?
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
2. Write a command to display all the files recursively with path under current directory?
4. Write a command to display the third and fifth character from each line of a file?
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?
6. How to replace the word "Gun" with "Pen" in the first 100 lines of a file?
7. Write a Unix command to display the lines in a file that do not contain the word "RAM"?
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
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.
basename /usr/local/bin/file
3. How to replace the second occurrence of the word "bat" with "ball" in a file?
4. How to remove all the occurrences of the word "jhon" except the first one in a line with in the entire
file?
5. How to replace the word "lite" with "light" from 100th line to last line in a file?
6. How to list the files that are accessed 5 days ago in the current directory?
7. How to list the files that were modified 5 days ago in the current directory?
8. How to list the files whose status is changed 5 days ago in the current directory?
ls -l|grep '^-'|wc -l
2. Write a command to display the first 10 characters from each line of a file?
3. The fields in each line are delimited by comma. Write a command to display third field from each
line of a file?
4. Write a command to print the fields from 10 to 20 from each line of a file?
6. By default the cut command displays the entire line if there is no delimiter in it. Which cut option is
used to suppress these kind of lines?
The -s option is used to suppress the lines that do not contain the delimiter.
7. Write a command to replace the word "bad" with "good" in file?
8. Write a command to replace the word "bad" with "good" globally in a file?
10. Write a command to switch the two consecutive words "apple" and "mango" in a file?
11. Write a command to display the characters from 10 to 20 from each line of a file?
grep july *
This will print all the lines in all files that contain the word july along with the file name. If any of the
files contain words like "JULY" or "July", the above command would not print those lines.
2. Write a command to print the lines that has the word "july" in all the files in a directory and also
suppress the file name in the output.
grep -h july *
3. Write a command to print the lines that has the word "july" while ignoring the case.
grep -i july *
The option i make the grep command to treat the pattern as case insensitive.
4. When you use a single file as input to the grep command to search for a pattern, it won't print the
filename in the output. Now write a grep command to print the file name in the output without using
the '-H' option.
The /dev/null or null device is special file that discards the data written to it. So, the /dev/null is
always an empty file.
Another way to print the file name is using the '-H' option. The grep command for this is
5. Write a command to print the file names in a directory that does not contain the word "july"?
grep -L july *
The '-L' option makes the grep command to print the file names that do not contain the specified
pattern.
6. Write a command to print the line numbers along with the line that has the word "july"?
The '-n' option is used to print the line numbers in a file. The line numbers start from 1
7. Write a command to print the lines that starts with the word "start"?
The '^' symbol specifies the grep command to search for the pattern at the start of the line.
8. In the text file, some lines are delimited by colon and some are delimited by space. Write a
command to print the third field of each line.
awk '{ if( $0 ~ /:/ ) { FS=":"; } else { FS =" "; } print $3 }' filename
10. Write a command to print the second and third line of a file without using NR.
11. How to create an alias for the complex command and remove the alias?
The alias utility is used to create the alias for a command. The below command creates alias for ps
-aef command.
unalias pg
date '+%Y-%m-%d'
The cut command is used to used to display selected columns or fields from each line of a file. Cut
command works in two modes:
Delimited selection: The fields in the line are delimited by a single character like
blank,comma etc.
Range selection: Each field starts with certain fixed offset defined as range.
1. Write a command to display the third and fourth character from each line of a file?
cut -c 3,4 filename
2. Write a command to display the characters from 10 to 20 from each line of a file?
cut -c 10-20 filename
3. Write a command to display the first 10 characters from each line of a file?
cut -c -10 filename
4. Write a comamnd to display from the 10th character to the end of the line?
cut -c 10- filename
5. 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
6. Write a command to print the fields from 10 to 20 from each line of a file?
cut -d',' -f10-20 filename
7. Write a command to print the first 5 fields from each line?
cut -d',' -f-5 filename
8. Write a command to print the fields from 10th to the end of the line?
cut -d',' -f10- filename
9. 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.
10. Write a cut command to extract the username from 'who am i' comamnd?
who am i | cut -f1 -d' '
6. Write a command to print the file names in a directory that has the word "july"?
grep -l july *
The '-l' option make the grep command to print only the filename without printing the content of the
file. As soon as the grep command finds the pattern in a file, it prints the pattern and stops searching
other lines in the file.
7. Write a command to print the file names in a directory that does not contain the word "july"?
grep -L july *
The '-L' option makes the grep command to print the filenames that do not contain the specified
pattern.
8. Write a command to print the line numbers along with the line that has the word "july"?
grep -n july filename
The '-n' option is used to print the line numbers in a file. The line numbers start from 1
9. Write a command to print the lines that starts with the word "start"?
grep '^start' filename
The '^' symbol specifies the grep command to search for the pattern at the start of the line.
10. 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.
11. 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.