0% found this document useful (0 votes)
62 views

Linux Interview Questions

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Linux Interview Questions

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Linux Questions Basic - Part 1

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

3)Which command is used to print the ip address?


->$ ifconfig

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

5)How to check the user-name in linux?


->$ whoami

6)How to check the machine os,which command is used?


->$ cat /etc/os-release

7)How to print the shell name?


->$ echo $SHELL or $echo $0

8)Which command in Linux is used to determine the execution time of a given


command or process? Also give one example.
->$ time command_name
Example:-time ls

9)Which command is used to list down all the available shells ?


->$ cat /etc/shells

10)Give names of any two shells?


->
/bin/sh
/bin/bash
/bin/ksh

11)If we want to shift to sh shell,which command we have to use?


->$ sh

12)Which command is used to check the current shell ?


->echo $0

13)How to check the host name?


->$ hostname OR uname

14)Which command is used to check user and userinfo?


->$ cat /etc/passwd (uid , gid ,etc)

15)What is the id of the root user?


-> 0

16)In which range system users have their id?


-> 1-999

17)What is the id of ec2-user?


-> 1000

18)In which range normal users have their id?


-> Above 1000 (1001-1999)

The range in which group id exist?


Above 2000

19)Which command is used to print the present working directory?


->$ pwd

20)Which command is used to print the string?


->$ echo “string_name”

21)How to check the reboot time of the system?


->
$who -b ( shows reboot time)
$uptime -s ( show reboot time and date)
$last reboot ( show the history of the reboot)

22)How to change the date and time?


->$ sudo date -s 'Fri Apr 14 09:30:00 AM UTC 2002'

19)Which command is used to display the manual page of a command?


->$ man command_name
Example: $ man ls

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

***This two commands are same : echo $USER or whoami***

23)How to change the name, primary prompt, secondary prompt?


->
$PS1='Adhiraj'
$PS2='Sakshi @'

[email protected] $
Adhiraj $ echo ‘ Hi ← PS1 (Adhiraj)
Sakshi @ hello ← PS2 (Sakshi @)
Sakshi @ hi’

24)How to create a user?Create one user


->
$sudo useradd username
Example: sudo useradd adhiraj

25)How to change the password of newly created user?Change the password of


adhiraj user
->
$sudo passwd username
Example: sudo passwd adhiraj

26)How to delete the user?


->$ sudo userdel username
Example: sudo userdel adhiraj

27)How to cleanly delete a user?


->$sudo userdel -r username
Example: sudo userdel -r adhiraj

28)How to create a hidden file?


->touch .file_name
29)In Linux, which file is the encrypted password file?
->sudo cat /etc/shadow

30)How to add a group?


->$sudo groupadd demo

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

32)How to change the group ID of existing group?Change the ID of demo group


which you have created earlier
->
$sudo groupmod -g new_ID groupname
$sudo groupmod -g 1111 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

36)How to delete a group?Delete group test


->$sudo groupdel test

37)Which command is used to list the contents in the current working directory?
->$ls

38)Which command is used to list the data according to time?


->$ls -lt

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

Display hidden file


$ls -a

41)How to list down the files created by a specific author?


->$ls --author

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

44)How to change the directory?Suppose you have to change it to folder1


->$cd folder1

45)How to move to the home directory?


->$cd ~

46)How to change the current working directory to the root directory?


->$cd /

47)How to move to the previously used directory?


->$cd ..

48)How to open a file?


->$cat filename

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.

50)Now append data in new.txt file using cat command.


->$cat>>new.txt
I love apples.
51)How to add line numbers to non blank lines.
->$cat -b filename

52)How to add line numbers to blank as well as to non blank lines.


->$cat -n filename

53)Which command is used to sort a file?


->$sort file_name

54)How we can sort files f1.txt ,f2.txt, f3.txt


->$sort f1.txt f2.txt f3.txt

55)How to sort a file content in reverse order?


->$sort -r filename

56)How to sort a file without caring about case sensitivity?


->$sort -f filename

57)How to sort numerical data on the basis of their true value in maths?
->$sort -n filename

58)Suppose you have file named as data.txt and having content


apple,1020,aurangabad,Maharashtra
mango,422,washim,Gujarat
potato,85,pune,Goa

Sort the data according to third column


->$sort -t "," -k3 data.txt

59)Sort the data in data.txt according to second column


->$sort -t "," -k2 data.txt

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

62)Sort the file and also print the no of occurence of elements.


->$sort filename | uniq -c
63)Sort the file and also show no duplicates and print the duplicate elements.
->$sort filename | uniq -cd

64)Sort the file and also show duplicate elements.


->$sort filename | uniq -d

*** 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!

69)In vi mode, how can we go to the 10th line?


-> :10

70)In vi mode, how can we go to the last line?


-> :$

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

which command can be used?


->:set nu

72)Now we want to remove the numbering which we set in earlier question,then


which command can be used?
->:set nonu

73)How to search in a file opened using vi mode?


->/search_word

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

75)How to turn off case-insensitive search.


->:set noic

76)How to replace an existing word with a new word in vi mode?


->%s/existing_word/new_word

77)How to replace a existing word with new word in vi mode(Replace all


occurrence in each line)
->%s/existing_word/new_word/g

78)What is the difference between %s/existing_word/new_word and


%s/existing_word/new_word/g?
->
1. `%s/existing_word/new_word`: This is a basic search and replace operation
that replaces the first occurrence of "existing_word" with "new_word" in a given
text or string. It's typically used with tools or functions that support basic text
replacement. If multiple instances of "existing_word" exist in the text, only the
first occurrence is replaced.
2. `%s/existing_word/new_word/g`: The addition of the "g" at the end of the
expression stands for "global." When used with search and replace functions or
regular expressions, this will replace all occurrences of "existing_word" with
"new_word" in the given text. It doesn't stop after the first occurrence; it
continues searching and replacing until it reaches the end of the text or string.

Here's a simple example to illustrate the difference:

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

91)Write a command to move 5th char left side


->5h

92)Write a command to move 6 line down


->6j

93)Write a command to move 3 line up


->3k

94)Write a command to move 4 char right side


->4l

95)Write a command to move 5 word in forward direction


->5w

96)Write a command to move 8 word in backward direction


->8b

97)Which char is used to copy


->y

98)Which char is used to paste


->p

99)Which char is used to delete


->d

100)How to search a word in command mode


->/word
101)If we want to search a word in command mode in forward direction then
which char we have to press?
->n

102)If we want to search a word in command mode in backward direction then


which char we have to press?
->N

103)List down all the insert modes commands


->i I a A o O s S

104)The command which is used to check whether there is any modification in


the file or not?
->$md5sum filename

105)Which command is used to split the file?


->$split filename

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

108)Write a command to split a file on the basis of no of chunks.Split the file


try.txt into 3 files.
->$split -n no_of_files filename
$split -n 3 try.txt

109)Which command is used to set default permissions for files or directories


that are created by the user?
->$umask

110)Which command is used to translate the characters?


->$ tr

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' '!@*%'

113)Write a command if we want to delete a specific character from the string


using tr command.
"Man is Mortal"
Delete all the vowels .
->$echo "Man is Mortal" |tr -d 'aeiou'

114)Write a command to delete all the vowels from fruits.txt file using .
->$cat fruits.txt | tr -d 'aeiou'

115)Write a command to squeeze all 'o'to single o


"Helloooo Brooooo"
->$echo "Helloooo Brooooo" |tr -s 'o' 'o'

116)Write a command to squeeze 'lo' 'lo'


"Hellllloooooooooo Brrroooooo"
->$echo "Hellllloooooooooo Brrroooooo" |tr -s 'lo' 'lo'

117)Write a command to change space -> tab


"Hello my name is sakshi"
->$echo "Hello my name is sakshi" | tr ' ' '\t'

118)Write a command to print each word in new line


"Hello my name is sakshi sawalikar"
->$echo "Hello my name is sakshi" | tr ' ' '\n'

119)How to find files in a directory using the whole path. Path=


/home/ec2-user/folder1 and filename that you have to search is f1.txt
->$find full_path -name 'filename'
$find /home/ec2-user/folder1 -name 'filename'

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

124)Sort the each word of a file try.txt


->cat try.txt | tr -s ' ' '/n' | sort

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

129)Suppose we have a txt file named as f1 having data


A B C D ------------M N O
And we want to print the data in the middle(E F G H) then write a command for
this task.
->$head -n 8 f1.txt|tail -n 4

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

131)Write a command to copy 5000 lines from f2.txt into f3.txt


->$head -n 5000 f2.txt > f3.txt
132)Write a command to see no of lines ,word count and characters of file try.txt
->$wc try.txt

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

136)Write a command to extract specific fields from emp.txt having data


FPT1,ADHI, SCIENCE
MPT2,SAKSHI,MATHS
HPT3,SHRUTI,BCOM

Write a command to get the output as


FPT1,
MPT2,
HPT3,
->$cut -c 1-5 emp.txt

137)Write a command to get the output as


FPT1
MPT2
HPT3
->$cut -c 1-4 emp.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

145)Write a command to redirect the output of a command save the error in


m2.txt
->$dates 2>m2.txt

146)Write a command to see the processes that are currently active.


->$ps

147)Print the shell name where processes are currently running.


->$echo $0

148)Write a command to print process id


->$echo $$

149)How to check in which terminal we are?


->$tty -> /dev/pts/

150)Which command is used in Linux to display a detailed list of currently


running processes in a full format.
->$ps -f

$ ps -e … who the demon process / all process


151)Which command in Linux is a real-time process monitoring tool that
provides a dynamic overview of the system's current resource usage, including
CPU, memory, and other system statistics.
->$top

152)What is the use of -e? Also give one example.


->If we want to print a word or line in new line than we can use -e
Example:- $echo -e "Hello\nWorld"

153)Write a program which will create a beep sound.


->$echo -e "Hello\a\a\a\aworld"

154)Write a command to change the colour(Make it Red) of word sakshi


->echo -e "\e[1;31m sakshi\e[0m "

155)Write a process to create and execute a script .


->Step1:=$vi sleep.sh

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

156)How to terminate the currently executing process.


->$ctrl+c

157)How to pause the currently executing process.


->$ctrl+z

158)How to start the paused process again.


->$fg

159)How to start the paused process again but in the background.


->$bg

160)Which command is used to monitor the contents of a file in real-time,


particularly log files, by displaying the last few lines of the file and
continuously updating the display as new lines are added to the file.
->$tail -f filename

161)Which command is used to list the available signal names or numbers on a


Unix-like system.
->$kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP

162)Which command is used to display detailed information about processes


running on a Unix-like system. It provides a full listing of processes in
a "long" format, showing information such as the process ID (PID), parent proce
(PPID), CPU and memory usage, start time, and command
associated with each process.
->ps -f

163)Alternate to(ctrl+z) stop the currently executing process.


->$kill -19 PID_NUMBER

164)Alternate to(fg and bg) to resume the currently executing process.


->$kill -18 PID_NUMBER

165)Alternate to(ctrl+c) terminate the currently executing process.


->$kill -9 PID_NUMBER

166)Which command used in Unix-like operating systems to run a command or


script that will continue running even after the user logs out or
terminates the session. It stands for "no hangup."Give one example
->Step 1:- vi sleep2.sh
Step2:-#!/bin/bash
>number.txt
for N in {1..1000}
do
echo ${N}>>numbers.txt
echo Hello$N
sleep 1
done
Step3:-chmod u+x sleep2.sh
Step4:-nohup ./sleep2.sh &

167)How to kill the process?Kill sleep2.sh


->pkill -f sleep2.sh

168)Which command is used to display RAM status.


->$free

169)Which command is used to display disk space usage (disk free)


->$df or $df -h

170)Which command is used in Unix-like operating systems to estimate file and


directory disk usage. Which provides information about the space
occupied by files and directories, helping you analyse disk usage on your system.
->$du -h (disk usage)

171)Declare a variable BASIC,DA,TA,HRA,GROSS assign BASIC as 7000 and find


the GROSS salary also display it on terminal.
->#!/bin/bash

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}

172)Write a script using the read command.


->#!/bin/bash

read -p "Enter you salary" SAL


echo "Your salary is:"${SAL}

173)Which command is used to start debugging mode.


->$set -x

174)Which command is used in debugging mode.


->$set +x

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

176)Which command is used to edit the crontab command


->$crontab -e

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

Step2:-The general format of a cron job line is as follows:


$* * * * * command or $* * * * */path/to/command
For example, to schedule a command to run every day at 9:30 AM, you can add the
following line:
$30 9 * * * /home/ec2-userW/to/command

Step3:-To view the updated list of cron jobs, you can use the crontab -l command:
$crontab -l

178)Write a script to hide password while writing


->#!/bin/bash

read -p "Enter name:" UNAME


read -sp "Enter password:" PASSWARD

179)Example of command line argument


->Step1:- vi ex2.sh
Step2:-#!/bin/bash
# Access the first command-line argument
name=$1
# Display a greeting message
echo "Hello, $name! Welcome to Linux."
Step3:-./ex2.sh sakshi

180)Write a program to concatenate two strings.


->Step1:-vi ex3.sh
Step2:-#!/bin/bash

# Access the first command-line argument


name=$1
surname=$2

# Display a greeting message


echo "Hello," $name $surname" ! Welcome to Linux."
Step3:-./vi ex3.sh sakshi sawalikar..

181)Use expr to add two whole numbers.


->$expr 20+30 | bc

182)Use expr to add two decimal numbers


->expr 20.5+20.5 | bc

183)Use bc two add two whole numbers


->echo 20+24 | bc

184)Use bc two add 20.5 and 74


->echo 20.5+74 | bc

185)Write a command to convert decimal to binary.


->echo "ibase=10;obase=2;47" | bc

186)Write a command to convert decimal to octal.


->echo "ibase=10;obase=8;47" |bc

187)Write a command to convert decimal to hexadecimal.


->echo "ibase=10;obase=16;47" | bc

188)Write a command to compress a file.


->$gzip filename

189)Write a command to decompress a file


->$gzip -d filename.gz

190)How to tar and untar a file.


-> tar a file: $ tar -cvf archive.tar /path/to/your/file
untar a file: $ tar -xvf archive.tar

You might also like