Unix Interview Questions
1. How many users are logged in currently?
Who | wc –l
2. Currently logged in users detail?
who
Or
finger
3. Tell us about the Unix File System?
/ - Starts with root
/bin - Binary executable files (All commands store in this directory)
/lib - Libraries (which will include additional files to portable with
other languages like java)
/dev - Device related files (like printer, cd-drive, pen drive)
/etc - Configuration files usually required for System- Administration
/tmp - Temporary files
/usr/bin - Additional binary executable files
4. How to create files in Unix/Linux?
touch file_name
Or
cat > file_name
Or
vi file_name
5. How to read content in a file?
cat file_name
more file_name
Or
vi file_name
6. How to create empty file?
touch file_name
7. How to copy file in unix/linux?
cp source target
cp file_name new_name
8. How to copy directory structure?
Using “r” option in cp command
Like cp –r directory_name destination
9. How to copy multiple files in Unix/linux?
cp file1 file2 /dir_name
10.What will “I” option do while using cp command?
It will ask the user for confirmation before overwriting the files.
cp -I source destination
11.How to delete directory?
rm –r dir
or
rmdir dir (Useful only with empty directories)
12.How to rename a file?
mv old_name new_name
13.How to check hidden files?
ls -a
14.How to search all files whose name starts with vowel in current directory?
ls [aeiou]*
15.How to create link?
ln file_name link_name (for hard link)
Or
ln –s file_name link_name (for soft link)
16.What is the difference between soft link and hard link?
Soft link Hard Link
Size is different compare to Size is same compare to original
original file file
We can differentiate between We can’t differentiate between
original file and soft link. Soft hard link and Original file
link indicated with “l” keyword.
Soft link has different Inode Hard link has same Inode number
number
If we delete original file, link cannot be If we delete original file, link can be
accessible accessible
17.How to give permission to any file or directory?
chmod 765 file_name
r - 4
w -2
x - 1
18.How to change the owner of any file or directory?
chown username filename ( Only System Admin or Root user can
change the owner of any file)
19. How to change group of any file or directory?
Chgrp groupname filename
20.User is not able to change the directory. What could be the reason?
User doesn’t have execute permission on that directory.
We will give execute permission to that directory, and then we will be able
to change the directory using “cd” command.
21.What’s the default permission for file and directory?
File : 644 (rw-r--r--)
Directory: 755 (rwxr-xr-x)
22.Why is file’s default permission as 644?
Because of umask value set to 022.
If we change umask value to any value, that value will be subtracted from
666 for file and 777 for directory
Like if umask value is 043 then file default permission would be 666-043 =
623
And directory default permission would be 777-043
= 734
23.How to create directory structure at once?
Using “p” option in mkdir command.
Like mkdir -p /a/b/c/d
24.How to calculate arithmetic operations in Unix/Linux?
Using “bc” or “expr” command
25.From which User you are logged in?
echo $LOGNAME
Or
Whoami
26.How to check version of your operation system?
Or
How to check on which operating system are you working on?
Using “uname” command
27.Show the date in below format?
Date: day-month-year
Time: Hour:Minutes:Seconds
Date ‘+Date: %d-%m-%y %n Time:%H:%M:%S’
28.How to check inode numbers of any file?
ls -il
29.What are the information stored in an inode number?
Owner of the file
Group to which the owner belongs
Type of file
File access permission
Date and time of last access
Number of links to the file
Size of the file
30.How to find the latest file in current directory?
ls -lrth ( last file in the output , would be latest file )
31.How to check disk utilization?
Using “df” command
32.What is the difference between “df” and “du” command?
du :- Disk Usage. It walks through directory tree and counts the sum size of all
files therein. It will show “how much disk space is being used by these files?”
df : Disk Free. Looks at disk used blocks directly in file system metadata.
Because of this it returns much faster than du, But can only show info about the
entire disk/partition. It will show “How much free disk space do we have? “
33.How to restrict any user to create larger files?
Using “ulimit” command
ulimit 1
1 means 1 block ( 512 bytes)
34.What are the uses of touch command?
Create an empty file
Modification time change ( if file is already present )
35.How to check type of your file?
file file_name
36.How to check type of your command?
type command_name
eg. type ls , type echo
37.How many lines in your file?
wc -l file_name
38.Tell us all the options used with “wc” command?
-l : Count all the lines in a file
-w : Count all the words in a file
-c : count all the characters in a file
39.How to arrange data in ascending order on the basis of Name for below
given file?
1) Amit Physics 80
2) Rahul Maths 90
3) Shyam Biology 87
4) Kedar English 85
5) Hari History 89
sort -t' ' -k2 file_name
40.How to remove duplicate lines from a file?
sort -u file_name
Or
sort file_name | uniq
41.How to check which lines are duplicated in our file?
sort file_name | uniq –d
42.How to print only second field from the below given file?
1) Amit Physics 80
2) Rahul Maths 90
3) Shyam Biology 87
4) Kedar English 85
5) Hari History 89
cut -d' ' -f2 file_name
Or
awk '{ print $2 }' file_name
43.How to cut the “columns” word from below given string.
“ As a result, the first 15 columns from each line “
echo “As a result, the first 15 columns from each line” | cut -d' ' –f7
Or
echo “As a result, the first 15 columns from each line” | awk ‘{print $7}’
44.How to cut the string “the first 15 columns from each line” from above given
full string?
echo “As a result, the first 15 columns from each line” | cut -d',' –f2
Or
echo “As a result, the first 15 columns from each line” |awk –F’,’ ‘{print
$2}’
45.How to cut the string “ the fir” from above given full string?
echo “As a result, the first 15 columns from each line” | cut -d',' –f2 | cut
–c1-7
46.How to search “manjeet” from a file?
grep “manjeet” file_name
47.We want to search “manjeet” as a word in a file?
grep -w “manjeet” file_name
48.We want to search “manjeet” , no matter how it is written ( in capital or
small letters ) ?
grep -i “manjeet” file_name
49.List all the files, which contains “manjeet”?
grep -l “manjeet” *
50.Search all the line which doesn’t contain “manjeet”?
grep -v “manjeet” file_name
51.How many times “manjeet” as a whole word comes in a file?
grep -cw “manjeet” file_name
52.We want 2 above and 2 below lines from the line which contains “manjeet”?
grep -A2 -B2 “manjeet” file_name
53.How to reduce the size of any file?
gzip file_name
or
compress file_name
or
zip file_name
54.What is the extension of zipped file?
Or
How will you identify zipped files?
gzip - .gz
zip - .z
compress - .Z
55.Count all the zipped files in your current directory?
ls *.gz | wc –l
56.How to read the content of zipped files?
zcat zipped_filename
57.How to unzip, zipped files?
gunzip zipped_filename
Or
uncompress zipped_filename
58.How to read the file content in reverse order?
tac file_name
59.For how much time your system is up or running?
uptime
60.How to redirect the output to any file?
Or
How to redirect the error to any file?
Output : Using “>”
ls > file_name
Error: using “2>”
ls 2> file_name
61.How to get the largest file name in your current directory?
ls -l | sort -t’ ‘ -nk5 | tail -1 | awk ‘{print $9}’
62.How to get the latest file name in your current directory?
ls -lrth | tail -1 | awk ‘{print $9}’
Or
ls -rth | tail -1
63. How to redirect the output in two directions?
Using “tee” command
who | tee logfile | wc –l
64.How to search and replace in a file using vi ?
We will go to last line mode after pressing “:”
Then we will type
:1,$s/search/replace/g
“g” stands to replace the search pattern globally
Or
sed ‘1,$s/search/replace/g’ file_name
65. How to go to 5th line in a file?
Line number + G
Like if we want to go to fifth line then we will press 5G
If we press only “G” then we will move to the last line in a file.
66.How to move 4th to 9th lines at the end of file?
We will go to last line mode after pressing “:”
:4,9 mo $
Or if we want to copy then
: 4,9 co $
67.How to search a pattern in a file using vi?
We will go to last line mode after pressing “?”
Then we will type what we want to search
? manjeet
68.What is the configuration file name for vi?
.exrc
69.How to remove blank or empty lines from a file?
grep -v ‘^$’ file_name
70.How many links are present in current directory?
ls -l | grep ‘^l’ | wc -l
for files: ls -l | grep ‘^-‘ | wc –l
for dir : ls -l | grep ‘^d’ | wc -l
71.How to check process information?
ps -aef
if we want to search for any particular process
ps -aef | grep “java”
72.How to stop any process?
kill -9 pid ( PID: process id )
73.What is zombie process?
When a process is finished or killed, but still showing in ps listing with a Z
state. This process has completed execution but still has an entry in the
process table is called as Zombie process.
74.What is an orphan process?
When the parent process is killed before its child process, and the PPID of
child process replaced by init process PID ( 1 ), that is called as Orphan
process
75.How to put the process in background?
sleep 12456754367 &
76. What is use of nohup command?
nohup sleep 1235434756652 &
if we are to ensure that the processes that we have executed should not
die even when we log out.
77.What is the default file name created for nohup?
nohup.out
78.How to change the priorities of any process?
Using “nice” command
Higher the nice value of a process lower is its priority.
79.How to send mail using linux?
Using “sendmail” command
Using “mailx” command
80.Find all the files without permission 777.
find / -type f ! –perm 777
81.Find all Read Only files.
find / -perm /u=r
82.Find all 777 permission files and use chmod command to set permissions to
644.
find / -type f -perm 777 -exec chmod 644 {} \;
83.To find a single file called tecmint.txt and remove it.
find / -type f -name “tecmint.txt” -exec rm {} \;
84. To find all empty files under certain path
find / -type f -empty
Or
find / -size 0
85.To find all hidden files, use below command
find / -name ‘.*’
86.To find all the files which are modified 50 days back.
find / -mtime +50
find / -atime +50 (access time in days)
find / -ctime +50 ( Creation time in days)
87.To find all the files which are modified more than 50 days back and less than
100 days.
find / -mtime +50 -mtime -100
88.To find all the files which are modified in last 1 hour.
find / -mmin 60 ( modification time )
find / -cmin 60 ( changed time )
find / -amin 60 ( access time )
89. How to check CPU utilization?
Using “top” command
90. How to print last line from a file?
tail -1 file_name
or
sed -n ‘$ p’ file_name
91.How to print last field from a file?
awk -F’ ‘ ‘{print $NF}’ file_name
92.How to print 1st to 4th and 6th to 9th line from a file?
sed -n -e ‘1,4 p’ -e ‘6,9 p’ file_name
93.How to delete all the lines from a file?
sed ‘1,$ d’ file_name
94.Try to specifying address from 10,4d , then what will happen?
sed ’10,4d’ file_name
only 10th line will be deleted.
95. How will you replace “Manager” with “Execution” from 1st to 10th line only?
sed ‘1,10s/Manager/Execution/g’ file_name
96.How to convert lower case alphabets to Upper case alphabets.
cat file_name | tr –s ‘[a-z]’ ‘[A-Z]’
97.How to search first word and last word from a file?
sed -n ‘1p’ file_name | awk ‘{print $1}’
tail -1 file_name | awk ‘{print $NF}’
98. How to delete “7” character in a file?
cat file_name | tr -d ‘7’
99. How to delete all numbers from a file?
cat file_name | tr –d ‘[0-9]’
a. How will you delete every third line from a file?
sed –e ‘1~3 d’ file_name
100. How to print only odd lines/ Even lines?
Odd Even
sed -n ‘1~2 p’ file_name sed –n ‘2~2p’ file_name
101. What is difference between the shell variable which is exported and
not exported?
Exported variables can be used in child shell, however normal variables cannot be
used in child shell
102. How would you get the character positions 10-20 from a file?
cut –c10-20 file_name
103. How do you know what’s your current shell?
echo $SHELL
104. How do you execute a unix command in background?
ls &
105. How to reverse a string?
echo “linux” | rev
106. How to count a character “#” in a file?
cat file_name | tr -cd ‘#’ | wc –c
107. How to get yesterday’s date?
date -d “1 days ago”
108. If any command is not working, what will you do to make it working?
First we will check the command path, where it is located using “which”
command.
which command_name
like: if want to check that “ls” command is not working
which ls
it will provide the path of the command.
Then we will check, whether found path is present in PATH variable or not.
echo $PATH
if it is not present then add the founded path in PATH variable
PATH=$PATH:founded_path
Or we can directly copy the command into /usr/bin directory.
109. How will you alias any command?
alias ls=’ls –l’
110. How will you executed two commands at once?
By separating commands with “;”
Like: who ; date
111. How to make any change permanent?
Insert the variable, alias or any change that we want to do in .profile or .bashrc
files. After inserting the entry we need to execute
source .profile or source .bashrc
or
we can logout and login again to make it in effect
112. How to move your home directory at once?
cd ~
113. How to move to your previous working directory?
cd -
114. How to compare two files?
Using “cmp” or “diff” command
cmp file1 file2
or
diff file1 file2
115. How to check common lines in two files?
comm -12 file1 file2
116. How to copy file from one server to remote server?
Using “scp” or “ftp” command
scp file_name user_name@192.168.225.51:path
file_name: file name which you want to copy
User_name: on which user you want to copy on remote server
Path: where you want to copy in remote server
Or
ftp root@192.168.225.51
get file_name : to get the one file from remote server to local server
mget file1 file2 : to copy multiple files from remote server to local server
put file_name : to copy one file from local server to remote server
mput file1 file2 : to copy multiple files from local server to remote server
117. How to run command on remote server?
Using “ssh” command
ssh root@192.168.225.51
Now you are connected to remote server, you can execute whatever you want
118. What is the use of tar command?
We use “tar” command to create or extract the archives.
119. How to create archive?
tar -cvf file.tar file1 file2 file3 x abcd
120. How to extract the archive?
tar –xvf file.tar
121. How to create and zip the archive?
tar –cvfz file.tar.gz file1 file2 file3
Or
To extract this we use
tar -xvfz file.tar.gz
122. How to check the connectivity between two servers?
telnet 192.168.225.51 22
or
ping 192.168.225.51
123. How to total third column data of a file :-
Name item Amount
Anil Apple 50000
Manjeet Samsung 20000
Priyanka Vivo 15000
We want sum of amount only then :-
Ans :- awk ‘{s+=$3} END {print s}’ filename