Linux Interview Questions
Linux Interview Questions
1) Which command in Linux is used to print information about the system and its
environment?
->$ uname
2)Which command in Linux is used to get more info about the system?
->$ uname -a
4)If ifconfig is not getting recognized by the system then which package we have
to install,again what will be the command?
->$ sudo yum install net-tools
20)Use of mandb
-> It is used to create or update the manual page database on the system.
21)The command in Linux is used to locate the executable file that will be
executed when a given command is run in the shell.
Shows the full path of the command.
->$which ls
22)The command that shows the directories that your shell will search through
when you entered a command
-> echo $PATH
[email protected] $
Adhiraj $ echo ‘ Hi ← PS1 (Adhiraj)
Sakshi @ hello ← PS2 (Sakshi @)
Sakshi @ hi’
31)If you want to create a group with a specific group ID then which command
have to be used?Also give example
->
$sudo groupadd -g group_id group_name
$sudo groupadd -g 1009 demo
33)If we want to rename the group name,then how to do it?Change the name of
demo group which you have created earlier and rename it to test
->
$sudo groupmod -n new_name oldername
$sudo groupmod -n test demo
34)How to add a user in a group?Add a user name adhiraj to your test group
->
$sudo usermod - aG groupname username
$sudo usermod -aG test adhiraj
35)How to remove a user from group?Remove user adhiraj from test group
->$sudo gpasswd --delete username groupname
$sudo gpasswd --delete adhiraj test
37)Which command is used to list the contents in the current working directory?
->$ls
39)Which command is used to list the data according to time but in reverse order?
->$ls -ltr
40)How can we see the hidden files present in the directory?
Create a hidden file first and then display it
->
Create hidden file
$touch .hidden.txt
42)How to sort as well as list down all the contents in the current working
directory?
->$ls -s
43)How to list down the files of particular extension?List down all the files of txt
format
->$ls *.txt
49)Create a new file name new.txt using cat command and insert a data into it
->$cat > new.txt
An apple a day keeps the doctor away.
57)How to sort numerical data on the basis of their true value in maths?
->$sort -n filename
60)Sort the data in data.txt according to the second column,but on the basis of
mathematical value.
->$sort -t "," -nk2 data.txt
61)Sort the data in data.txt according to the second column,but on the basis of
mathematical value but in reverse order.
->$sort -t "," -nrk2 data.txt
*** Important
How to switch to command mode:
From Insert Mode: If you are in insert mode (where you can type and edit text),
press the Esc key. This will take you back to command mode.
From Visual Mode: If you are in visual mode (used for selecting text), press the Esc
key to exit visual mode and return to command mode.
From Replace Mode: If you are in replace mode, press the Esc key to exit replace
mode and return to command mode.
Once you are in command mode, you can use various navigation and editing
commands to manipulate text or save the file, as mentioned in the previous
response.
65)In vi mode, which symbol along with the colon is used to save the data?
-> :w
66)In vi mode which symbol along with colon is used to quit without saving the
data?
-> :q!
67)In vi mode which symbol along with colon is used to save the data and quit
from the file?
-> :wq
68)In vi mode which symbol along with colon is used to quit without saving the
data?
-> :q!
71)In vi mode ,if we want the numbering for each line for an example
hello ---> 1 hello
hii 2 hii
3
how are you 4 how are you
74)For example, let's say you have a file named example.txt with the following
contents:
This is an example text file
with some random words in it.
If you want to search for the word "example" in a case-insensitive manner, then
which command can be used?
->:set ic
/EXAMPLE
Suppose you have the text: "apple orange apple banana apple" and you want to
replace "apple" with "fruit."
- Using `%s/apple/fruit`, you'll get: "fruit orange apple banana apple" (only the
first "apple" is replaced).
- Using `%s/apple/fruit/g`, you'll get: "fruit orange fruit banana fruit" (all
occurrences of "apple" are replaced).
The "g" modifier makes the replacement operation global, affecting all instances
of the search term in the text.
79)Write a command to replace a word madhu with lalita (Replace all occurrences
within 20 and 30th line)
->20,30s/madhu/lalita
80)Command mode (Movement of cursor around the file) "h" is used for
->Move one char left side
81)Command mode (Movement of cursor around the file) "j" is used for
->Move one line down
82)Command mode (Movement of cursor around the file) "k" is used for
->Move one line up
83)Command mode (Movement of cursor around the file) "l" is used for
->Move one char right side
84)Command mode (Movement of cursor around the file) "w" is used for
->Move one word in forward direction
85)Command mode (Movement of cursor around the file) "b" is used for
->Move one word in backward direction
86)Command mode (Movement of cursor around the file) "$" is used for
->Move to the last char of the lineCommand mode
87)Command mode (Movement of cursor around the file) "^" is used for
->Move to the begining char of the current line
88)Command mode (Movement of cursor around the file) "0" is used for
->Move to the very begining of the current line
89)Command mode (Movement of cursor around the file) "G" is used for
->Move to the last line of the file
90)Command mode (Movement of cursor around the file) "gg" is used for
->Move to the first line of the file
106)Write a command to split a file on the basic of no of lines.Split the file try.txt
and each file should contain 4 lines
->$split -no_of_line filename
$split -4 try.txt
107)Write a command to split a file on the basic of bytes.Split the file try.txt and
each file should contain 426723 byte
->split -b byte_value filename
split -b 426723 try.txt
111) Write a command to convert all the vowels which are in small letters to
capital letters in the given sentence and print that newly translated sentence.
"Man is Mortal"
->$echo "Man is Mortal" |tr 'aeiou' 'AEIOU'
112)Write a command to convert 'abcd' '!@*%' in the given sentence.
"I love apples,dogs,cat and banana".
->$echo "I love apples,dog,cat and banana" |tr 'abcd' '!@*%'
114)Write a command to delete all the vowels from fruits.txt file using .
->$cat fruits.txt | tr -d 'aeiou'
120)How to get the last 10 lines of a file.Display last 10 lines of file try.txt
->tail try.txt
121)How to get the last 3 lines of a file.Display last 3 lines of file try.txt
->tail -n 3 try.txt
122)How to get first 10 lines of a file.Display the first 10 lines of file try.txt
->head try.txt
123)How to get the first 5 lines of file.Display the first 5 lines of file try.txt
->head -n 5 try.txt
1000 lines
Print 501-510
-> head -n 510 | tail -n 10 f1.txt
125)Sort the file try.txt according to the occurrence (count) of each word in the
file and then display the word having the highest count and comes last after
sorting .
->cat try.txt | tr -s ' ' '/n' | sort |uniq -c |sort -n |tail -n 1
126)Sort the file try.txt according to the occurrence (count) of each word in the
file and then display the first word having the highest count and comes first after
sorting .
->cat try.txt | tr -s ' ' '/n' | sort |uniq -c |sort -n |head -n 1
127)Which command is used in linux to view the content of text file one page at a
time.Use that command to view the file f5.txt
->$more f5.txt
128)Write a command to search a word (pavbhaji)in a text file named try.txt using
less command.
->$less try.txt
/pavbhaji
130)Suppose we have f2.txt having 10000 records and we want from 500 to 510
then
->$head -n 510 f2.txt | tail -n 11
133)If we want to see numbers of lines only ,then write a command for it.
->$wc -l try.txt
134)If we want to see numbers of word count only ,then write a command for it.
->$wc -w try.txt
135)If we want to see numbers of no of characters only ,then write a command for
it.
->$wc -c try.txt
138)Write a command to extract specific fields from emp.txt having data and
write that extracted data into another txt file.
Write a command to get the output as
FPT1,
MPT2,
HPT3,
and save it into another file named as f1.txt
->$cat emp.txt | cut -c 1-5 > f1.txt
139)Write a command to extract 2-5 fields from emp.txt having data and write
that extracted data into another txt file named as f1.txt
->$cat emp.txt |cut -c 2-5 >f1.txt
140)Write a command to extract 3-10 fields from emp.txt having data and write
that extracted data into another txt file named as f1.txt
->$cat emp.txt |cut -c 3-10 >f1.txt
141)Write a command to display the content of files f1.txt f2.txt f3.txt vertically
using paste command
->$paste f1.txt f2.txt f3.txt
142)Write a command to display following output using cut command for emp.txt
file
Output:-FPT1
MPT2
HPT3
->cut -d "," -f2 emp.txt
143)Write a command to display the following output using cut command for
emp.txt file to display third column.
->$cut -d "," -f3 emp.txt
144)Write a command to redirect the output of date command save the output in
m2.txt
->$date > m2.txt or date 1>m.txt
Step2:= Press i
Step3:=Write a code
#!/bin/bash
echo "a"
sleep 1
echo "b"
sleep 1
echo "c"
sleep 1
echo "d"
sleep 1
echo "e"
sleep 1
echo "d"
sleep 1
echo "e"
sleep 1
echo "f"
sleep 1
echo "g"
sleep 1
echo "h"
sleep 1
echo "i"
sleep 1
echo "j"
Step4:=Press :wq
Then hit enter
Step5:=./sleep.sh
BASIC=70000
DA=`echo ${BASIC} \*.1 | bc`
TA=`echo ${BASIC} \*.1 | bc`
HRA=`echo ${BASIC} \*.1 | bc`
GROSS=`echo ${BASIC} + ${DA} + ${TA} + ${HRA} | bc`
echo The Gross Salary is ${GROSS}
175)Which command is used to list the cron jobs for the current user,which is a
time-based job scheduler in Unix-like operating systems, and each
user can have their own set of scheduled tasks or jobs.
->$crontab -l
177)Write an example of how to use the crontab command to create and edit cron
jobs.
->Step1:-To edit the cron jobs for the current user, use the following command:
$crontab -e
Step3:-To view the updated list of cron jobs, you can use the crontab -l command:
$crontab -l