Top 50 Unix Interview Questions & Answers



Commands

  1. To delete all special character from file f1.txt.

sed 's/[!@#\$%^&*()]//g'  f1.txt

  1. Remove duplicate lines from file x1.

Uniq x1

  1. To print first 10 lines from file f1.

head -10 f1

  1. To display line 1 to 5 from x1.

cat -n x1 | hello -5

  1. To display line 1 to 5 from x1 and sort a line of file and also remove repeated lines.

Head -5 x1 | sort -u

  1. Replace all occurrences of 'SYBCA' with 'TYBCA' in 5th line of file f1.txt.

sed ‘5 s/SYBCA/TYBCA/’ f1.txt

  1. Count the occurrences of 'TYBCA' in studentlist file.

grep -n ‘TYBCA’ studenlist | wc -w

  1. To run a utility x1 at 11:00 AM.

at 11:00 AM x1

  1. Display Number of process. [display process of user]

ps

  1. Display lines starting from 10th line to the end of file x1.

Tail +10 f1

  1. Display all files in current directory where the 1st character is  numeric and last character is not alphabet.

    ls | grep -e "^[0-9$]"

  1. Display content of last modified file.

ls -t | head -1

  1. Display content of top 3 largest file in a working directory.

Ls -S | head -3

  1. Display all files in current directory whose 1st character is  is not digit.

ls | grep -e “^[^0-9]”

  1.  To display inode of all files of current directory.

ls -i

  1. Count the number of character in 1st 3 lines of file f1.

Head -3 f1 | wc -m


  1. Remove a file forcibly which do not have write permission.

      rm -f filename

  1. To concatenate  two files f1 and f2 including some text to be inserted from keyboard after content of f1.

Cat f1 f2

Cat>>f1 | cat f1>f2

  1. Remove directory tree dir/dir2/dir3 using single command.

Rm -rf dir

  1. assign a value of 10th position parameter to variable x.

x=10

echo $x

  1. Display last word of each line from a file x1.

Awk ‘{print $NF}’ caroline

  1. Display number of words in 40 to 60 of file f1.txt.


  1. Display all lines that stats with 'let' from a file x1.  And the letter l, e or t may be in any case.

Grep -i “^let” x1

  1. Replace 'kernel' with 'kernel architecture' using remember pattern of sed utility.

sed ‘s/kernel/kernel architecture/g’ filename

  1. List the user from etc/passwd in alphabetically sorted order.


  1. Count the frequency of users who are logged in from more than one terminal.

Who |wc -l

  1. Display starting in upper case of the file in f1.txt.

Grep “^[A-Z]” f1.txt


GREP /SED

  1. Display all lines between 20 and 30 of file x1.

Tail +25 x1 | tail -5

  1. To display first 6 lines from file x1.

Head -6 x1

  1. Display lines beginning either with alphabet or digit from file x1.

grep "^[aeiou]*[0-9]" x1


  1. Display lines that do not contain "unix".

Grep “^[^unix]” filename

  1. Display lines which are not starting with 2 at beginning.

grep ‘^[^2]’ filename

  1. Write a command to display all file name containing only digits in filename.

ls | grep -e "^[0-9$]"


  1. To display those lines in between 25 and 50 having pattern 'unix' in it.


    sed -n 25,50p caroline | grep -n "unix"


  1. Display lines before a line that contain pattern 'xyz' in file f1.txt.

Grep ‘^xyz’ f1.txt

  1. Find out the number of the character '?' occur in file f1.txt.

Grep -c ‘?’ f1.txt

  1. To count the number of words in line 40 through 60 of file f1.txt.

sed -n 40,60p caroline | wc -w


  1. Display lines having exactly 50 characters in file f1.

Sed -n 50p f1

  1. count the number of blank lines in file f1.

Grep -c “ “ f1

  1. count the number of blank lines in between 10 and 20 of x1.

grep -c "^$" caroline


  1. display lines having at least one * character in file x1.

Grep ‘.’ X1

  1. Display the directory listing.

ls -d */


  1. Display files having read and write permission for the group.

Chmod 624 filename

  1. Write a command to substitute doshi with desai.

Sed -e ‘s/doshi/desai/g’ filname

  1. To display all lines that contain pattern bc* in a line.

grep “bc*” filname

  1. To replace tybca with TYBCA in input file in.sh and write those lines to output file out.sh.

    Sed -n ‘s/tybca/TYBCA/p’ in.sh>out.sh

  1. To display occurrences of 'BCA' in file x1.

Grep “BCA” x1

  1. To replace all occurrences of 'she' with 'he' and 'hi' with 'hello'.

  sed -i ‘s/she/he/g’ filename |   sed -i ‘s/hi/hello/g ’ filename 


  1. To print lines with line nos. which contain "marketing".

      grep -n ‘marketing’ file name

  1. Display lines which starts with 'The'.

       Grep ‘^the’ filename

  1. Delete all vowels from file x1. 

sed  's/[aeiou]//g' caroline



AWK

  1. Write an AWK script to display number of words and characters in each line. 

Awk BEGIN{ print “record\t characters \t words”}

{

Len=length($0)

total=len

Print(NR”\t”,len,”:\t”,NF,$0)

Words=NF

}

END{

Print(“\n total”)

Print(“charcter”\t total len)

Print(“lines :\t” NR)

}


  1. Write an AWK script that display last word of each line. 

awk 'NF>1{print $NF}' caroline


  1. Print odd number of words in each line. 

Awk ‘NR%2!=0’ filename

  1. Print all user/login names available in etc/passwd.  

cat /etc/passwd | awk -F ":" '{print $1}' | head


  1. Print the fields 2,3,4, and 6 from test.txt file which contain the pattern 'comp'.

awk -F"|" '/comp/ {print $2,$3,$4,$6}' test.txt



  1. print all lines containing string 'for' in test.txt file. 

  awk '/for/ {print}' caroline

 

  1. count the occurrences of pattern 'operating system' in file f1.

awk  '/for/ {print NF}' caroline | wc -w


  1. Write an AWK script to reverse individual word of a line from file. e.g : INPUT : hello word OUTPUT : ollehdrow

Awk ‘{for(i=NF;i>1;i--) printf “%s” ,$i;print “” }’ filename

  1. Create a student-dat file which contain rollno and marks of 5 tests. Write an awk script to calculate total and average marks for each student. 


  1. AWK COMMAND:

  1. Write an awk script to display file contents in reverse. 

(i.e last line should be displayed first,…..,first line should be displayed last.)

awk '{a[i++]=$0}END{for(j=i-1;j>=0;j--)print a[j];}' caroline


2.Print lines on 18 to 30 from file f1.txt  

Awk  ‘NR > 18 && NR <=30 ‘ f1.txt

  1. Count the total no. of lines in a file. 

   Awk ‘{print}’ file | wc -l

  1. Write a script to print 1to 10 numbers. 


 awk 'BEGIN { for(i=1;i<=10;i++) print i}'


  1. write a command to print those lines where field2 is computer field3 > 15000 from sales file. 

awk '{ if($4 > 15000) print $4 }' emp


  1. Switch the first two fields in each line of a text and put the result in a new file.

Awk  ‘$1 < 20’ filename 


  1. To only print lines where in the first field had a numeric value of less than 20. 

awk '{ if (length($0) < 20) print  } END { print  }' emp


  1. Write an awk script that will count total no. of students in each department using file stud.txt having fields (rno,name,dept,marks). Display total marks department wise. 

awk '{ if($3 == "com")  print  }' stud


Write a shell scripts:

  1. Write a shell script to test file is an executable or not. [OR exists or not/ readable(Also display total number of lines, words, characters of file.)] 

  echo “enter the file name”

read a

if [ -x $a ]

then

echo “executable”

wc $a

else

echo “not executable”

wc $a

fi


  1. To print 1 to 20 numbers in reverse order and also calculate sum of odd numbers. 


for i in {20..0..-1}

do


if (( $( echo $i % 2 ) == 1 ))

then




sum= expr $sum + $i

echo $sum


fi


done


echo "sum is $sum"



  1. Write a shell script to test the length of string is zero or not. 

echo “enter the string”

read a

expr length $a


  1. Write a shell script to list, copy and rename file using select case. 


echo -e "\n rename:"

echo -e "\n enter the choice :"

read c

case $c in

1)

        ls -l $f1

;;

2)

        echo "enter name of copy file"

        read f2

        cp $f1 $f2

;;

3)

        echo "enter new name"

        read nf

        mv $f1 $nf

;;

*)

        echo "select valid option "

;;

esac


  1. Write a script to reverse string. the string should be passed as arguments. 

echo "enter string"

read name

echo $name | rev



  1. Write a menu driven script to convert the content of file in to uppercase or lowercase based on user choice. 

echo "enter string "

read name

echo "convert string into uppercase press 1"

echo "convert string into lowercase press 2"

read a

case $a in

1)

echo $name | tr "[a-z]" "[A-Z]"

;;

2)

echo $name | tr "[A-Z]" "[a-z]"

;;

esac



  1. Write a shell script to check whether 2 files are same or not. if the files are same the 2nd file should be deleted from directory. 


echo “enter the file 1”

read a

echo “enter the file 2”

read b

if cmp -s “$a” “$b”

then

echo “match”

rm $b

else

echo “not match”

fi


  1. Write a shell script to display the calendar for current month with current date replaced by * or ** depending on whether the date has one digit or two. 


if (( $(date "+%d") <= 9 ))

then

cal|sed -r "s/\b$(date "+%d")\b/*/"

else

cal|sed -r "s/\b$(date "+%d")\b/**/"

fi



  1. Write a shell script to perform mathematical operation using select case. 


echo "enter the two number"

read a b

echo "1:addition/n2:subtraction/n3:multipication/n4:divison"

read c

case "$c" in

1)z=`expr $a + $b`;;

2)z=`expr $a - $b`;;

3)z=`expr $a /* $b`;;

4)z=`expr $a / $b`;;

esac

echo $z



  1. Write a shell script to check whether  the entered input is palindrome or not. 18B084-neha

echo -n "Enter number : "

read n

sd=0

 rev=""

 on=$n

 while [ $n -gt 0 ]

do

    sd=$(( $n % 10 )) # get Remainder

    n=$(( $n / 10 ))  # get next digit

    rev=$( echo ${rev}${sd} )

done

 if [ $on -eq $rev ];

then

  echo "Number is palindrome"

else

  echo "Number is NOT palindrome"

fi

  1. Write a shell script that display 5th line of all files of current directory that starts with vowels. 

Echo “enter file name”

Read file

grep "^[aeiou]" file | sed -n '5p'


  1. Write a script which count nos. of vowels in the entered string. 

echo “enter the string”

read str

len=$(expr length $str)

count=0

while[$len -gt 0]

do

ch=$(echo $str | cut -c $len)

case $ch in

[aeiouAEIOU])

Count=$(($count + 1))

Echo $ch

;;

Esac

Len=$(($len -1))

Done

echo $count


  1. write a scripts to validate the name of person accepted through keyboard so that it doesn't exceed 10 character of length. 


echo “enter the name”

read a

count= expr length “$a”;

echo $count

if [ $count -le 10 ]

then

echo $count

echo “valid”

else

echo “invalid”

fi


  1. write a shell script to accept 2 decimal numbers from keyboard and display  their sum in hexadecimal form. [/octal] 


  1. Write a shell script  that will read 2 nos from command line and display even nos between these two. 


echo "enter two numbers"

read a b


if(( $a % 2  == 0 ))

then

echo "even number $a"

elif(( $b % 2 == 0))

then

echo ":even number $b "

elif(( $a % 2 ==0 && $b % 2 == 0 ))

then

echo "both are even"

else

echo "both are not even"

fi



  1. Write a shell script  that will read a file name and pattern from the command line and check whether that pattern exist in file and if exist then count total no. of words in the lines where pattern found. 


echo “enter the file name”

read a

echo “enter the pattern”

read b

grep -n $b $a | wc -w




  1. Perform following file studlst': 

  • WAC to locate the "Unix" ignoring case

  • WAC to display occurance of "tybca"

  • WAC to display line which start with 'The'

  • WAC to display line which end with 'India'

  • WAC to print 1st six lines of file.


echo "print line where locate unix"

grep -i -n "unix" caroline

echo "print matched lines "

grep -o "tybca" caroline

echo "print the line start with the"

grep "^The" caroline

echo "print the line end with india"

grep "india$" caroline

echo "first 6 lines"


head -6 caroline


Post a Comment

If you have any doubts, Please let me know
Thanks!

Previous Post Next Post